|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Space48\CodeQuality\Utils; |
| 4 | + |
| 5 | +use PHPMD\AbstractNode; |
| 6 | + |
| 7 | +class MagentoClassTypeResolver |
| 8 | +{ |
| 9 | + private $types = []; |
| 10 | + |
| 11 | + private $typeConfig = [ |
| 12 | + 'plugin' => [ |
| 13 | + 'xmlNode' => 'plugin', |
| 14 | + 'xmlAttribute' => 'type', |
| 15 | + 'configFile' => 'di.xml' |
| 16 | + ], |
| 17 | + 'observer' => [ |
| 18 | + 'xmlNode' => 'observer', |
| 19 | + 'xmlAttribute' => 'instance', |
| 20 | + 'configFile' => 'events.xml' |
| 21 | + ] |
| 22 | + ]; |
| 23 | + |
| 24 | + /** |
| 25 | + * Returns type of the Magento class - 'plugin', 'observer', etc |
| 26 | + * |
| 27 | + * @param AbstractNode $node |
| 28 | + * @return false|string |
| 29 | + */ |
| 30 | + public function resolveType(AbstractNode $node) |
| 31 | + { |
| 32 | + if (!$this->types[$this->getClassName($node)]) { |
| 33 | + foreach ($this->typeConfig as $typeName => $type) { |
| 34 | + foreach (self::locateFiles($node, $type['configFile']) as $filePath) { |
| 35 | + $domDocument = new \DomDocument('1.0', 'UTF-8'); |
| 36 | + $domDocument->loadXML(\file_get_contents($filePath)); |
| 37 | + $domXPath = new \DOMXPath($domDocument); |
| 38 | + |
| 39 | + /** @var \DOMElement $nodeMatches [] */ |
| 40 | + $nodeMatches = $domXPath->query( |
| 41 | + \sprintf( |
| 42 | + '//%s[@%s="%s"]', |
| 43 | + $type['xmlNode'], |
| 44 | + $type['xmlAttribute'], |
| 45 | + $this->getClassName($node) |
| 46 | + ) |
| 47 | + ); |
| 48 | + |
| 49 | + if ($nodeMatches->length) { |
| 50 | + $this->types[$this->getClassName($node)] = $typeName; |
| 51 | + break 2; |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + return $this->types[$this->getClassName($node)] ?? false; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @param AbstractNode $node |
| 62 | + * @return string |
| 63 | + */ |
| 64 | + private function getClassName(AbstractNode $node): string |
| 65 | + { |
| 66 | + return $node->getNamespaceName() . '\\' . $node->getParentName(); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @param AbstractNode $node |
| 71 | + * @param string $filename |
| 72 | + * @return array |
| 73 | + */ |
| 74 | + private function locateFiles(AbstractNode $node, string $filename): array |
| 75 | + { |
| 76 | + $namespace = explode('\\', $node->getNamespaceName()); |
| 77 | + // [2] will be the name of the folder just after Module folder in standard Magento module structure |
| 78 | + if (empty($namespace[2])) { |
| 79 | + return []; |
| 80 | + } |
| 81 | + |
| 82 | + $path = strstr($node->getFileName(), $namespace[2], true) . 'etc'; |
| 83 | + |
| 84 | + return array_filter($this->searchFiles($path, $filename)); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @param string $path |
| 89 | + * @param string $fileName |
| 90 | + * @return array |
| 91 | + */ |
| 92 | + private function searchFiles(string $path, string $fileName): array |
| 93 | + { |
| 94 | + $configFiles = []; |
| 95 | + |
| 96 | + foreach (scandir($path) as $directory) { |
| 97 | + if ($directory == '.' || $directory == '..') { |
| 98 | + continue; |
| 99 | + } |
| 100 | + |
| 101 | + $filePath = $path . DIRECTORY_SEPARATOR . $directory; |
| 102 | + if ($directory == $fileName) { |
| 103 | + $configFiles[] = $filePath; |
| 104 | + } elseif (is_dir($filePath)) { |
| 105 | + $configFiles = array_merge($configFiles, $this->searchFiles($filePath, $fileName)); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return $configFiles; |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments