Skip to content

Commit 07ce695

Browse files
committed
Add support for final constants
1 parent 4107e81 commit 07ce695

File tree

6 files changed

+38
-6
lines changed

6 files changed

+38
-6
lines changed

src/phpDocumentor/Reflection/Php/Constant.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ final class Constant implements Element
3838
/** @var Visibility */
3939
private $visibility;
4040

41+
/** @var bool */
42+
private $final;
43+
4144
/**
4245
* Initializes the object.
4346
*/
@@ -46,13 +49,15 @@ public function __construct(
4649
?DocBlock $docBlock = null,
4750
?string $value = null,
4851
?Location $location = null,
49-
?Visibility $visibility = null
52+
?Visibility $visibility = null,
53+
bool $final = false
5054
) {
5155
$this->fqsen = $fqsen;
5256
$this->docBlock = $docBlock;
5357
$this->value = $value;
5458
$this->location = $location ?: new Location(-1);
5559
$this->visibility = $visibility ?: new Visibility(Visibility::PUBLIC_);
60+
$this->final = $final;
5661
}
5762

5863
/**
@@ -96,4 +101,9 @@ public function getVisibility(): Visibility
96101
{
97102
return $this->visibility;
98103
}
104+
105+
public function isFinal(): bool
106+
{
107+
return $this->final;
108+
}
99109
}

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ protected function doCreate(
7878
$this->createDocBlock($const->getDocComment(), $context->getTypeContext()),
7979
$const->getValue() !== null ? $this->valueConverter->prettyPrintExpr($const->getValue()) : null,
8080
new Location($const->getLine()),
81-
$this->buildVisibility($const)
81+
$this->buildVisibility($const),
82+
$const->isFinal()
8283
));
8384
}
8485
}

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

+5
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,11 @@ public function getValue(): Expr
109109
return $this->classConstants->consts[$this->index]->value;
110110
}
111111

112+
public function isFinal(): bool
113+
{
114+
return $this->classConstants->isFinal();
115+
}
116+
112117
/**
113118
* @link http://php.net/manual/en/iterator.current.php
114119
*/

tests/unit/phpDocumentor/Reflection/Php/ConstantTest.php

+9
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ public function testGetValue(): void
6060
$this->assertSame($this->value, $this->fixture->getValue());
6161
}
6262

63+
/**
64+
* @covers ::isFinal
65+
* @covers ::__construct
66+
*/
67+
public function testIsFinal(): void
68+
{
69+
$this->assertFalse($this->fixture->isFinal());
70+
}
71+
6372
/**
6473
* @covers ::getFqsen
6574
* @covers ::getName

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ final class ClassConstantIteratorTest extends MockeryTestCase
3434
* @covers ::valid()
3535
* @covers ::rewind()
3636
* @covers ::getName()
37+
* @covers ::getValue()
3738
* @covers ::getFqsen()
3839
*/
3940
public function testIterateProps(): void
4041
{
41-
$const1 = new Const_('\Space\MyClass::MY_CONST1', new Variable('a'));
42+
$const1 = new Const_('\Space\MyClass::MY_CONST1', new Variable('1'));
4243
$const1->fqsen = new Fqsen((string) $const1->name);
43-
$const2 = new Const_('\Space\MyClass::MY_CONST2', new Variable('b'));
44+
$const2 = new Const_('\Space\MyClass::MY_CONST2', new Variable('2'));
4445
$const2->fqsen = new Fqsen((string) $const2->name);
4546

4647
$classConstantNode = new ClassConst([$const1, $const2]);
@@ -49,7 +50,7 @@ public function testIterateProps(): void
4950
foreach (new ClassConstantIterator($classConstantNode) as $constant) {
5051
$this->assertEquals('\Space\MyClass::MY_CONST' . $i, $constant->getName());
5152
$this->assertEquals('\Space\MyClass::MY_CONST' . $i, (string) $constant->getFqsen());
52-
53+
$this->assertEquals($i, $constant->getValue()->name);
5354
++$i;
5455
}
5556
}

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

+7-1
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,15 @@ public function testMatches(): void
6060
}
6161

6262
/** @dataProvider visibilityProvider */
63-
public function testCreateWithVisibility(int $input, string $expectedVisibility): void
63+
public function testCreateWithVisibility(int $input, string $expectedVisibility, bool $isFinal = false): void
6464
{
6565
$constantStub = $this->buildConstantIteratorStub($input);
6666

6767
$class = $this->performCreate($constantStub);
6868

6969
$constant = current($class->getConstants());
7070
$this->assertConstant($constant, $expectedVisibility);
71+
$this->assertSame($isFinal, $constant->isFinal());
7172
}
7273

7374
/** @return array<string|int[]> */
@@ -86,6 +87,11 @@ public function visibilityProvider(): array
8687
ClassNode::MODIFIER_PRIVATE,
8788
'private',
8889
],
90+
[
91+
ClassNode::MODIFIER_PRIVATE | ClassNode::MODIFIER_FINAL,
92+
'private',
93+
true,
94+
],
8995
];
9096
}
9197

0 commit comments

Comments
 (0)