|
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\Fqsen; |
|
18
|
|
|
use phpDocumentor\Reflection\Location; |
|
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\Stmt\Class_ as ClassNode; |
|
24
|
|
|
use PhpParser\Node\Stmt\ClassConst; |
|
25
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
|
26
|
|
|
use PhpParser\Node\Stmt\Property as PropertyNode; |
|
27
|
|
|
use PhpParser\Node\Stmt\TraitUse; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Strategy to create a ClassElement including all sub elements. |
|
31
|
|
|
*/ |
|
32
|
|
|
// @codingStandardsIgnoreStart |
|
33
|
|
|
final class Class_ extends AbstractFactory implements ProjectFactoryStrategy |
|
34
|
|
|
// @codingStandardsIgnoreEnd |
|
35
|
|
|
{ |
|
36
|
1 |
|
public function matches($object): bool |
|
37
|
|
|
{ |
|
38
|
1 |
|
return $object instanceof ClassNode; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Creates an ClassElement out of the given object. |
|
43
|
|
|
* Since an object might contain other objects that need to be converted the $factory is passed so it can be |
|
44
|
|
|
* used to create nested Elements. |
|
45
|
|
|
* |
|
46
|
|
|
* @param ClassNode $object object to convert to an Element |
|
47
|
|
|
* @param StrategyContainer $strategies used to convert nested objects. |
|
48
|
|
|
* @param Context $context of the created object |
|
49
|
|
|
* @return ClassElement |
|
50
|
|
|
*/ |
|
51
|
8 |
|
protected function doCreate($object, StrategyContainer $strategies, ?Context $context = null) |
|
52
|
|
|
{ |
|
53
|
8 |
|
$docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context); |
|
54
|
|
|
|
|
55
|
8 |
|
$classElement = new ClassElement( |
|
56
|
8 |
|
$object->fqsen, |
|
57
|
8 |
|
$docBlock, |
|
58
|
8 |
|
$object->extends ? new Fqsen('\\' . $object->extends) : null, |
|
59
|
8 |
|
$object->isAbstract(), |
|
60
|
8 |
|
$object->isFinal(), |
|
61
|
8 |
|
new Location($object->getLine()) |
|
62
|
|
|
); |
|
63
|
|
|
|
|
64
|
8 |
|
if (isset($object->implements)) { |
|
65
|
1 |
|
foreach ($object->implements as $interfaceClassName) { |
|
66
|
1 |
|
$classElement->addInterface( |
|
67
|
1 |
|
new Fqsen('\\' . $interfaceClassName->toString()) |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
8 |
|
if (isset($object->stmts)) { |
|
73
|
4 |
|
foreach ($object->stmts as $stmt) { |
|
74
|
4 |
|
switch (get_class($stmt)) { |
|
75
|
4 |
|
case TraitUse::class: |
|
76
|
1 |
|
foreach ($stmt->traits as $use) { |
|
77
|
1 |
|
$classElement->addUsedTrait(new Fqsen('\\' . $use->toString())); |
|
78
|
|
|
} |
|
79
|
1 |
|
break; |
|
80
|
3 |
|
case PropertyNode::class: |
|
81
|
1 |
|
$properties = new PropertyIterator($stmt); |
|
82
|
1 |
|
foreach ($properties as $property) { |
|
83
|
1 |
|
$element = $this->createMember($property, $strategies, $context); |
|
84
|
1 |
|
$classElement->addProperty($element); |
|
|
|
|
|
|
85
|
|
|
} |
|
86
|
1 |
|
break; |
|
87
|
2 |
|
case ClassMethod::class: |
|
88
|
1 |
|
$method = $this->createMember($stmt, $strategies, $context); |
|
89
|
1 |
|
$classElement->addMethod($method); |
|
|
|
|
|
|
90
|
1 |
|
break; |
|
91
|
1 |
|
case ClassConst::class: |
|
92
|
1 |
|
$constants = new ClassConstantIterator($stmt); |
|
93
|
1 |
|
foreach ($constants as $const) { |
|
94
|
1 |
|
$element = $this->createMember($const, $strategies, $context); |
|
95
|
1 |
|
$classElement->addConstant($element); |
|
|
|
|
|
|
96
|
|
|
} |
|
97
|
4 |
|
break; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
8 |
|
return $classElement; |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.