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\ProjectFactoryStrategy; |
19
|
|
|
use phpDocumentor\Reflection\Php\Property as PropertyDescriptor; |
20
|
|
|
use phpDocumentor\Reflection\Php\StrategyContainer; |
21
|
|
|
use phpDocumentor\Reflection\Php\Visibility; |
22
|
|
|
use phpDocumentor\Reflection\PrettyPrinter; |
23
|
|
|
use phpDocumentor\Reflection\Types\Context; |
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
|
6 |
|
public function __construct(PrettyPrinter $prettyPrinter) |
42
|
|
|
{ |
43
|
6 |
|
$this->valueConverter = $prettyPrinter; |
44
|
6 |
|
} |
45
|
|
|
|
46
|
1 |
|
public function matches($object): bool |
47
|
|
|
{ |
48
|
1 |
|
return $object instanceof PropertyIterator; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Creates an PropertyDescriptor out of the given object. |
53
|
|
|
* |
54
|
|
|
* Since an object might contain other objects that need to be converted the $factory is passed so it can be |
55
|
|
|
* used to create nested Elements. |
56
|
|
|
* |
57
|
|
|
* @param PropertyIterator $object object to convert to an PropertyDescriptor |
58
|
|
|
* @param StrategyContainer $strategies used to convert nested objects. |
59
|
|
|
* @return PropertyDescriptor |
60
|
|
|
*/ |
61
|
4 |
|
protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null) |
62
|
|
|
{ |
63
|
4 |
|
$visibility = $this->buildVisibility($object); |
64
|
4 |
|
$default = null; |
65
|
4 |
|
if ($object->getDefault() !== null) { |
66
|
4 |
|
$default = $this->valueConverter->prettyPrintExpr($object->getDefault()); |
|
|
|
|
67
|
|
|
} |
68
|
|
|
|
69
|
4 |
|
$docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context); |
70
|
|
|
|
71
|
4 |
|
return new PropertyDescriptor( |
72
|
4 |
|
$object->getFqsen(), |
73
|
4 |
|
$visibility, |
74
|
4 |
|
$docBlock, |
|
|
|
|
75
|
4 |
|
$default, |
76
|
4 |
|
$object->isStatic(), |
77
|
4 |
|
new Location($object->getLine()) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Converts the visibility of the property to a valid Visibility object. |
83
|
|
|
*/ |
84
|
4 |
|
private function buildVisibility(PropertyIterator $node): Visibility |
85
|
|
|
{ |
86
|
4 |
|
if ($node->isPrivate()) { |
87
|
2 |
|
return new Visibility(Visibility::PRIVATE_); |
88
|
2 |
|
} elseif ($node->isProtected()) { |
89
|
1 |
|
return new Visibility(Visibility::PROTECTED_); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return new Visibility(Visibility::PUBLIC_); |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
This check looks at variables that are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.