1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace ApiGen\Parser\Reflection; |
4
|
|
|
|
5
|
|
|
use ApiGen\Contracts\Parser\Reflection\AbstractFunctionMethodReflectionInterface; |
6
|
|
|
use ApiGen\Contracts\Parser\Reflection\ClassReflectionInterface; |
7
|
|
|
use ApiGen\Contracts\Parser\Reflection\ParameterReflectionInterface; |
8
|
|
|
|
9
|
|
|
final class ReflectionParameter extends AbstractReflection implements ParameterReflectionInterface |
10
|
|
|
{ |
11
|
|
|
public function getTypeHint(): string |
12
|
|
|
{ |
13
|
|
|
if ($this->isArray()) { |
14
|
|
|
return 'array'; |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
if ($this->isCallable()) { |
18
|
|
|
return 'callable'; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
$className = $this->getClassName(); |
22
|
|
|
if ($className) { |
23
|
|
|
return $className; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
$annotations = $this->getDeclaringFunction()->getAnnotation('param'); |
27
|
|
|
if ($annotations) { |
|
|
|
|
28
|
|
|
if (! empty($annotations[$this->getPosition()])) { |
29
|
|
|
[$types] = preg_split('~\s+|$~', $annotations[$this->getPosition()], 2); |
|
|
|
|
30
|
|
|
if (! empty($types) && $types[0] !== '$') { |
31
|
|
|
return $types; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
return ''; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getDescription(): string |
40
|
|
|
{ |
41
|
|
|
$annotations = $this->getDeclaringFunction()->getAnnotation('param'); |
42
|
|
|
if (empty($annotations[$this->getPosition()])) { |
43
|
|
|
return ''; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$description = trim(strpbrk($annotations[$this->getPosition()], "\n\r\t ")); |
47
|
|
|
return preg_replace('~^(\\$' . $this->getName() . '(?:,\\.{3})?)(\\s+|$)~i', '\\2', $description, 1); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function getDefaultValueDefinition(): ?string |
51
|
|
|
{ |
52
|
|
|
return $this->reflection === null ? null : $this->reflection->getDefaultValueDefinition(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function isDefaultValueAvailable(): bool |
56
|
|
|
{ |
57
|
|
|
return $this->reflection->isDefaultValueAvailable(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getPosition(): int |
61
|
|
|
{ |
62
|
|
|
return $this->reflection->getPosition(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function isArray(): bool |
66
|
|
|
{ |
67
|
|
|
return $this->reflection->isArray(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function isCallable(): bool |
71
|
|
|
{ |
72
|
|
|
return $this->reflection->isCallable(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function getClass(): ?ClassReflectionInterface |
76
|
|
|
{ |
77
|
|
|
$className = (string) $this->reflection->getClassName(); |
78
|
|
|
return $className === '' ? null : $this->getParsedClasses()[$className]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function getClassName(): ?string |
82
|
|
|
{ |
83
|
|
|
return $this->reflection->getClassName(); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function allowsNull(): bool |
87
|
|
|
{ |
88
|
|
|
return $this->reflection->allowsNull(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function isOptional(): bool |
92
|
|
|
{ |
93
|
|
|
return $this->reflection->isOptional(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function isPassedByReference(): bool |
97
|
|
|
{ |
98
|
|
|
return $this->reflection->isPassedByReference(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function canBePassedByValue(): bool |
102
|
|
|
{ |
103
|
|
|
return $this->reflection->canBePassedByValue(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function getDeclaringFunction(): AbstractFunctionMethodReflectionInterface |
107
|
|
|
{ |
108
|
|
|
$functionName = $this->reflection->getDeclaringFunctionName(); |
109
|
|
|
|
110
|
|
|
$className = $this->reflection->getDeclaringClassName(); |
111
|
|
|
|
112
|
|
|
if ($className) { |
113
|
|
|
return $this->getParsedClasses()[$className]->getMethod($functionName); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return $this->parserStorage->getFunctions()[$functionName]; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
public function getDeclaringFunctionName(): string |
120
|
|
|
{ |
121
|
|
|
return (string) $this->reflection->getDeclaringFunctionName(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getDeclaringClass(): ?ClassReflectionInterface |
125
|
|
|
{ |
126
|
|
|
$className = $this->reflection->getDeclaringClassName(); |
127
|
|
|
return $className === '' ? null : $this->getParsedClasses()[$className]; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
public function getDeclaringClassName(): string |
131
|
|
|
{ |
132
|
|
|
return (string) $this->reflection->getDeclaringClassName(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function isUnlimited(): bool |
136
|
|
|
{ |
137
|
|
|
return false; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.