1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of phpDocumentor. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright 2010-2015 Mike van Riel<[email protected]> |
9
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
10
|
|
|
* @link http://phpdoc.org |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
|
14
|
|
|
namespace phpDocumentor\Reflection\Php\Factory; |
15
|
|
|
|
16
|
|
|
use phpDocumentor\Reflection\Php\Constant as ConstantElement; |
17
|
|
|
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy; |
18
|
|
|
use phpDocumentor\Reflection\Php\StrategyContainer; |
19
|
|
|
use phpDocumentor\Reflection\PrettyPrinter; |
20
|
|
|
use phpDocumentor\Reflection\Types\Context; |
21
|
|
|
use PhpParser\Comment\Doc; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Strategy to convert ClassConstantIterator to ConstantElement |
25
|
|
|
* |
26
|
|
|
* @see ConstantElement |
27
|
|
|
* @see ClassConstantIterator |
28
|
|
|
*/ |
29
|
|
|
final class Constant extends AbstractFactory implements ProjectFactoryStrategy |
30
|
|
|
{ |
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
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns true when the strategy is able to handle the object. |
48
|
|
|
* |
49
|
|
|
* @param object $object object to check. |
50
|
|
|
* @return boolean |
51
|
|
|
*/ |
52
|
|
|
public function matches($object) |
53
|
|
|
{ |
54
|
|
|
return $object instanceof ClassConstantIterator; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Creates an Constant out of the given object. |
59
|
|
|
* Since an object might contain other objects that need to be converted the $factory is passed so it can be |
60
|
|
|
* used to create nested Elements. |
61
|
|
|
* |
62
|
|
|
* @param ClassConstantIterator $object object to convert to an Element |
63
|
|
|
* @param StrategyContainer $strategies used to convert nested objects. |
64
|
|
|
* @param Context $context of the created object |
65
|
|
|
* @return Constant |
66
|
|
|
*/ |
67
|
|
|
protected function doCreate($object, StrategyContainer $strategies, Context $context = null) |
68
|
|
|
{ |
69
|
|
|
$docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $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
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.