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