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