From 2db3db719f692545e1e970bbb82cea14906e2650 Mon Sep 17 00:00:00 2001 From: Rustam Date: Sun, 15 Oct 2023 18:10:19 +0500 Subject: [PATCH] Move parser classifier into separate PR --- src/ParserClassifier.php | 55 ---------------------- src/ParserVisitor.php | 86 ---------------------------------- tests/ParserClassifierTest.php | 16 ------- 3 files changed, 157 deletions(-) delete mode 100644 src/ParserClassifier.php delete mode 100644 src/ParserVisitor.php delete mode 100644 tests/ParserClassifierTest.php diff --git a/src/ParserClassifier.php b/src/ParserClassifier.php deleted file mode 100644 index 13a8b19..0000000 --- a/src/ParserClassifier.php +++ /dev/null @@ -1,55 +0,0 @@ -parser = (new ParserFactory())->create(ParserFactory::ONLY_PHP7); - - $traverser = new NodeTraverser(); - $traverser->addVisitor(new NameResolver()); - - $this->nodeTraverser = $traverser; - } - - /** - * @return iterable - */ - protected function getAvailableClasses(): iterable - { - $files = $this->getFiles(); - $visitor = new ParserVisitor($this->interfaces, $this->attributes, $this->parentClass); - $this->nodeTraverser->addVisitor($visitor); - - foreach ($files as $file) { - try { - $nodes = $this->parser->parse($file->getContents()); - if ($nodes !== null) { - $this->nodeTraverser->traverse($nodes); - } - } catch (\Throwable) { - // Ignore broken files or parsing errors - } - } - - return $visitor->getClassNames(); - } -} diff --git a/src/ParserVisitor.php b/src/ParserVisitor.php deleted file mode 100644 index 6344a87..0000000 --- a/src/ParserVisitor.php +++ /dev/null @@ -1,86 +0,0 @@ - - */ - private array $classNames = []; - - /** - * @psalm-param class-string $allowedParentClass - */ - public function __construct( - private array $allowedInterfaces, - private array $allowedAttributes, - private ?string $allowedParentClass = null - ) { - } - - public function enterNode(Node $node) - { - if (!($node instanceof Class_)) { - return parent::enterNode($node); - } - - if (!$this->skipClass($node)) { - /** - * @var class-string $className - * @psalm-suppress PossiblyNullReference checked in {@see skipClass} method. - */ - $className = $node->namespacedName->toString(); - $this->classNames[] = $className; - } - - return NodeTraverser::DONT_TRAVERSE_CHILDREN; - } - - /** - * @return array - */ - public function getClassNames(): array - { - return $this->classNames; - } - - private function skipClass(Class_ $class): bool - { - if ($class->namespacedName === null || $class->isAnonymous()) { - return true; - } - $className = $class->namespacedName->toString(); - $interfacesNames = class_implements($className); - if ( - $interfacesNames !== false && - count(array_intersect($this->allowedInterfaces, $interfacesNames)) !== count($this->allowedInterfaces) - ) { - return true; - } - $attributesNames = []; - foreach ($class->attrGroups as $attrGroup) { - foreach ($attrGroup->attrs as $attr) { - $attributesNames[] = $attr->name->toString(); - } - } - if (count(array_intersect($this->allowedAttributes, $attributesNames)) !== count($this->allowedAttributes)) { - return true; - } - - $classParents = class_parents($className); - - return ($this->allowedParentClass !== null && $classParents !== false) && - !in_array($this->allowedParentClass, $classParents, true); - } -} diff --git a/tests/ParserClassifierTest.php b/tests/ParserClassifierTest.php deleted file mode 100644 index 488355a..0000000 --- a/tests/ParserClassifierTest.php +++ /dev/null @@ -1,16 +0,0 @@ -