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