Skip to content

Commit 1d5e7f8

Browse files
committed
use prettyPrinter to extract the value of a constant
1 parent 068b15c commit 1d5e7f8

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,11 @@ public function getDocComment()
8787
return $docComment;
8888
}
8989

90+
public function getValue()
91+
{
92+
return $this->classConstants->consts[$this->index]->value;
93+
}
94+
9095
/**
9196
* (PHP 5 &gt;= 5.0.0)<br/>
9297
* Return the current element

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

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
use phpDocumentor\Reflection\Php\Constant as ConstantElement;
1717
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
1818
use phpDocumentor\Reflection\Php\StrategyContainer;
19+
use phpDocumentor\Reflection\PrettyPrinter;
1920
use phpDocumentor\Reflection\Types\Context;
21+
use PhpParser\Comment\Doc;
2022

2123
/**
2224
* Strategy to convert ClassConstantIterator to ConstantElement
@@ -26,6 +28,21 @@
2628
*/
2729
class Constant implements ProjectFactoryStrategy
2830
{
31+
/**
32+
* @var PrettyPrinter
33+
*/
34+
private $valueConverter;
35+
36+
/**
37+
* Initializes the object.
38+
*
39+
* @param PrettyPrinter $prettyPrinter
40+
*/
41+
public function __construct(PrettyPrinter $prettyPrinter)
42+
{
43+
$this->valueConverter = $prettyPrinter;
44+
}
45+
2946
/**
3047
* Returns true when the strategy is able to handle the object.
3148
*
@@ -49,6 +66,28 @@ public function matches($object)
4966
*/
5067
public function create($object, StrategyContainer $strategies, Context $context = null)
5168
{
52-
return new ConstantElement($object->getFqsen());
69+
$docBlock = $this->createDocBlock($object->getDocComment(), $strategies, $context);
70+
$default = null;
71+
if ($object->getValue() !== null) {
72+
$default = $this->valueConverter->prettyPrintExpr($object->getValue());
73+
}
74+
75+
return new ConstantElement($object->getFqsen(), $docBlock, $default);
76+
}
77+
78+
/**
79+
* @param Doc $docBlock
80+
* @param StrategyContainer $strategies
81+
* @param Context $context
82+
* @return null|\phpDocumentor\Reflection\DocBlock
83+
*/
84+
private function createDocBlock(Doc $docBlock = null, StrategyContainer $strategies, Context $context = null)
85+
{
86+
if ($docBlock === null) {
87+
return null;
88+
}
89+
90+
$strategy = $strategies->findMatching($docBlock);
91+
return $strategy->create($docBlock, $strategies, $context);
5392
}
5493
}

src/phpDocumentor/Reflection/Php/ProjectFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public static function createInstance()
5050
[
5151
new Factory\Argument(),
5252
new Factory\Class_(),
53-
new Factory\Constant(),
53+
new Factory\Constant(new PrettyPrinter()),
5454
new Factory\DocBlock(DocBlockFactory::createInstance()),
5555
new Factory\File(NodesFactory::createInstance()),
5656
new Factory\Function_(),

tests/component/ProjectCreationTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ public function testCreateProjectWithClass()
5656
$this->assertArrayHasKey($fileName, $project->getFiles());
5757
$this->assertArrayHasKey('\\Pizza', $project->getFiles()[$fileName]->getClasses());
5858
$this->assertArrayHasKey('\\Pizza::PACKAGING', $project->getFiles()[$fileName]->getClasses()['\\Pizza']->getConstants());
59+
$constant = $project->getFiles()[$fileName]->getClasses()['\\Pizza']->getConstants()['\\Pizza::PACKAGING'];
60+
61+
$this->assertEquals('box',$constant->getValue());
5962
}
6063

6164
public function testWithNamespacedClass()

0 commit comments

Comments
 (0)