|
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | 8 | use PhpParser\Node\Arg; |
| 9 | +use PhpParser\Node\ArrayItem; |
9 | 10 | use PhpParser\Node\Attribute; |
10 | 11 | use PhpParser\Node\Expr\Array_; |
11 | 12 | use PhpParser\Node\Expr\ArrayDimFetch; |
12 | 13 | use PhpParser\Node\Expr\BinaryOp\BooleanAnd; |
13 | 14 | use PhpParser\Node\Expr\Closure; |
14 | 15 | use PhpParser\Node\Expr\Isset_; |
| 16 | +use PhpParser\Node\Expr\MethodCall; |
15 | 17 | use PhpParser\Node\Expr\PostDec; |
16 | 18 | use PhpParser\Node\Expr\PostInc; |
17 | 19 | use PhpParser\Node\Expr\PreDec; |
18 | 20 | use PhpParser\Node\Expr\PreInc; |
19 | 21 | use PhpParser\Node\Expr\PropertyFetch; |
20 | 22 | use PhpParser\Node\Expr\StaticPropertyFetch; |
21 | 23 | use PhpParser\Node\Expr\Variable; |
| 24 | +use PhpParser\Node\Identifier; |
22 | 25 | use PhpParser\Node\Name\FullyQualified; |
23 | 26 | use PhpParser\Node\Param; |
| 27 | +use PhpParser\Node\Scalar\String_; |
24 | 28 | use PhpParser\Node\Stmt\Break_; |
25 | 29 | use PhpParser\Node\Stmt\Class_; |
26 | 30 | use PhpParser\Node\Stmt\Do_; |
@@ -87,6 +91,16 @@ public function enterNode(Node $node): ?Node |
87 | 91 | return null; |
88 | 92 | } |
89 | 93 |
|
| 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 | + |
90 | 104 | if ($node instanceof If_ || $node instanceof Else_ || $node instanceof ElseIf_) { |
91 | 105 | $this->processContextInIf($node); |
92 | 106 | return null; |
@@ -140,6 +154,55 @@ private function processContextInClass(Node $node): void |
140 | 154 | } |
141 | 155 | } |
142 | 156 |
|
| 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 | + |
143 | 206 | private function processContextInAttribute(Attribute $attribute): void |
144 | 207 | { |
145 | 208 | $this->simpleCallableNodeTraverser->traverseNodesWithCallable( |
|
0 commit comments