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\Location; |
20
|
|
|
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy; |
21
|
|
|
use phpDocumentor\Reflection\Php\StrategyContainer; |
22
|
|
|
use phpDocumentor\Reflection\Types\Context; |
23
|
|
|
use PhpParser\Comment\Doc; |
24
|
|
|
use PhpParser\Node; |
25
|
|
|
use PhpParser\Node\Stmt\ClassConst; |
26
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
27
|
|
|
use PhpParser\Node\Stmt\Interface_ as InterfaceNode; |
28
|
|
|
use phpDocumentor\Reflection\Php\Interface_ as InterfaceElement; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Strategy to create a InterfaceElement including all sub elements. |
32
|
|
|
*/ |
33
|
|
|
// @codingStandardsIgnoreStart |
34
|
|
|
final class Interface_ extends AbstractFactory implements ProjectFactoryStrategy |
35
|
|
|
// @codingStandardsIgnoreEnd |
36
|
|
|
{ |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns true when the strategy is able to handle the object. |
40
|
|
|
* |
41
|
|
|
* @param InterfaceNode $object object to check. |
42
|
|
|
* @return boolean |
43
|
|
|
*/ |
44
|
1 |
|
public function matches($object) |
45
|
|
|
{ |
46
|
1 |
|
return $object instanceof InterfaceNode; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Creates an Interface_ out of the given object. |
51
|
|
|
* Since an object might contain other objects that need to be converted the $factory is passed so it can be |
52
|
|
|
* used to create nested Elements. |
53
|
|
|
* |
54
|
|
|
* @param InterfaceNode $object object to convert to an Element |
55
|
|
|
* @param StrategyContainer $strategies used to convert nested objects. |
56
|
|
|
* @param Context $context of the created object |
57
|
|
|
* @return InterfaceElement |
58
|
|
|
*/ |
59
|
4 |
|
protected function doCreate($object, StrategyContainer $strategies, Context $context = null) |
60
|
|
|
{ |
61
|
4 |
|
$docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context); |
62
|
4 |
|
$parents = array(); |
63
|
4 |
|
foreach ($object->extends as $extend) { |
64
|
|
|
$parents['\\' . (string)$extend] = new Fqsen('\\' . (string)$extend); |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
$interface = new InterfaceElement($object->fqsen, $parents, $docBlock, new Location($object->getLine())); |
|
|
|
|
68
|
|
|
|
69
|
4 |
|
if (isset($object->stmts)) { |
70
|
2 |
|
foreach ($object->stmts as $stmt) { |
71
|
2 |
|
switch (get_class($stmt)) { |
72
|
|
|
case ClassMethod::class: |
73
|
1 |
|
$method = $this->createMember($stmt, $strategies, $context); |
74
|
1 |
|
$interface->addMethod($method); |
|
|
|
|
75
|
1 |
|
break; |
76
|
|
|
case ClassConst::class: |
77
|
1 |
|
$constants = new ClassConstantIterator($stmt); |
|
|
|
|
78
|
1 |
|
foreach ($constants as $const) { |
79
|
1 |
|
$element = $this->createMember($const, $strategies, $context); |
80
|
1 |
|
$interface->addConstant($element); |
|
|
|
|
81
|
|
|
} |
82
|
2 |
|
break; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
4 |
|
return $interface; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
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.