|
12 | 12 | namespace Translation\Extractor\Visitor\Php\Symfony; |
13 | 13 |
|
14 | 14 | use PhpParser\Node; |
15 | | -use PhpParser\Node\Stmt; |
| 15 | +use PhpParser\Node\Stmt\Class_; |
16 | 16 |
|
17 | 17 | trait FormTrait |
18 | 18 | { |
19 | 19 | private bool $isFormType = false; |
| 20 | + private array $classMap = []; |
| 21 | + private array $hierarchyCache = []; |
| 22 | + private array $symfonyInterface = [ |
| 23 | + 'Symfony\Component\Form\FormTypeInterface', |
| 24 | + 'FormTypeInterface', |
| 25 | + ]; |
20 | 26 |
|
21 | | - /** |
22 | | - * Check if this node is a form type. |
23 | | - */ |
24 | | - private function isFormType(Node $node): bool |
| 27 | + protected function registerClass(Class_ $node): void |
25 | 28 | { |
26 | | - // Check if the class implements FormTypeInterface |
27 | | - if ($node instanceof Stmt\Class_) { |
28 | | - $this->isFormType = \in_array( |
29 | | - 'Symfony\Component\Form\FormTypeInterface', |
30 | | - array_map(fn ($interface) => $interface->toString(), $node->implements ?? []) |
31 | | - ); |
| 29 | + if (!isset($node->name)) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + $fqcn = $node->name->toString(); |
| 34 | + |
| 35 | + $this->classMap[$fqcn] = [ |
| 36 | + 'extends' => $node->extends?->toString(), |
| 37 | + 'implements' => array_map( |
| 38 | + fn($i) => $i->toString(), |
| 39 | + $node->implements |
| 40 | + ), |
| 41 | + ]; |
| 42 | + } |
| 43 | + |
| 44 | + protected function resolveHierarchy( |
| 45 | + string $class, |
| 46 | + array &$visited = [] |
| 47 | + ): array { |
| 48 | + if (isset($this->hierarchyCache[$class])) { |
| 49 | + return $this->hierarchyCache[$class]; |
| 50 | + } |
| 51 | + |
| 52 | + if (isset($visited[$class])) { |
| 53 | + return [ |
| 54 | + 'extends' => [], |
| 55 | + 'implements' => [], |
| 56 | + ]; |
| 57 | + } |
| 58 | + |
| 59 | + $visited[$class] = true; |
| 60 | + |
| 61 | + $data = $this->classMap[$class] ?? null; |
| 62 | + |
| 63 | + if (!$data) { |
| 64 | + return [ |
| 65 | + 'extends' => [], |
| 66 | + 'implements' => [], |
| 67 | + ]; |
| 68 | + } |
| 69 | + |
| 70 | + $hierarchy = [ |
| 71 | + 'extends' => [], |
| 72 | + 'implements' => $data['implements'], |
| 73 | + ]; |
| 74 | + |
| 75 | + $parent = $data['extends']; |
| 76 | + |
| 77 | + if ($parent) { |
| 78 | + $hierarchy['extends'][$parent] = |
| 79 | + $this->resolveHierarchy($parent, $visited); |
| 80 | + } |
| 81 | + |
| 82 | + $this->hierarchyCache[$class] = $hierarchy; |
| 83 | + |
| 84 | + return $hierarchy; |
| 85 | + } |
| 86 | + |
| 87 | + protected function containsInterface( |
| 88 | + array $hierarchy |
| 89 | + ): bool { |
| 90 | + |
| 91 | + foreach ($this->symfonyInterface as $interface) { |
| 92 | + if (in_array($interface, $hierarchy['implements'], true)) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + foreach ($hierarchy['extends'] as $parentHierarchy) { |
| 98 | + if ($this->containsInterface($parentHierarchy)) { |
| 99 | + return true; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + return false; |
| 104 | + } |
| 105 | + |
| 106 | + protected function isFormType(Node $node): bool |
| 107 | + { |
| 108 | + if ($node instanceof Class_) { |
| 109 | + $this->registerClass($node); |
| 110 | + $hierarchy = $this->resolveHierarchy($node->name->toString()); |
| 111 | + $this->isFormType = $this->containsInterface($hierarchy); |
32 | 112 | } |
33 | 113 |
|
34 | 114 | return $this->isFormType; |
|
0 commit comments