|
2 | 2 |
|
3 | 3 | namespace mglaman\PHPStanDrupal\Rules\Drupal;
|
4 | 4 |
|
| 5 | +use Drupal\Core\Render\PlaceholderGenerator; |
| 6 | +use Drupal\Core\Render\Renderer; |
5 | 7 | use mglaman\PHPStanDrupal\Drupal\ServiceMap;
|
6 | 8 | use PhpParser\Node;
|
7 | 9 | use PhpParser\Node\Name;
|
@@ -63,18 +65,22 @@ public function processNode(Node $node, Scope $scope): array
|
63 | 65 |
|
64 | 66 | // @todo Move into its own rule.
|
65 | 67 | if ($keyChecked === '#lazy_builder') {
|
66 |
| - // Check if being used in array_intersect_key. |
67 |
| - // NOTE: This only works against existing patterns in Drupal core where the array with boolean values is |
68 |
| - // being passed as the argument to array_intersect_key. |
69 |
| - $parent = $node->getAttribute('parent'); |
70 |
| - if ($parent instanceof Node\Expr\Array_) { |
71 |
| - $parent = $parent->getAttribute('parent'); |
72 |
| - if ($parent instanceof Node\Arg) { |
73 |
| - $parent = $parent->getAttribute('parent'); |
74 |
| - if ($parent instanceof Node\Expr\FuncCall |
75 |
| - && $parent->name instanceof Name |
76 |
| - && $parent->name->toString() === 'array_intersect_key' |
77 |
| - ) { |
| 68 | + if ($scope->isInClass()) { |
| 69 | + $classReflection = $scope->getClassReflection(); |
| 70 | + // @todo why doesn't isInClass assert this isn't null? |
| 71 | + assert($classReflection !== null); |
| 72 | + $classType = new ObjectType($classReflection->getName()); |
| 73 | + // These classes use #lazy_builder in array_intersect_key. With |
| 74 | + // PHPStan 1.6, nodes do not track their parent/next/prev which |
| 75 | + // saves a lot of memory. But makes it harder to detect if we're |
| 76 | + // in a call to array_intersect_key. This is an easier workaround. |
| 77 | + $allowedTypes = [ |
| 78 | + PlaceholderGenerator::class, |
| 79 | + Renderer::class, |
| 80 | + 'Drupal\Tests\Core\Render\RendererPlaceholdersTest', |
| 81 | + ]; |
| 82 | + foreach ($allowedTypes as $allowedType) { |
| 83 | + if ($classType->isInstanceOf($allowedType)->yes()) { |
78 | 84 | return [];
|
79 | 85 | }
|
80 | 86 | }
|
|
0 commit comments