Skip to content

Commit f8a9c1a

Browse files
committed
Merge pull request #78 from jaapio/feature/constantStrategy
Feature/constant strategy
2 parents 068b15c + 7753921 commit f8a9c1a

File tree

7 files changed

+76
-22
lines changed

7 files changed

+76
-22
lines changed

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

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use phpDocumentor\Reflection\Php\Factory;
1818
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy;
1919
use phpDocumentor\Reflection\Php\StrategyContainer;
20+
use phpDocumentor\Reflection\PrettyPrinter;
2021
use phpDocumentor\Reflection\Types\Context;
2122
use PhpParser\Node\Param;
2223

@@ -28,6 +29,20 @@
2829
*/
2930
final class Argument implements ProjectFactoryStrategy
3031
{
32+
/**
33+
* @var PrettyPrinter
34+
*/
35+
private $valueConverter;
36+
37+
/**
38+
* Initializes the object.
39+
*
40+
* @param PrettyPrinter $prettyPrinter
41+
*/
42+
public function __construct(PrettyPrinter $prettyPrinter)
43+
{
44+
$this->valueConverter = $prettyPrinter;
45+
}
3146

3247
/**
3348
* Returns true when the strategy is able to handle the object.
@@ -61,6 +76,11 @@ public function create($object, StrategyContainer $strategies, Context $context
6176
);
6277
}
6378

64-
return new ArgumentDescriptor($object->name, $object->default, $object->byRef, $object->variadic);
79+
$default = null;
80+
if ($object->default !== null) {
81+
$default = $this->valueConverter->prettyPrintExpr($object->default);
82+
}
83+
84+
return new ArgumentDescriptor($object->name, $default, $object->byRef, $object->variadic);
6585
}
6686
}

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public static function createInstance()
4848
{
4949
return new static(
5050
[
51-
new Factory\Argument(),
51+
new Factory\Argument(new PrettyPrinter()),
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()

tests/component/ProjectNamespaceTest.php

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -43,22 +43,7 @@ class ProjectNamespaceTest extends \PHPUnit_Framework_TestCase
4343
*/
4444
protected function setUp()
4545
{
46-
$docBlockFactory = DocBlockFactory::createInstance();
47-
48-
$this->fixture = new ProjectFactory(
49-
array(
50-
new Argument(),
51-
new Class_(),
52-
new Constant(),
53-
new DocBlockStrategy($docBlockFactory),
54-
new File(NodesFactory::createInstance()),
55-
new Function_(),
56-
new Interface_(),
57-
new Method(),
58-
new Property(new PrettyPrinter()),
59-
new Trait_(),
60-
)
61-
);
46+
$this->fixture = $this->fixture = ProjectFactory::createInstance();
6247
}
6348

6449
public function testWithNamespacedClass()

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,11 @@
1313

1414
use phpDocumentor\Reflection\Php\Argument as ArgumentDescriptor;
1515
use phpDocumentor\Reflection\Php\ProjectFactoryStrategies;
16+
use phpDocumentor\Reflection\PrettyPrinter;
1617
use phpDocumentor\Reflection\Types\Context;
1718
use PhpParser\Node\Param;
1819
use Mockery as m;
20+
use PhpParser\Node\Scalar\String_;
1921

2022
/**
2123
* Class ArgumentTest
@@ -25,7 +27,7 @@ class ArgumentTest extends TestCase
2527
{
2628
protected function setUp()
2729
{
28-
$this->fixture = new Argument();
30+
$this->fixture = new Argument(new PrettyPrinter());
2931
}
3032

3133
/**
@@ -46,7 +48,7 @@ public function testCreate()
4648

4749
$argMock = m::mock(Param::class);
4850
$argMock->name = 'myArgument';
49-
$argMock->default = 'MyDefault';
51+
$argMock->default = new String_('MyDefault');
5052
$argMock->byRef = true;
5153
$argMock->variadic = true;
5254

0 commit comments

Comments
 (0)