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-2018 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 phpDocumentor\Reflection\Location; |
16
|
|
|
use phpDocumentor\Reflection\Php\Method as MethodDescriptor; |
17
|
|
|
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy; |
18
|
|
|
use phpDocumentor\Reflection\Php\StrategyContainer; |
19
|
|
|
use phpDocumentor\Reflection\Php\Visibility; |
20
|
|
|
use phpDocumentor\Reflection\TypeResolver; |
21
|
|
|
use phpDocumentor\Reflection\Types\Context; |
22
|
|
|
use phpDocumentor\Reflection\Types\Mixed_; |
23
|
|
|
use PhpParser\Node\NullableType; |
24
|
|
|
use PhpParser\Node\Stmt\ClassMethod; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Strategy to create MethodDescriptor and arguments when applicable. |
28
|
|
|
*/ |
29
|
|
|
final class Method extends AbstractFactory implements ProjectFactoryStrategy |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* Returns true when the strategy is able to handle the object. |
33
|
|
|
* |
34
|
|
|
* @param object $object object to check. |
35
|
|
|
* @return boolean |
36
|
|
|
*/ |
37
|
1 |
|
public function matches($object) |
38
|
|
|
{ |
39
|
1 |
|
return $object instanceof ClassMethod; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates an MethodDescriptor out of the given object including its child elements. |
44
|
|
|
* |
45
|
|
|
* @param ClassMethod $object object to convert to an MethodDescriptor |
46
|
|
|
* @param StrategyContainer $strategies used to convert nested objects. |
47
|
|
|
* @param Context $context of the created object |
48
|
|
|
* @return MethodDescriptor |
49
|
|
|
*/ |
50
|
6 |
|
protected function doCreate($object, StrategyContainer $strategies, Context $context = null) |
51
|
|
|
{ |
52
|
6 |
|
$docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context); |
53
|
|
|
|
54
|
6 |
|
$returnType = null; |
55
|
6 |
|
if ($object->getReturnType() !== null) { |
56
|
2 |
|
$typeResolver = new TypeResolver(); |
57
|
2 |
|
if ($object->getReturnType() instanceof NullableType) { |
58
|
1 |
|
$typeString = '?' . $object->getReturnType()->type; |
59
|
|
|
} else { |
60
|
1 |
|
$typeString = (string)$object->getReturnType(); |
61
|
|
|
} |
62
|
2 |
|
$returnType = $typeResolver->resolve($typeString, $context); |
63
|
|
|
} |
64
|
|
|
|
65
|
6 |
|
$method = new MethodDescriptor( |
66
|
6 |
|
$object->fqsen, |
67
|
6 |
|
$this->buildVisibility($object), |
68
|
6 |
|
$docBlock, |
|
|
|
|
69
|
6 |
|
$object->isAbstract(), |
70
|
6 |
|
$object->isStatic(), |
71
|
6 |
|
$object->isFinal(), |
72
|
6 |
|
new Location($object->getLine()), |
73
|
6 |
|
$returnType |
74
|
|
|
); |
75
|
|
|
|
76
|
6 |
|
foreach ($object->params as $param) { |
77
|
1 |
|
$method->addArgument($this->createMember($param, $strategies, $context)); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
6 |
|
return $method; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Converts the visibility of the method to a valid Visibility object. |
85
|
|
|
* |
86
|
|
|
* @param ClassMethod $node |
87
|
|
|
* @return Visibility |
88
|
|
|
*/ |
89
|
6 |
|
private function buildVisibility(ClassMethod $node) |
90
|
|
|
{ |
91
|
6 |
|
if ($node->isPrivate()) { |
92
|
4 |
|
return new Visibility(Visibility::PRIVATE_); |
93
|
2 |
|
} elseif ($node->isProtected()) { |
94
|
1 |
|
return new Visibility(Visibility::PROTECTED_); |
95
|
|
|
} |
96
|
|
|
|
97
|
1 |
|
return new Visibility(Visibility::PUBLIC_); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
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.