Skip to content

Commit 808f1a8

Browse files
Fix getAttributeDescription() error (#264)
* test: Add a test to check getDescription() return value for invalid value * fix: Avoid error if value is invalid
1 parent a3d4704 commit 808f1a8

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

src/Enum.php

+3
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,9 @@ protected static function getAttributeDescription($value): ?string
403403
$reflection = self::getReflection();
404404
$constantName = static::getKey($value);
405405
$constReflection = $reflection->getReflectionConstant($constantName);
406+
if ($constReflection === false) {
407+
return null;
408+
}
406409
$descriptionAttributes = $constReflection->getAttributes(Description::class);
407410

408411
if (count($descriptionAttributes) === 1) {

tests/EnumAttributeDescriptionTest.php

+5
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,9 @@ public function test_an_exception_is_thrown_when_accessing_a_description_which_i
2525

2626
DescriptionFromAttribute::InvalidCaseWithMultipleDescriptions()->description;
2727
}
28+
29+
public function test_an_exception_is_not_thrown_when_accessing_a_description_for_an_invalid_value()
30+
{
31+
$this->assertSame('', DescriptionFromAttribute::getDescription('invalid'));
32+
}
2833
}

0 commit comments

Comments
 (0)