Skip to content

Commit 8ccd850

Browse files
authored
Merge pull request #301 from phpDocumentor/feature/add-support-for-using-traits-in-enums
Add support for using traits in enums
2 parents 9fac0fb + 8021b36 commit 8ccd850

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)