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\Fqsen; |
17
|
|
|
use phpDocumentor\Reflection\Location; |
18
|
|
|
use phpDocumentor\Reflection\Php\Factory; |
19
|
|
|
use phpDocumentor\Reflection\Php\Function_ as FunctionDescriptor; |
20
|
|
|
use phpDocumentor\Reflection\Php\ProjectFactoryStrategy; |
21
|
|
|
use phpDocumentor\Reflection\Php\StrategyContainer; |
22
|
|
|
use phpDocumentor\Reflection\TypeResolver; |
23
|
|
|
use phpDocumentor\Reflection\Types\Context; |
24
|
|
|
use PhpParser\Comment\Doc; |
25
|
|
|
use PhpParser\Node\NullableType; |
26
|
|
|
use PhpParser\Node\Stmt\Function_ as FunctionNode; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Strategy to convert Function_ to FunctionDescriptor |
30
|
|
|
* |
31
|
|
|
* @see FunctionDescriptor |
32
|
|
|
* @see \PhpParser\Node\ |
33
|
|
|
*/ |
34
|
|
|
// @codingStandardsIgnoreStart |
35
|
|
|
final class Function_ extends AbstractFactory implements ProjectFactoryStrategy |
36
|
|
|
// @codingStandardsIgnoreEnd |
37
|
|
|
{ |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Returns true when the strategy is able to handle the object. |
41
|
|
|
* |
42
|
|
|
* @param FunctionNode $object object to check. |
43
|
|
|
* @return boolean |
44
|
1 |
|
*/ |
45
|
|
|
public function matches($object) |
46
|
1 |
|
{ |
47
|
|
|
return $object instanceof FunctionNode; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Creates an FunctionDescriptor out of the given object including its child elements. |
52
|
|
|
* |
53
|
|
|
* @param \PhpParser\Node\Stmt\Function_ $object object to convert to an Element |
54
|
|
|
* @param StrategyContainer $strategies used to convert nested objects. |
55
|
|
|
* @param Context $context of the created object |
56
|
|
|
* @return FunctionDescriptor |
57
|
3 |
|
*/ |
58
|
|
|
protected function doCreate($object, StrategyContainer $strategies, Context $context = null) |
59
|
3 |
|
{ |
60
|
|
|
$docBlock = $this->createDocBlock($strategies, $object->getDocComment(), $context); |
61
|
3 |
|
|
62
|
3 |
|
$returnType = null; |
63
|
|
|
if ($object->getReturnType() !== null) { |
64
|
|
|
$typeResolver = new TypeResolver(); |
65
|
|
|
if ($object->getReturnType() instanceof NullableType) { |
66
|
|
|
$typeString = '?' . $object->getReturnType()->type; |
67
|
3 |
|
} else { |
68
|
|
|
$typeString = (string)$object->getReturnType(); |
69
|
3 |
|
} |
70
|
1 |
|
$returnType = $typeResolver->resolve($typeString, $context); |
71
|
1 |
|
} |
72
|
|
|
|
73
|
|
|
$function = new FunctionDescriptor($object->fqsen, $docBlock, new Location($object->getLine()), $returnType); |
|
|
|
|
74
|
3 |
|
|
75
|
|
|
foreach ($object->params as $param) { |
76
|
|
|
$strategy = $strategies->findMatching($param); |
77
|
|
|
$function->addArgument($strategy->create($param, $strategies, $context)); |
|
|
|
|
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $function; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
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.