From e24d68222e5a0e99899dbd447b471c277eefa170 Mon Sep 17 00:00:00 2001 From: Sergio Vera Date: Tue, 12 Sep 2023 15:19:55 +0200 Subject: [PATCH] Add enum to visitor --- dev/tests/Unit/ClassHierarchy/EntityTest.php | 54 +++++++++++++++++++ src/ClassHierarchy/DependencyGraph.php | 16 ++++++ .../DependencyInspectionVisitor.php | 10 ++-- src/ClassHierarchy/Entity.php | 13 ++++- src/ClassHierarchy/EntityFactory.php | 9 ++++ 5 files changed, 98 insertions(+), 4 deletions(-) diff --git a/dev/tests/Unit/ClassHierarchy/EntityTest.php b/dev/tests/Unit/ClassHierarchy/EntityTest.php index 17e60423..21c3027f 100644 --- a/dev/tests/Unit/ClassHierarchy/EntityTest.php +++ b/dev/tests/Unit/ClassHierarchy/EntityTest.php @@ -479,6 +479,21 @@ public function testIsTrait(string $type, bool $expected) ); } + /** + * @dataProvider dataProviderIsEnum + * @param string $type + * @param bool $expected + */ + public function testIsEnum(string $type, bool $expected) + { + $entity = new Entity('myName', $type); + + $this->assertEquals( + $expected, + $entity->isEnum() + ); + } + /* * Data providers */ @@ -503,6 +518,10 @@ public function dataProviderIsClass() Entity::TYPE_TRAIT, false, ], + 'entity-is-enum-returns-false' => [ + Entity::TYPE_ENUM, + false, + ], ]; } @@ -526,6 +545,10 @@ public function dataProviderIsInterface() Entity::TYPE_TRAIT, false, ], + 'entity-is-enum-returns-false' => [ + Entity::TYPE_ENUM, + false, + ], ]; } @@ -549,6 +572,37 @@ public function dataProviderIsTrait() Entity::TYPE_TRAIT, true, ], + 'entity-is-enum-returns-false' => [ + Entity::TYPE_ENUM, + false, + ], + ]; + } + + /** + * Provides test data for {@link EntityTest::testIsEnum()} + * + * @return array + */ + public function dataProviderIsEnum() + { + return [ + 'entity-is-class-returns-false' => [ + Entity::TYPE_CLASS, + false, + ], + 'entity-is-interface-returns-false' => [ + Entity::TYPE_INTERFACE, + false, + ], + 'entity-is-trait-returns-false' => [ + Entity::TYPE_TRAIT, + false, + ], + 'entity-is-enum-returns-true' => [ + Entity::TYPE_ENUM, + true, + ], ]; } diff --git a/src/ClassHierarchy/DependencyGraph.php b/src/ClassHierarchy/DependencyGraph.php index 1a80c75a..83e6e7ff 100644 --- a/src/ClassHierarchy/DependencyGraph.php +++ b/src/ClassHierarchy/DependencyGraph.php @@ -103,4 +103,20 @@ public function findOrCreateTrait(string $fullyQualifiedName): Entity return $trait; } + + /** + * @param string $fullyQualifiedName + * @return Entity + */ + public function findOrCreateEnum(string $fullyQualifiedName): Entity + { + $enum = $this->findEntityByName($fullyQualifiedName); + + if (!$enum) { + $enum = $this->entityFactory->createEnum($fullyQualifiedName); + $this->addEntity($enum); + } + + return $enum; + } } diff --git a/src/ClassHierarchy/DependencyInspectionVisitor.php b/src/ClassHierarchy/DependencyInspectionVisitor.php index 375ebc76..eae1db11 100644 --- a/src/ClassHierarchy/DependencyInspectionVisitor.php +++ b/src/ClassHierarchy/DependencyInspectionVisitor.php @@ -11,6 +11,7 @@ use Magento\SemanticVersionChecker\Helper\Node as NodeHelper; use PhpParser\Node; +use PhpParser\Node\Stmt\Enum_ as EnumNode; use PhpParser\Node\Stmt\Class_ as ClassNode; use PhpParser\Node\Stmt\ClassLike; use PhpParser\Node\Stmt\ClassMethod; @@ -22,7 +23,7 @@ use PhpParser\NodeVisitorAbstract; /** - * Implements a visitor for `class`, `interface` and `trait` nodes that generates a dependency graph. + * Implements a visitor for `class`, `interface`, `trait` and `enum` nodes that generates a dependency graph. */ class DependencyInspectionVisitor extends NodeVisitorAbstract { @@ -94,8 +95,8 @@ public function enterNode(Node $node) } /** - * Handles Class, Interface, and Traits nodes. Sets currentClassLike entity and will populate extends, implements, - * and API information + * Handles Class, Interface, Traits and Enum nodes. Sets currentClassLike entity and will populate extends, + * implements, and API information * * @param ClassLike $node * @return int|null @@ -135,6 +136,9 @@ private function handleClassLike(ClassLike $node) case $node instanceof TraitNode: $this->currentClassLike = $this->dependencyGraph->findOrCreateTrait((string)$namespacedName); break; + case $node instanceof EnumNode: + $this->currentClassLike = $this->dependencyGraph->findOrCreateEnum((string)$namespacedName); + break; } $this->currentClassLike->setIsApi($this->nodeHelper->isApiNode($node)); return null; diff --git a/src/ClassHierarchy/Entity.php b/src/ClassHierarchy/Entity.php index 7f87664f..cbd2ac8a 100644 --- a/src/ClassHierarchy/Entity.php +++ b/src/ClassHierarchy/Entity.php @@ -14,7 +14,7 @@ use PhpParser\Node\Stmt\PropertyProperty; /** - * Implements an entity that reflects a `class`, `interface` or `trait` and its dependencies. + * Implements an entity that reflects a `class`, `interface`, `enum` or `trait` and its dependencies. */ class Entity { @@ -24,6 +24,7 @@ class Entity public const TYPE_CLASS = 'class'; public const TYPE_INTERFACE = 'interface'; public const TYPE_TRAIT = 'trait'; + public const TYPE_ENUM = 'enum'; /**#@-*/ /** @@ -327,6 +328,16 @@ public function isTrait(): bool return $this->type === self::TYPE_TRAIT; } + /** + * Reflects whether current entity reflects an `enum`. + * + * @return bool + */ + public function isEnum(): bool + { + return $this->type === self::TYPE_ENUM; + } + /* * Private methods */ diff --git a/src/ClassHierarchy/EntityFactory.php b/src/ClassHierarchy/EntityFactory.php index 473bca32..880f0c86 100644 --- a/src/ClassHierarchy/EntityFactory.php +++ b/src/ClassHierarchy/EntityFactory.php @@ -40,4 +40,13 @@ public function createTrait(string $name): Entity { return new Entity($name, Entity::TYPE_TRAIT); } + + /** + * @param string $name + * @return Entity + */ + public function createEnum(string $name): Entity + { + return new Entity($name, Entity::TYPE_ENUM); + } }