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 InvalidArgumentException; |
17
|
|
|
use phpDocumentor\Reflection\Element; |
18
|
|
|
use phpDocumentor\Reflection\Fqsen; |
19
|
|
|
use phpDocumentor\Reflection\Php\Class_ as ClassElement; |
20
|
|
|
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy; |
21
|
|
|
use phpDocumentor\Reflection\Php\StrategyContainer; |
22
|
|
|
use phpDocumentor\Reflection\Types\Context; |
23
|
|
|
use PhpParser\Node; |
24
|
|
|
use PhpParser\Node\Stmt\Class_ as ClassNode; |
25
|
|
|
use PhpParser\Node\Stmt\ClassConst; |
26
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
27
|
|
|
use PhpParser\Node\Stmt\Property as PropertyNode; |
28
|
|
|
use PhpParser\Comment\Doc; |
29
|
|
|
use PhpParser\Node\Stmt\TraitUse; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Strategy to create a ClassElement including all sub elements. |
33
|
|
|
*/ |
34
|
|
|
final class Class_ implements ProjectFactoryStrategy |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Returns true when the strategy is able to handle the object. |
39
|
|
|
* |
40
|
|
|
* @param object $object object to check. |
41
|
|
|
* @return boolean |
42
|
|
|
*/ |
43
|
1 |
|
public function matches($object) |
44
|
|
|
{ |
45
|
1 |
|
return $object instanceof ClassNode; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Creates an ClassElement out of the given object. |
50
|
|
|
* Since an object might contain other objects that need to be converted the $factory is passed so it can be |
51
|
|
|
* used to create nested Elements. |
52
|
|
|
* |
53
|
|
|
* @param ClassNode $object object to convert to an Element |
54
|
|
|
* @param StrategyContainer $strategies used to convert nested objects. |
55
|
|
|
* @param Context $context of the created object |
56
|
|
|
* @return ClassElement |
57
|
|
|
*/ |
58
|
9 |
|
public function create($object, StrategyContainer $strategies, Context $context = null) |
|
|
|
|
59
|
|
|
{ |
60
|
9 |
|
if (!$this->matches($object)) { |
61
|
1 |
|
throw new InvalidArgumentException( |
62
|
1 |
|
sprintf('%s cannot handle objects with the type %s', |
63
|
1 |
|
__CLASS__, |
64
|
1 |
|
is_object($object) ? get_class($object) : gettype($object) |
65
|
|
|
) |
66
|
|
|
); |
67
|
|
|
} |
68
|
|
|
|
69
|
8 |
|
$docBlock = $this->createDocBlock($object->getDocComment(), $strategies, $context); |
70
|
|
|
|
71
|
8 |
|
$classElement = new ClassElement( |
72
|
8 |
|
$object->fqsen, |
|
|
|
|
73
|
|
|
$docBlock, |
|
|
|
|
74
|
8 |
|
$object->extends ? new Fqsen('\\' . $object->extends) : null, |
75
|
8 |
|
$object->isAbstract(), |
76
|
8 |
|
$object->isFinal() |
77
|
|
|
); |
78
|
|
|
|
79
|
8 |
|
if (isset($object->implements)) { |
80
|
1 |
|
foreach ($object->implements as $interfaceClassName) { |
81
|
1 |
|
$classElement->addInterface( |
82
|
1 |
|
new Fqsen('\\' . $interfaceClassName->toString()) |
83
|
|
|
); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
8 |
|
if (isset($object->stmts)) { |
88
|
4 |
|
foreach ($object->stmts as $stmt) { |
89
|
4 |
|
switch (get_class($stmt)) { |
90
|
|
|
case TraitUse::class: |
91
|
1 |
|
foreach ($stmt->traits as $use) { |
|
|
|
|
92
|
1 |
|
$classElement->addUsedTrait(new Fqsen('\\'. $use->toString())); |
93
|
|
|
} |
94
|
1 |
|
break; |
95
|
|
|
case PropertyNode::class: |
96
|
1 |
|
$properties = new PropertyIterator($stmt); |
|
|
|
|
97
|
1 |
|
foreach ($properties as $property) { |
98
|
1 |
|
$element = $this->createMember($property, $strategies, $context); |
99
|
1 |
|
$classElement->addProperty($element); |
|
|
|
|
100
|
|
|
} |
101
|
1 |
|
break; |
102
|
|
|
case ClassMethod::class: |
103
|
1 |
|
$method = $this->createMember($stmt, $strategies, $context); |
104
|
1 |
|
$classElement->addMethod($method); |
|
|
|
|
105
|
1 |
|
break; |
106
|
|
|
case ClassConst::class: |
107
|
1 |
|
$constants = new ClassConstantIterator($stmt); |
|
|
|
|
108
|
1 |
|
foreach ($constants as $const) { |
109
|
1 |
|
$element = $this->createMember($const, $strategies, $context); |
110
|
1 |
|
$classElement->addConstant($element); |
|
|
|
|
111
|
|
|
} |
112
|
4 |
|
break; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
8 |
|
return $classElement; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param Node|PropertyIterator|ClassConstantIterator $stmt |
122
|
|
|
* @param StrategyContainer $strategies |
123
|
|
|
* @param Context $context |
124
|
|
|
* @return Element |
125
|
|
|
*/ |
126
|
4 |
|
private function createMember($stmt, StrategyContainer $strategies, Context $context = null) |
127
|
|
|
{ |
128
|
4 |
|
$strategy = $strategies->findMatching($stmt); |
129
|
4 |
|
return $strategy->create($stmt, $strategies, $context); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* @param Doc $docBlock |
135
|
|
|
* @param StrategyContainer $strategies |
136
|
|
|
* @param Context $context |
137
|
|
|
* @return null|\phpDocumentor\Reflection\DocBlock |
138
|
|
|
*/ |
139
|
8 |
|
private function createDocBlock(Doc $docBlock = null, StrategyContainer $strategies, Context $context = null) |
|
|
|
|
140
|
|
|
{ |
141
|
8 |
|
if ($docBlock === null) { |
142
|
7 |
|
return null; |
143
|
|
|
} |
144
|
|
|
|
145
|
1 |
|
return $this->createMember($docBlock, $strategies, $context); |
|
|
|
|
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|