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
|
|
|
namespace phpDocumentor\Reflection\Php\Factory; |
14
|
|
|
|
15
|
|
|
use InvalidArgumentException; |
16
|
|
|
use phpDocumentor\Reflection\Php\Factory; |
17
|
|
|
use phpDocumentor\Reflection\Php\Property as PropertyDescriptor; |
18
|
|
|
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy; |
19
|
|
|
use phpDocumentor\Reflection\Php\StrategyContainer; |
20
|
|
|
use phpDocumentor\Reflection\Php\Visibility; |
21
|
|
|
use phpDocumentor\Reflection\PrettyPrinter; |
22
|
|
|
use phpDocumentor\Reflection\Types\Context; |
23
|
|
|
use PhpParser\Comment\Doc; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Strategy to convert PropertyIterator to PropertyDescriptor |
27
|
|
|
* |
28
|
|
|
* @see PropertyDescriptor |
29
|
|
|
* @see PropertyIterator |
30
|
|
|
*/ |
31
|
|
|
final class Property extends AbstractFactory implements ProjectFactoryStrategy |
32
|
|
|
{ |
33
|
|
|
/** |
34
|
|
|
* @var PrettyPrinter |
35
|
|
|
*/ |
36
|
|
|
private $valueConverter; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Initializes the object. |
40
|
|
|
* |
41
|
|
|
* @param PrettyPrinter $prettyPrinter |
42
|
|
|
*/ |
43
|
6 |
|
public function __construct(PrettyPrinter $prettyPrinter) |
44
|
|
|
{ |
45
|
6 |
|
$this->valueConverter = $prettyPrinter; |
46
|
6 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Returns true when the strategy is able to handle the object. |
50
|
|
|
* |
51
|
|
|
* @param object $object object to check. |
52
|
|
|
* @return boolean |
53
|
|
|
*/ |
54
|
1 |
|
public function matches($object) |
55
|
|
|
{ |
56
|
1 |
|
return $object instanceof PropertyIterator; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Creates an PropertyDescriptor out of the given object. |
61
|
|
|
* Since an object might contain other objects that need to be converted the $factory is passed so it can be |
62
|
|
|
* used to create nested Elements. |
63
|
|
|
* |
64
|
|
|
* @param PropertyIterator $object object to convert to an PropertyDescriptor |
65
|
|
|
* @param StrategyContainer $strategies used to convert nested objects. |
66
|
|
|
* @param Context $context |
67
|
|
|
* @return PropertyDescriptor |
68
|
|
|
*/ |
69
|
4 |
|
protected function doCreate($object, StrategyContainer $strategies, Context $context = null) |
70
|
|
|
{ |
71
|
4 |
|
$visibility = $this->buildVisibility($object); |
72
|
4 |
|
$default = null; |
73
|
4 |
|
if ($object->getDefault() !== null) { |
74
|
4 |
|
$default = $this->valueConverter->prettyPrintExpr($object->getDefault()); |
75
|
4 |
|
} |
76
|
4 |
|
$docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context); |
77
|
|
|
|
78
|
4 |
|
return new PropertyDescriptor($object->getFqsen(), $visibility, $docBlock, $default, $object->isStatic()); |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Converts the visibility of the property to a valid Visibility object. |
83
|
|
|
* |
84
|
|
|
* @param PropertyIterator $node |
85
|
|
|
* @return Visibility |
86
|
|
|
*/ |
87
|
4 |
|
private function buildVisibility(PropertyIterator $node) |
88
|
|
|
{ |
89
|
4 |
|
if ($node->isPrivate()) { |
90
|
2 |
|
return new Visibility(Visibility::PRIVATE_); |
91
|
2 |
|
} elseif ($node->isProtected()) { |
92
|
1 |
|
return new Visibility(Visibility::PROTECTED_); |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
return new Visibility(Visibility::PUBLIC_); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
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.