Skip to content

Commit 7cc60e8

Browse files
authored
[CodeQuality] Skip by-ref use() closures in CallbackSingleAssertToSimplerRector (#715)
1 parent 08758f4 commit 7cc60e8

2 files changed

Lines changed: 30 additions & 0 deletions

File tree

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Rector\PHPUnit\Tests\CodeQuality\Rector\MethodCall\CallbackSingleAssertToSimplerRector\Fixture;
4+
5+
use PHPUnit\Framework\TestCase;
6+
7+
final class SkipReferencedUse extends TestCase
8+
{
9+
public function test()
10+
{
11+
$download = null;
12+
13+
$builder = $this->getMockBuilder('AnyType')->getMock();
14+
15+
$builder->expects($this->once())
16+
->method('detach')
17+
->with($this->callback(function ($downloadDetach) use (&$download): bool {
18+
$this->assertSame($downloadDetach, $download);
19+
20+
return true;
21+
}));
22+
}
23+
}

rules/CodeQuality/Rector/MethodCall/CallbackSingleAssertToSimplerRector.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ private function matchCallbackSoleAssertSameExpected(Expr $expr): ?Expr
125125
return null;
126126
}
127127

128+
// skip closures capturing by reference, as the referenced value is likely modified above
129+
foreach ($innerClosure->uses as $use) {
130+
if ($use->byRef) {
131+
return null;
132+
}
133+
}
134+
128135
// exactly assertSame() expression + "return true;"
129136
$closureStmts = $innerClosure->getStmts();
130137
if (count($closureStmts) !== 2) {

0 commit comments

Comments
 (0)