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