1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Atreyu; |
4
|
|
|
|
5
|
|
|
use phpDocumentor\Reflection\DocBlock; |
6
|
|
|
|
7
|
|
|
abstract class BaseReflector |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param array $docBlockParams |
11
|
|
|
* @param \ReflectionParameter $param |
12
|
|
|
* @param array $arguments |
13
|
|
|
* |
14
|
|
|
* @return bool|string |
15
|
|
|
*/ |
16
|
|
|
public function getParamDocBlockHint(array $docBlockParams, \ReflectionParameter $param, array $arguments = []) |
17
|
|
|
{ |
18
|
|
|
$typeHint = false; |
19
|
|
|
|
20
|
|
|
/** @var DocBlock\Tag\ParamTag $docBlockParam */ |
21
|
|
|
foreach ($docBlockParams as $docBlockParam) { |
22
|
|
|
|
23
|
|
|
if (!($docBlockParam instanceof DocBlock\Tag\ParamTag)) { |
24
|
|
|
continue; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
$type = $docBlockParam->getType(); |
28
|
|
|
$docBlockParamName = $docBlockParam->getVariableName(); |
29
|
|
|
|
30
|
|
|
if (($param->getName() === ltrim($docBlockParamName, '$')) |
31
|
|
|
&& (!empty($type)) |
32
|
|
|
) { |
33
|
|
|
$definitions = explode('|', $type); |
34
|
|
|
|
35
|
|
|
foreach ($arguments as $key => $argument) { |
36
|
|
|
|
37
|
|
|
foreach ($definitions as $definition) { |
38
|
|
|
|
39
|
|
|
if (is_object($argument) |
40
|
|
|
&& in_array(ltrim($definition, '\\'), $this->getImplemented(get_class($argument))) |
41
|
|
|
&& (is_numeric($key) || (ltrim($docBlockParamName, '$') === $key) |
42
|
|
|
)) { |
43
|
|
|
$typeHint = $definition; |
44
|
|
|
|
45
|
|
|
// no need to loop again, since we found a match already! |
46
|
|
|
continue 3; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
if ($typeHint === false) { |
52
|
|
|
|
53
|
|
|
// use first definition, there is no way to know which instance of the hinted doc block definitions is actually required |
54
|
|
|
// because there were either no arguments given or no argument match was found |
55
|
|
|
list($firstDefinition,) = $definitions; |
56
|
|
|
|
57
|
|
|
if (!in_array(strtolower($firstDefinition), ['int', 'float', 'bool', 'string', 'array', 'null', 'mixed'])) { |
58
|
|
|
|
59
|
|
|
$typeHint = $firstDefinition; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $typeHint; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param $className |
70
|
|
|
* |
71
|
|
|
* @return array |
72
|
|
|
*/ |
73
|
|
|
public function getImplemented($className) |
74
|
|
|
{ |
75
|
|
|
return array_merge([$className], class_implements($className)); |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|