diff --git a/rules-tests/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector/Fixture/skip_referenced_use.php.inc b/rules-tests/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector/Fixture/skip_referenced_use.php.inc new file mode 100644 index 00000000..2c4461d0 --- /dev/null +++ b/rules-tests/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector/Fixture/skip_referenced_use.php.inc @@ -0,0 +1,23 @@ +getMockBuilder('AnyType')->getMock(); + + $builder->expects($this->once()) + ->method('detach') + ->with($this->callback(function ($downloadDetach) use (&$download): bool { + $this->assertSame($downloadDetach, $download); + + return true; + })); + } +} diff --git a/rules/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector.php b/rules/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector.php index 4d4cae45..95db43ad 100644 --- a/rules/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector.php +++ b/rules/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector.php @@ -125,6 +125,13 @@ private function matchCallbackSoleAssertSameExpected(Expr $expr): ?Expr return null; } + // skip closures capturing by reference, as the referenced value is likely modified above + foreach ($innerClosure->uses as $use) { + if ($use->byRef) { + return null; + } + } + // exactly assertSame() expression + "return true;" $closureStmts = $innerClosure->getStmts(); if (count($closureStmts) !== 2) {