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