Skip to content

Commit 1ce7922

Browse files
authored
[Php81] Skip ArrayToFirstClassCallableRector on serialized callable array keys and Definition::setFactory() (#8117)
1 parent ef1594b commit 1ce7922

7 files changed

Lines changed: 141 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;
4+
5+
final class SkipCallbackKeyedArray
6+
{
7+
public function run()
8+
{
9+
$config = [
10+
'callback' => [$this, 'name'],
11+
];
12+
}
13+
14+
public function name()
15+
{
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;
4+
5+
final class SkipFactoryKeyedArray
6+
{
7+
public function run()
8+
{
9+
$config = [
10+
'factory' => [$this, 'name'],
11+
];
12+
}
13+
14+
public function name()
15+
{
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;
4+
5+
use Symfony\Component\DependencyInjection\Definition;
6+
7+
final class SkipSetFactoryArgument
8+
{
9+
public function run(Definition $definition)
10+
{
11+
$definition->setFactory([$this, 'name']);
12+
}
13+
14+
public function name()
15+
{
16+
}
17+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Rector\Tests\Php81\Rector\Array_\ArrayToFirstClassCallableRector\Fixture;
4+
5+
final class SkipValidationGroupsKeyedArray
6+
{
7+
public function run()
8+
{
9+
$config = [
10+
'validation_groups' => [$this, 'name'],
11+
];
12+
}
13+
14+
public function name()
15+
{
16+
}
17+
}

‎rules/Php81/Rector/Array_/ArrayToFirstClassCallableRector.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ public function refactor(Node $node): StaticCall|MethodCall|null
9494
return null;
9595
}
9696

97+
if ($node->getAttribute(AttributeKey::IS_ARRAY_AS_STRING_CALLABLE)) {
98+
return null;
99+
}
100+
97101
$scope = ScopeFetcher::fetch($node);
98102

99103
$arrayCallable = $this->arrayCallableMethodMatcher->match($node, $scope);

‎src/NodeTypeResolver/Node/AttributeKey.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,12 @@ final class AttributeKey
157157

158158
public const string IS_INSIDE_SYMFONY_PHP_CLOSURE = 'is_inside_symfony_php_closure';
159159

160+
/**
161+
* Array callable kept as data, not converted to first class callable,
162+
* e.g. 'callback'/'factory' keyed array item, or Definition::setFactory() argument
163+
*/
164+
public const string IS_ARRAY_AS_STRING_CALLABLE = 'is_array_as_string_callable';
165+
160166
public const string IS_INSIDE_BYREF_FUNCTION_LIKE = 'is_inside_byref_function_like';
161167

162168
public const string CLASS_CONST_FETCH_NAME = 'class_const_fetch_name';

‎src/PhpParser/NodeVisitor/ContextNodeVisitor.php‎

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,25 @@
66

77
use PhpParser\Node;
88
use PhpParser\Node\Arg;
9+
use PhpParser\Node\ArrayItem;
910
use PhpParser\Node\Attribute;
1011
use PhpParser\Node\Expr\Array_;
1112
use PhpParser\Node\Expr\ArrayDimFetch;
1213
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
1314
use PhpParser\Node\Expr\Closure;
1415
use PhpParser\Node\Expr\Isset_;
16+
use PhpParser\Node\Expr\MethodCall;
1517
use PhpParser\Node\Expr\PostDec;
1618
use PhpParser\Node\Expr\PostInc;
1719
use PhpParser\Node\Expr\PreDec;
1820
use PhpParser\Node\Expr\PreInc;
1921
use PhpParser\Node\Expr\PropertyFetch;
2022
use PhpParser\Node\Expr\StaticPropertyFetch;
2123
use PhpParser\Node\Expr\Variable;
24+
use PhpParser\Node\Identifier;
2225
use PhpParser\Node\Name\FullyQualified;
2326
use PhpParser\Node\Param;
27+
use PhpParser\Node\Scalar\String_;
2428
use PhpParser\Node\Stmt\Break_;
2529
use PhpParser\Node\Stmt\Class_;
2630
use PhpParser\Node\Stmt\Do_;
@@ -87,6 +91,16 @@ public function enterNode(Node $node): ?Node
8791
return null;
8892
}
8993

94+
if ($node instanceof Array_) {
95+
$this->processArrayInSerializedCallableKey($node);
96+
return null;
97+
}
98+
99+
if ($node instanceof MethodCall) {
100+
$this->processSetFactoryArgument($node);
101+
return null;
102+
}
103+
90104
if ($node instanceof If_ || $node instanceof Else_ || $node instanceof ElseIf_) {
91105
$this->processContextInIf($node);
92106
return null;
@@ -140,6 +154,55 @@ private function processContextInClass(Node $node): void
140154
}
141155
}
142156

157+
/**
158+
* The array is data, not a callable to convert: 'callback'/'factory' keyed item holds
159+
* a [$value, 'method'] pair likely to be serialized as a string.
160+
*/
161+
private function processArrayInSerializedCallableKey(Array_ $array): void
162+
{
163+
foreach ($array->items as $arrayItem) {
164+
if (! $arrayItem instanceof ArrayItem) {
165+
continue;
166+
}
167+
168+
if (! $arrayItem->key instanceof String_) {
169+
continue;
170+
}
171+
172+
if (! in_array($arrayItem->key->value, ['callback', 'factory', 'validation_groups'], true)) {
173+
continue;
174+
}
175+
176+
if ($arrayItem->value instanceof Array_) {
177+
$arrayItem->value->setAttribute(AttributeKey::IS_ARRAY_AS_STRING_CALLABLE, true);
178+
}
179+
}
180+
}
181+
182+
/**
183+
* Symfony's Definition::setFactory() does not accept a first class callable,
184+
* keep the [$value, 'method'] array as is.
185+
*/
186+
private function processSetFactoryArgument(MethodCall $methodCall): void
187+
{
188+
if (! $methodCall->name instanceof Identifier) {
189+
return;
190+
}
191+
192+
if ($methodCall->name->toString() !== 'setFactory') {
193+
return;
194+
}
195+
196+
$firstArg = $methodCall->args[0] ?? null;
197+
if (! $firstArg instanceof Arg) {
198+
return;
199+
}
200+
201+
if ($firstArg->value instanceof Array_) {
202+
$firstArg->value->setAttribute(AttributeKey::IS_ARRAY_AS_STRING_CALLABLE, true);
203+
}
204+
}
205+
143206
private function processContextInAttribute(Attribute $attribute): void
144207
{
145208
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(

0 commit comments

Comments
 (0)