Skip to content

Commit

Permalink
Implemented ReflectionClassConstant::isFinal()
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Sep 23, 2021
1 parent de0a34d commit c0ee39b
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Reflection/Adapter/ReflectionClassConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public function getAttributes(?string $name = null, int $flags = 0): array

public function isFinal(): bool
{
throw new Exception\NotImplemented('Not implemented');
return $this->betterClassConstant->isFinal();
}

public function isEnumCase(): bool
Expand Down
8 changes: 8 additions & 0 deletions src/Reflection/ReflectionClassConstant.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

class ReflectionClassConstant
{
public const IS_FINAL = 32;

private bool $valueWasCached = false;

/** @var scalar|array<scalar>|null const value */
Expand Down Expand Up @@ -108,6 +110,11 @@ public function isProtected(): bool
return $this->node->isProtected();
}

public function isFinal(): bool
{
return $this->node->isFinal();
}

/**
* Returns a bitfield of the access modifiers for this constant
*/
Expand All @@ -117,6 +124,7 @@ public function getModifiers(): int
$val += $this->isPublic() ? CoreReflectionClassConstant::IS_PUBLIC : 0;
$val += $this->isProtected() ? CoreReflectionClassConstant::IS_PROTECTED : 0;
$val += $this->isPrivate() ? CoreReflectionClassConstant::IS_PRIVATE : 0;
$val += $this->isFinal() ? self::IS_FINAL : 0;

return $val;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ public static function toString(ReflectionClassConstant $constantReflection): st
$value = $constantReflection->getValue();

return sprintf(
"Constant [ %s %s %s ] { %s }\n",
"Constant [ %s%s %s %s ] { %s }\n",
$constantReflection->isFinal() ? 'final ' : '',
self::visibilityToString($constantReflection),
gettype($value),
$constantReflection->getName(),
Expand Down
1 change: 1 addition & 0 deletions test/unit/Fixture/ExampleClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class ExampleClass
public const MY_CONST_3 = 345;
protected const MY_CONST_4 = 456;
private const MY_CONST_5 = 567;
public final const MY_CONST_6 = 678;

/**
* @var int|float|\stdClass
Expand Down
9 changes: 5 additions & 4 deletions test/unit/Fixture/ExampleClassExport.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
Class [ <user> class Roave\BetterReflectionTest\Fixture\ExampleClass ] {
@@ %s/test/unit/Fixture/ExampleClass.php 10-52
@@ %s/test/unit/Fixture/ExampleClass.php 10-53

- Constants [5] {
- Constants [6] {
Constant [ public integer MY_CONST_1 ] { 123 }
Constant [ public integer MY_CONST_2 ] { 234 }
Constant [ public integer MY_CONST_3 ] { 345 }
Constant [ protected integer MY_CONST_4 ] { 456 }
Constant [ private integer MY_CONST_5 ] { 567 }
Constant [ final public integer MY_CONST_6 ] { 678 }
}

- Static properties [1] {
Expand All @@ -26,7 +27,7 @@ Class [ <user> class Roave\BetterReflectionTest\Fixture\ExampleClass ] {

- Methods [2] {
Method [ <user, ctor> public method __construct ] {
@@ %s/test/unit/Fixture/ExampleClass.php 45 - 47
@@ %s/test/unit/Fixture/ExampleClass.php 46 - 48

- Parameters [2] {
Parameter #0 [ <optional> ?int $promotedProperty = 123 ]
Expand All @@ -35,7 +36,7 @@ Class [ <user> class Roave\BetterReflectionTest\Fixture\ExampleClass ] {
}

Method [ <user> public method someMethod ] {
@@ %s/test/unit/Fixture/ExampleClass.php 49 - 51
@@ %s/test/unit/Fixture/ExampleClass.php 50 - 52
}
}
}
1 change: 1 addition & 0 deletions test/unit/Fixture/StringCastClassConstants.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ class StringCastConstants
protected const PROTECTED_CONSTANT = 0;
private const PRIVATE_CONSTANT = 'string';
const NO_VISIBILITY_CONSTANT = [];
final public const FINAL_CONSTANT = 'final';
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function methodExpectationProvider(): array
['getDeclaringClass', null, $this->createMock(BetterReflectionClass::class), []],
['getDocComment', null, '', []],
['getAttributes', NotImplemented::class, null, []],
['isFinal', NotImplemented::class, null, []],
['isFinal', null, true, []],
['isEnumCase', NotImplemented::class, null, []],
];
}
Expand Down
8 changes: 8 additions & 0 deletions test/unit/Reflection/ReflectionClassConstantTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ public function testPrivateVisibility(): void
self::assertTrue($const->isPrivate());
}

public function testFinal(): void
{
$const = $this->getExampleConstant('MY_CONST_6');
self::assertTrue($const->isFinal());
}

public function testToString(): void
{
self::assertSame("Constant [ public integer MY_CONST_1 ] { 123 }\n", (string) $this->getExampleConstant('MY_CONST_1'));
Expand All @@ -73,9 +79,11 @@ public function getModifiersProvider(): array
{
return [
['MY_CONST_1', CoreReflectionClassConstant::IS_PUBLIC],
['MY_CONST_2', CoreReflectionClassConstant::IS_PUBLIC],
['MY_CONST_3', CoreReflectionClassConstant::IS_PUBLIC],
['MY_CONST_4', CoreReflectionClassConstant::IS_PROTECTED],
['MY_CONST_5', CoreReflectionClassConstant::IS_PRIVATE],
['MY_CONST_6', CoreReflectionClassConstant::IS_PUBLIC | ReflectionClassConstant::IS_FINAL],
];
}

Expand Down
4 changes: 3 additions & 1 deletion test/unit/Reflection/ReflectionClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ public function testGetConstants(): void
'MY_CONST_3' => 345,
'MY_CONST_4' => 456,
'MY_CONST_5' => 567,
'MY_CONST_6' => 678,
], $classInfo->getConstants());
}

Expand All @@ -328,7 +329,7 @@ public function testGetReflectionConstants(): void
{
$reflector = new ClassReflector($this->getComposerLocator());
$classInfo = $reflector->reflect(ExampleClass::class);
self::assertCount(5, $classInfo->getReflectionConstants());
self::assertCount(6, $classInfo->getReflectionConstants());
}

public function testGetReflectionConstant(): void
Expand All @@ -340,6 +341,7 @@ public function testGetReflectionConstant(): void
self::assertSame(345, $classInfo->getReflectionConstant('MY_CONST_3')->getValue());
self::assertSame(456, $classInfo->getReflectionConstant('MY_CONST_4')->getValue());
self::assertSame(567, $classInfo->getReflectionConstant('MY_CONST_5')->getValue());
self::assertSame(678, $classInfo->getReflectionConstant('MY_CONST_6')->getValue());
self::assertNull($classInfo->getConstant('NON_EXISTENT_CONSTANT'));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function toStringProvider(): array
['PROTECTED_CONSTANT', "Constant [ protected integer PROTECTED_CONSTANT ] { 0 }\n"],
['PRIVATE_CONSTANT', "Constant [ private string PRIVATE_CONSTANT ] { string }\n"],
['NO_VISIBILITY_CONSTANT', "Constant [ public array NO_VISIBILITY_CONSTANT ] { Array }\n"],
['FINAL_CONSTANT', "Constant [ final public string FINAL_CONSTANT ] { final }\n"],
];
}

Expand Down

0 comments on commit c0ee39b

Please sign in to comment.