Skip to content

[CodeQuality] Skip new object instances in NarrowIdenticalWithConsecutiveRector#726

Merged
TomasVotruba merged 1 commit into
mainfrom
skip-new-object-narrow-consecutive
Jul 13, 2026
Merged

[CodeQuality] Skip new object instances in NarrowIdenticalWithConsecutiveRector#726
TomasVotruba merged 1 commit into
mainfrom
skip-new-object-narrow-consecutive

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Skip narrowing withConsecutive() / willReturnOnConsecutiveCalls() when any consecutive arg builds a new object.

Each new call creates a fresh instance, possibly with a property/constructor dependency, so collapsing them to a single with() / willReturn() changes intent.

 $someServiceMock->expects($this->exactly(3))
     ->method('prepare')
-    ->withConsecutive(
-        [new \stdClass()],
-        [new \stdClass()],
-        [new \stdClass()],
-    );
+    ->withConsecutive(
+        [new \stdClass()],
+        [new \stdClass()],
+        [new \stdClass()],
+    ); // now skipped, left as-is

Plain scalar repeats still narrow as before:

 $someServiceMock->expects($this->exactly(3))
-    ->method('prepare')
-    ->withConsecutive([1], [1], [1]);
+    ->method('prepare')->with([1]);

@TomasVotruba

Copy link
Copy Markdown
Member Author

To avoid side-effects, that we found in one project when new statefull class was created.

@TomasVotruba TomasVotruba merged commit 088cf1a into main Jul 13, 2026
8 checks passed
@TomasVotruba TomasVotruba deleted the skip-new-object-narrow-consecutive branch July 13, 2026 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant