Skip to content

Commit

Permalink
Implemented ReflectionClassConstant::isFinal()
Browse files Browse the repository at this point in the history
  • Loading branch information
kukulich committed Oct 14, 2021
1 parent 7fe7b82 commit 72a5cf3
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
Expand Up @@ -101,7 +101,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
Expand Up @@ -17,6 +17,8 @@

class ReflectionClassConstant
{
public const IS_FINAL = 32;

private ?CompiledValue $compiledValue = null;

private Reflector $reflector;
Expand Down Expand Up @@ -104,6 +106,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 @@ -113,6 +120,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
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
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
@@ -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
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';
}
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
Expand Up @@ -58,6 +58,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 @@ -75,9 +81,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
Expand Up @@ -311,6 +311,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 @@ -330,7 +331,7 @@ public function testGetReflectionConstants(): void
{
$reflector = new DefaultReflector($this->getComposerLocator());
$classInfo = $reflector->reflectClass(ExampleClass::class);
self::assertCount(5, $classInfo->getReflectionConstants());
self::assertCount(6, $classInfo->getReflectionConstants());
}

public function testGetReflectionConstant(): void
Expand All @@ -342,6 +343,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
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 72a5cf3

Please sign in to comment.