|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace voku\PhpDocFixer\XmlDocs; |
| 6 | + |
| 7 | +final class XmlReader |
| 8 | +{ |
| 9 | + private string $xml_path; |
| 10 | + |
| 11 | + public function __construct(string $xml_path) |
| 12 | + { |
| 13 | + $this->xml_path = $xml_path; |
| 14 | + } |
| 15 | + |
| 16 | + /** |
| 17 | + * @return array |
| 18 | + * |
| 19 | + * @phpstan-return array<string, array{ |
| 20 | + * return?: string, |
| 21 | + * params?: array<string, string>, |
| 22 | + * absoluteFilePath: string |
| 23 | + * }> |
| 24 | + */ |
| 25 | + public function parse(): array |
| 26 | + { |
| 27 | + $xmlParser = new \voku\helper\XmlDomParser(); |
| 28 | + $xmlParser->autoRemoveXPathNamespaces(); |
| 29 | + |
| 30 | + $finder = new \Symfony\Component\Finder\Finder(); |
| 31 | + $finder->files()->in($this->xml_path); |
| 32 | + |
| 33 | + $data = [[]]; |
| 34 | + foreach ($finder as $file) { |
| 35 | + $fileName = $file->getFilename(); |
| 36 | + if (\strpos($fileName, '.xml') === false) { |
| 37 | + continue; |
| 38 | + } |
| 39 | + |
| 40 | + $absoluteFilePath = $file->getPath() . \DIRECTORY_SEPARATOR . $fileName; |
| 41 | + $content = $file->getContents(); |
| 42 | + |
| 43 | + \preg_match('#<methodsynopsis>.*</methodsynopsis>#Uis', $content, $contentMethod); |
| 44 | + $contentMethod = $contentMethod[0] ?? null; |
| 45 | + |
| 46 | + \preg_match('#<methodsynopsis role="oop">.*</methodsynopsis>#Uis', $content, $contentMethodOop); |
| 47 | + $contentMethodOop = $contentMethodOop[0] ?? null; |
| 48 | + |
| 49 | + \preg_match('#<constructorsynopsis>.*</constructorsynopsis>#Uis', $content, $contentConstructor); |
| 50 | + $contentConstructor = $contentConstructor[0] ?? null; |
| 51 | + |
| 52 | + if (!$contentMethod && !$contentMethodOop && !$contentConstructor) { |
| 53 | + continue; |
| 54 | + } |
| 55 | + |
| 56 | + if ($contentMethod) { |
| 57 | + $methodsynopsis = $xmlParser->loadXml($contentMethod); |
| 58 | + $data[] = $this->findTypes($methodsynopsis, $absoluteFilePath); |
| 59 | + } |
| 60 | + |
| 61 | + if ($contentMethodOop) { |
| 62 | + $methodsynopsisOop = $xmlParser->loadXml($contentMethodOop); |
| 63 | + $data[] = $this->findTypes($methodsynopsisOop, $absoluteFilePath); |
| 64 | + } |
| 65 | + |
| 66 | + if ($contentConstructor) { |
| 67 | + $constructorsynopsis = $xmlParser->loadXml($contentConstructor); |
| 68 | + $data[] = $this->findTypes($constructorsynopsis, $absoluteFilePath); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + return \array_merge([], ...$data); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param \voku\helper\XmlDomParser $xmlParser |
| 77 | + * @param string $absoluteFilePath |
| 78 | + * |
| 79 | + * @return array |
| 80 | + * |
| 81 | + * @phpstan-return array<string, array{ |
| 82 | + * return?: string, |
| 83 | + * params?: array<string, string>, |
| 84 | + * absoluteFilePath: string |
| 85 | + * }> |
| 86 | + */ |
| 87 | + private function findTypes( |
| 88 | + \voku\helper\XmlDomParser $xmlParser, |
| 89 | + string $absoluteFilePath |
| 90 | + ): array { |
| 91 | + // init |
| 92 | + $data = []; |
| 93 | + |
| 94 | + $name = $xmlParser->findOne('//methodname')->text(); |
| 95 | + $data[$name]['absoluteFilePath'] = $absoluteFilePath; |
| 96 | + |
| 97 | + $returnTypesArray = []; |
| 98 | + $returnTypesTmp = $xmlParser->findMultiOrFalse('type.union type'); |
| 99 | + if ($returnTypesTmp !== false) { |
| 100 | + foreach ($returnTypesTmp as $returnTypeTmp) { |
| 101 | + $returnTypeText = $returnTypeTmp->text(); |
| 102 | + $returnTypesArray[$returnTypeText] = \ltrim($returnTypeText, '\\'); |
| 103 | + } |
| 104 | + } elseif (($returnTypeTmp = $xmlParser->findOneOrFalse('type')) !== false) { |
| 105 | + $returnTypeText = $returnTypeTmp->text(); |
| 106 | + $returnTypesArray[$returnTypeText] = \ltrim($returnTypeText, '\\'); |
| 107 | + } |
| 108 | + $data[$name]['return'] = \implode('|', $returnTypesArray ?? []); |
| 109 | + |
| 110 | + $params = $xmlParser->findMultiOrFalse('//methodparam'); |
| 111 | + if ($params !== false) { |
| 112 | + foreach ($params as $param) { |
| 113 | + $paramName = $param->findOne('parameter')->text(); |
| 114 | + $paramTypesArray = []; |
| 115 | + |
| 116 | + $paramTypesTmp = $param->findMultiOrFalse('type.union type'); |
| 117 | + if ($paramTypesTmp !== false) { |
| 118 | + foreach ($paramTypesTmp as $paramTypeTmp) { |
| 119 | + $paramTypeText = $paramTypeTmp->text(); |
| 120 | + $paramTypesArray[$paramTypeText] = \ltrim($paramTypeText, '\\'); |
| 121 | + } |
| 122 | + } elseif (($paramTypeTmp = $param->findOneOrFalse('type')) !== false) { |
| 123 | + $paramTypeText = $paramTypeTmp->text(); |
| 124 | + $paramTypesArray[$paramTypeText] = \ltrim($paramTypeText, '\\'); |
| 125 | + } |
| 126 | + |
| 127 | + $data[$name]['params'][$paramName] = \implode('|', $paramTypesArray ?? []); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return $data; |
| 132 | + } |
| 133 | +} |
0 commit comments