Skip to content

Commit 7f240cf

Browse files
authored
Merge pull request #49 from svera/add-enum
Add enum to visitor
2 parents 3b85c72 + 0579a55 commit 7f240cf

File tree

5 files changed

+98
-4
lines changed

5 files changed

+98
-4
lines changed

dev/tests/Unit/ClassHierarchy/EntityTest.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,21 @@ public function testIsTrait(string $type, bool $expected)
479479
);
480480
}
481481

482+
/**
483+
* @dataProvider dataProviderIsEnum
484+
* @param string $type
485+
* @param bool $expected
486+
*/
487+
public function testIsEnum(string $type, bool $expected)
488+
{
489+
$entity = new Entity('myName', $type);
490+
491+
$this->assertEquals(
492+
$expected,
493+
$entity->isEnum()
494+
);
495+
}
496+
482497
/*
483498
* Data providers
484499
*/
@@ -503,6 +518,10 @@ public function dataProviderIsClass()
503518
Entity::TYPE_TRAIT,
504519
false,
505520
],
521+
'entity-is-enum-returns-false' => [
522+
Entity::TYPE_ENUM,
523+
false,
524+
],
506525
];
507526
}
508527

@@ -526,6 +545,10 @@ public function dataProviderIsInterface()
526545
Entity::TYPE_TRAIT,
527546
false,
528547
],
548+
'entity-is-enum-returns-false' => [
549+
Entity::TYPE_ENUM,
550+
false,
551+
],
529552
];
530553
}
531554

@@ -549,6 +572,37 @@ public function dataProviderIsTrait()
549572
Entity::TYPE_TRAIT,
550573
true,
551574
],
575+
'entity-is-enum-returns-false' => [
576+
Entity::TYPE_ENUM,
577+
false,
578+
],
579+
];
580+
}
581+
582+
/**
583+
* Provides test data for {@link EntityTest::testIsEnum()}
584+
*
585+
* @return array
586+
*/
587+
public function dataProviderIsEnum()
588+
{
589+
return [
590+
'entity-is-class-returns-false' => [
591+
Entity::TYPE_CLASS,
592+
false,
593+
],
594+
'entity-is-interface-returns-false' => [
595+
Entity::TYPE_INTERFACE,
596+
false,
597+
],
598+
'entity-is-trait-returns-false' => [
599+
Entity::TYPE_TRAIT,
600+
false,
601+
],
602+
'entity-is-enum-returns-true' => [
603+
Entity::TYPE_ENUM,
604+
true,
605+
],
552606
];
553607
}
554608

src/ClassHierarchy/DependencyGraph.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,20 @@ public function findOrCreateTrait(string $fullyQualifiedName): Entity
103103

104104
return $trait;
105105
}
106+
107+
/**
108+
* @param string $fullyQualifiedName
109+
* @return Entity
110+
*/
111+
public function findOrCreateEnum(string $fullyQualifiedName): Entity
112+
{
113+
$enum = $this->findEntityByName($fullyQualifiedName);
114+
115+
if (!$enum) {
116+
$enum = $this->entityFactory->createEnum($fullyQualifiedName);
117+
$this->addEntity($enum);
118+
}
119+
120+
return $enum;
121+
}
106122
}

src/ClassHierarchy/DependencyInspectionVisitor.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use Magento\SemanticVersionChecker\Helper\Node as NodeHelper;
1313
use PhpParser\Node;
14+
use PhpParser\Node\Stmt\Enum_ as EnumNode;
1415
use PhpParser\Node\Stmt\Class_ as ClassNode;
1516
use PhpParser\Node\Stmt\ClassLike;
1617
use PhpParser\Node\Stmt\ClassMethod;
@@ -22,7 +23,7 @@
2223
use PhpParser\NodeVisitorAbstract;
2324

2425
/**
25-
* Implements a visitor for `class`, `interface` and `trait` nodes that generates a dependency graph.
26+
* Implements a visitor for `class`, `interface`, `trait` and `enum` nodes that generates a dependency graph.
2627
*/
2728
class DependencyInspectionVisitor extends NodeVisitorAbstract
2829
{
@@ -94,8 +95,8 @@ public function enterNode(Node $node)
9495
}
9596

9697
/**
97-
* Handles Class, Interface, and Traits nodes. Sets currentClassLike entity and will populate extends, implements,
98-
* and API information
98+
* Handles Class, Interface, Traits and Enum nodes. Sets currentClassLike entity and will populate extends,
99+
* implements, and API information
99100
*
100101
* @param ClassLike $node
101102
* @return int|null
@@ -135,6 +136,9 @@ private function handleClassLike(ClassLike $node)
135136
case $node instanceof TraitNode:
136137
$this->currentClassLike = $this->dependencyGraph->findOrCreateTrait((string)$namespacedName);
137138
break;
139+
case $node instanceof EnumNode:
140+
$this->currentClassLike = $this->dependencyGraph->findOrCreateEnum((string)$namespacedName);
141+
break;
138142
}
139143
$this->currentClassLike->setIsApi($this->nodeHelper->isApiNode($node));
140144
return null;

src/ClassHierarchy/Entity.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use PhpParser\Node\Stmt\PropertyProperty;
1515

1616
/**
17-
* Implements an entity that reflects a `class`, `interface` or `trait` and its dependencies.
17+
* Implements an entity that reflects a `class`, `interface`, `enum` or `trait` and its dependencies.
1818
*/
1919
class Entity
2020
{
@@ -24,6 +24,7 @@ class Entity
2424
public const TYPE_CLASS = 'class';
2525
public const TYPE_INTERFACE = 'interface';
2626
public const TYPE_TRAIT = 'trait';
27+
public const TYPE_ENUM = 'enum';
2728
/**#@-*/
2829

2930
/**
@@ -327,6 +328,16 @@ public function isTrait(): bool
327328
return $this->type === self::TYPE_TRAIT;
328329
}
329330

331+
/**
332+
* Reflects whether current entity reflects an `enum`.
333+
*
334+
* @return bool
335+
*/
336+
public function isEnum(): bool
337+
{
338+
return $this->type === self::TYPE_ENUM;
339+
}
340+
330341
/*
331342
* Private methods
332343
*/

src/ClassHierarchy/EntityFactory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,13 @@ public function createTrait(string $name): Entity
4040
{
4141
return new Entity($name, Entity::TYPE_TRAIT);
4242
}
43+
44+
/**
45+
* @param string $name
46+
* @return Entity
47+
*/
48+
public function createEnum(string $name): Entity
49+
{
50+
return new Entity($name, Entity::TYPE_ENUM);
51+
}
4352
}

0 commit comments

Comments
 (0)