Skip to content

Commit 8021b36

Browse files
committed
Add support for using traits in enums
Although the basic architecture is present in the Enum type, the TraitUse strategy validates that only a class or other trait could have a trait. I have expanded the validation to include enums.
1 parent 9fac0fb commit 8021b36

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/phpDocumentor/Reflection/Php/Factory/TraitUse.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use InvalidArgumentException;
88
use phpDocumentor\Reflection\Fqsen;
99
use phpDocumentor\Reflection\Php\Class_;
10+
use phpDocumentor\Reflection\Php\Enum_;
1011
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
1112
use phpDocumentor\Reflection\Php\StrategyContainer;
1213
use phpDocumentor\Reflection\Php\Trait_;
@@ -31,8 +32,12 @@ public function create(ContextStack $context, object $object, StrategyContainer
3132

3233
$class = $context->peek();
3334

34-
if ($class instanceof Class_ === false && $class instanceof Trait_ === false) {
35-
throw new InvalidArgumentException('Traits can only be used in class or trait');
35+
if (
36+
$class instanceof Class_ === false
37+
&& $class instanceof Trait_ === false
38+
&& $class instanceof Enum_ === false
39+
) {
40+
throw new InvalidArgumentException('Traits can only be used in classes, enums or other traits');
3641
}
3742

3843
foreach ($object->traits as $trait) {

tests/unit/phpDocumentor/Reflection/Php/Factory/TraitUseTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use phpDocumentor\Reflection\Element;
99
use phpDocumentor\Reflection\Fqsen;
1010
use phpDocumentor\Reflection\Php\Class_ as Class_Element;
11+
use phpDocumentor\Reflection\Php\Enum_ as Enum_Element;
1112
use phpDocumentor\Reflection\Php\Interface_;
1213
use phpDocumentor\Reflection\Php\ProjectFactoryStrategies;
1314
use phpDocumentor\Reflection\Php\Trait_ as Trait_Element;
@@ -26,6 +27,7 @@ public function consumerProvider(): array
2627
return [
2728
[new Class_Element(new Fqsen('\MyClass'))],
2829
[new Trait_Element(new Fqsen('\MyTrait'))],
30+
[new Enum_Element(new Fqsen('\MyEnum'), null)],
2931
];
3032
}
3133

0 commit comments

Comments
 (0)