1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of phpDocumentor. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @copyright 2010-2018 Mike van Riel<[email protected]> |
11
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
12
|
|
|
* @link http://phpdoc.org |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace phpDocumentor\Reflection\Php\Factory; |
16
|
|
|
|
17
|
|
|
use phpDocumentor\Reflection\Location; |
18
|
|
|
use phpDocumentor\Reflection\Php\Constant as ConstantElement; |
19
|
|
|
use phpDocumentor\Reflection\Php\StrategyContainer; |
20
|
|
|
use phpDocumentor\Reflection\PrettyPrinter; |
21
|
|
|
use phpDocumentor\Reflection\Types\Context; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Strategy to convert ClassConstantIterator to ConstantElement |
25
|
|
|
* |
26
|
|
|
* @see ConstantElement |
27
|
|
|
* @see ClassConstantIterator |
28
|
|
|
*/ |
29
|
|
|
final class ClassConstant extends AbstractFactory |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var PrettyPrinter |
33
|
|
|
*/ |
34
|
|
|
private $valueConverter; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Initializes the object. |
38
|
|
|
*/ |
39
|
|
|
public function __construct(PrettyPrinter $prettyPrinter) |
40
|
|
|
{ |
41
|
|
|
$this->valueConverter = $prettyPrinter; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function matches($object): bool |
45
|
|
|
{ |
46
|
|
|
return $object instanceof ClassConstantIterator; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Creates an Constant out of the given object. |
51
|
|
|
* Since an object might contain other objects that need to be converted the $factory is passed so it can be |
52
|
|
|
* used to create nested Elements. |
53
|
|
|
* |
54
|
|
|
* @param ClassConstantIterator $object object to convert to an Element |
55
|
|
|
* @param StrategyContainer $strategies used to convert nested objects. |
56
|
|
|
* @param Context $context of the created object |
57
|
|
|
* @return ConstantElement |
58
|
|
|
*/ |
59
|
|
|
protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null) |
60
|
|
|
{ |
61
|
|
|
$docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context); |
62
|
|
|
$default = null; |
63
|
|
|
if ($object->getValue() !== null) { |
64
|
|
|
$default = $this->valueConverter->prettyPrintExpr($object->getValue()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return new ConstantElement($object->getFqsen(), $docBlock, $default, new Location($object->getLine())); |
|
|
|
|
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
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.