Skip to content

Commit 6bb202c

Browse files
authored
[CodeQuality] Skip closure use by ref on InlineArrayReturnAssignRector (#7507)
* [CodeQuality] Skip closure use by ref on InlineArrayReturnAssignRector * fix
1 parent 306401c commit 6bb202c

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodeQuality\Rector\ClassMethod\InlineArrayReturnAssignRector\Fixture;
4+
5+
final class SkipUseClosureUseByRef
6+
{
7+
public static function getData(): array
8+
{
9+
$closure = function () use (&$person)
10+
{
11+
$person['name'] = 'Timmy';
12+
$person['surname'] = 'Back';
13+
14+
return $person;
15+
};
16+
17+
$person = ['test'];
18+
var_dump($closure());
19+
}
20+
}

rules/CodeQuality/Rector/ClassMethod/InlineArrayReturnAssignRector.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use PhpParser\Node\Expr\Array_;
99
use PhpParser\Node\Expr\ArrayDimFetch;
1010
use PhpParser\Node\Expr\Assign;
11+
use PhpParser\Node\Expr\Closure;
1112
use PhpParser\Node\Expr\New_;
1213
use PhpParser\Node\Expr\Variable;
1314
use PhpParser\Node\FunctionLike;
@@ -100,6 +101,18 @@ public function refactor(Node $node): ?Node
100101
return null;
101102
}
102103
}
104+
105+
if ($node instanceof Closure) {
106+
foreach ($node->uses as $use) {
107+
if (! $use->byRef) {
108+
continue;
109+
}
110+
111+
if ($this->isName($use->var, $returnedVariableName)) {
112+
return null;
113+
}
114+
}
115+
}
103116
}
104117

105118
$emptyArrayAssign = $this->resolveDefaultEmptyArrayAssign($stmts, $returnedVariableName);

0 commit comments

Comments
 (0)