@@ -20,220 +20,220 @@ |
||
20 | 20 | */ |
21 | 21 | class Argument |
22 | 22 | { |
23 | - /** |
|
24 | - * Checks that argument is exact value or object. |
|
25 | - * |
|
26 | - * @param mixed $value |
|
27 | - * |
|
28 | - * @return Token\ExactValueToken |
|
29 | - */ |
|
30 | - public static function exact($value) |
|
31 | - { |
|
32 | - return new Token\ExactValueToken($value); |
|
33 | - } |
|
34 | - |
|
35 | - /** |
|
36 | - * Checks that argument is of specific type or instance of specific class. |
|
37 | - * |
|
38 | - * @param string $type Type name (`integer`, `string`) or full class name |
|
39 | - * |
|
40 | - * @return Token\TypeToken |
|
41 | - */ |
|
42 | - public static function type($type) |
|
43 | - { |
|
44 | - return new Token\TypeToken($type); |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * Checks that argument object has specific state. |
|
49 | - * |
|
50 | - * @param string $methodName |
|
51 | - * @param mixed $value |
|
52 | - * |
|
53 | - * @return Token\ObjectStateToken |
|
54 | - */ |
|
55 | - public static function which($methodName, $value) |
|
56 | - { |
|
57 | - return new Token\ObjectStateToken($methodName, $value); |
|
58 | - } |
|
59 | - |
|
60 | - /** |
|
61 | - * Checks that argument matches provided callback. |
|
62 | - * |
|
63 | - * @param callable $callback |
|
64 | - * |
|
65 | - * @return Token\CallbackToken |
|
66 | - */ |
|
67 | - public static function that($callback) |
|
68 | - { |
|
69 | - return new Token\CallbackToken($callback); |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Matches any single value. |
|
74 | - * |
|
75 | - * @return Token\AnyValueToken |
|
76 | - */ |
|
77 | - public static function any() |
|
78 | - { |
|
79 | - return new Token\AnyValueToken; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Matches all values to the rest of the signature. |
|
84 | - * |
|
85 | - * @return Token\AnyValuesToken |
|
86 | - */ |
|
87 | - public static function cetera() |
|
88 | - { |
|
89 | - return new Token\AnyValuesToken; |
|
90 | - } |
|
91 | - |
|
92 | - /** |
|
93 | - * Checks that argument matches all tokens |
|
94 | - * |
|
95 | - * @param mixed ... a list of tokens |
|
96 | - * |
|
97 | - * @return Token\LogicalAndToken |
|
98 | - */ |
|
99 | - public static function allOf() |
|
100 | - { |
|
101 | - return new Token\LogicalAndToken(func_get_args()); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Checks that argument array or countable object has exact number of elements. |
|
106 | - * |
|
107 | - * @param integer $value array elements count |
|
108 | - * |
|
109 | - * @return Token\ArrayCountToken |
|
110 | - */ |
|
111 | - public static function size($value) |
|
112 | - { |
|
113 | - return new Token\ArrayCountToken($value); |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Checks that argument array contains (key, value) pair |
|
118 | - * |
|
119 | - * @param mixed $key exact value or token |
|
120 | - * @param mixed $value exact value or token |
|
121 | - * |
|
122 | - * @return Token\ArrayEntryToken |
|
123 | - */ |
|
124 | - public static function withEntry($key, $value) |
|
125 | - { |
|
126 | - return new Token\ArrayEntryToken($key, $value); |
|
127 | - } |
|
128 | - |
|
129 | - /** |
|
130 | - * Checks that arguments array entries all match value |
|
131 | - * |
|
132 | - * @param mixed $value |
|
133 | - * |
|
134 | - * @return Token\ArrayEveryEntryToken |
|
135 | - */ |
|
136 | - public static function withEveryEntry($value) |
|
137 | - { |
|
138 | - return new Token\ArrayEveryEntryToken($value); |
|
139 | - } |
|
140 | - |
|
141 | - /** |
|
142 | - * Checks that argument array contains value |
|
143 | - * |
|
144 | - * @param mixed $value |
|
145 | - * |
|
146 | - * @return Token\ArrayEntryToken |
|
147 | - */ |
|
148 | - public static function containing($value) |
|
149 | - { |
|
150 | - return new Token\ArrayEntryToken(self::any(), $value); |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Checks that argument array has key |
|
155 | - * |
|
156 | - * @param mixed $key exact value or token |
|
157 | - * |
|
158 | - * @return Token\ArrayEntryToken |
|
159 | - */ |
|
160 | - public static function withKey($key) |
|
161 | - { |
|
162 | - return new Token\ArrayEntryToken($key, self::any()); |
|
163 | - } |
|
164 | - |
|
165 | - /** |
|
166 | - * Checks that argument does not match the value|token. |
|
167 | - * |
|
168 | - * @param mixed $value either exact value or argument token |
|
169 | - * |
|
170 | - * @return Token\LogicalNotToken |
|
171 | - */ |
|
172 | - public static function not($value) |
|
173 | - { |
|
174 | - return new Token\LogicalNotToken($value); |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * @param string $value |
|
179 | - * |
|
180 | - * @return Token\StringContainsToken |
|
181 | - */ |
|
182 | - public static function containingString($value) |
|
183 | - { |
|
184 | - return new Token\StringContainsToken($value); |
|
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Checks that argument is identical value. |
|
189 | - * |
|
190 | - * @param mixed $value |
|
191 | - * |
|
192 | - * @return Token\IdenticalValueToken |
|
193 | - */ |
|
194 | - public static function is($value) |
|
195 | - { |
|
196 | - return new Token\IdenticalValueToken($value); |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * Check that argument is same value when rounding to the |
|
201 | - * given precision. |
|
202 | - * |
|
203 | - * @param float $value |
|
204 | - * @param float $precision |
|
205 | - * |
|
206 | - * @return Token\ApproximateValueToken |
|
207 | - */ |
|
208 | - public static function approximate($value, $precision = 0) |
|
209 | - { |
|
210 | - return new Token\ApproximateValueToken($value, $precision); |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * Checks that argument is in array. |
|
215 | - * |
|
216 | - * @param array $value |
|
217 | - * |
|
218 | - * @return Token\InArrayToken |
|
219 | - */ |
|
220 | - |
|
221 | - public static function in($value) |
|
222 | - { |
|
223 | - return new Token\InArrayToken($value); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * Checks that argument is not in array. |
|
228 | - * |
|
229 | - * @param array $value |
|
230 | - * |
|
231 | - * @return Token\NotInArrayToken |
|
232 | - */ |
|
233 | - |
|
234 | - public static function notIn($value) |
|
235 | - { |
|
236 | - return new Token\NotInArrayToken($value); |
|
237 | - } |
|
23 | + /** |
|
24 | + * Checks that argument is exact value or object. |
|
25 | + * |
|
26 | + * @param mixed $value |
|
27 | + * |
|
28 | + * @return Token\ExactValueToken |
|
29 | + */ |
|
30 | + public static function exact($value) |
|
31 | + { |
|
32 | + return new Token\ExactValueToken($value); |
|
33 | + } |
|
34 | + |
|
35 | + /** |
|
36 | + * Checks that argument is of specific type or instance of specific class. |
|
37 | + * |
|
38 | + * @param string $type Type name (`integer`, `string`) or full class name |
|
39 | + * |
|
40 | + * @return Token\TypeToken |
|
41 | + */ |
|
42 | + public static function type($type) |
|
43 | + { |
|
44 | + return new Token\TypeToken($type); |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * Checks that argument object has specific state. |
|
49 | + * |
|
50 | + * @param string $methodName |
|
51 | + * @param mixed $value |
|
52 | + * |
|
53 | + * @return Token\ObjectStateToken |
|
54 | + */ |
|
55 | + public static function which($methodName, $value) |
|
56 | + { |
|
57 | + return new Token\ObjectStateToken($methodName, $value); |
|
58 | + } |
|
59 | + |
|
60 | + /** |
|
61 | + * Checks that argument matches provided callback. |
|
62 | + * |
|
63 | + * @param callable $callback |
|
64 | + * |
|
65 | + * @return Token\CallbackToken |
|
66 | + */ |
|
67 | + public static function that($callback) |
|
68 | + { |
|
69 | + return new Token\CallbackToken($callback); |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Matches any single value. |
|
74 | + * |
|
75 | + * @return Token\AnyValueToken |
|
76 | + */ |
|
77 | + public static function any() |
|
78 | + { |
|
79 | + return new Token\AnyValueToken; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Matches all values to the rest of the signature. |
|
84 | + * |
|
85 | + * @return Token\AnyValuesToken |
|
86 | + */ |
|
87 | + public static function cetera() |
|
88 | + { |
|
89 | + return new Token\AnyValuesToken; |
|
90 | + } |
|
91 | + |
|
92 | + /** |
|
93 | + * Checks that argument matches all tokens |
|
94 | + * |
|
95 | + * @param mixed ... a list of tokens |
|
96 | + * |
|
97 | + * @return Token\LogicalAndToken |
|
98 | + */ |
|
99 | + public static function allOf() |
|
100 | + { |
|
101 | + return new Token\LogicalAndToken(func_get_args()); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Checks that argument array or countable object has exact number of elements. |
|
106 | + * |
|
107 | + * @param integer $value array elements count |
|
108 | + * |
|
109 | + * @return Token\ArrayCountToken |
|
110 | + */ |
|
111 | + public static function size($value) |
|
112 | + { |
|
113 | + return new Token\ArrayCountToken($value); |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Checks that argument array contains (key, value) pair |
|
118 | + * |
|
119 | + * @param mixed $key exact value or token |
|
120 | + * @param mixed $value exact value or token |
|
121 | + * |
|
122 | + * @return Token\ArrayEntryToken |
|
123 | + */ |
|
124 | + public static function withEntry($key, $value) |
|
125 | + { |
|
126 | + return new Token\ArrayEntryToken($key, $value); |
|
127 | + } |
|
128 | + |
|
129 | + /** |
|
130 | + * Checks that arguments array entries all match value |
|
131 | + * |
|
132 | + * @param mixed $value |
|
133 | + * |
|
134 | + * @return Token\ArrayEveryEntryToken |
|
135 | + */ |
|
136 | + public static function withEveryEntry($value) |
|
137 | + { |
|
138 | + return new Token\ArrayEveryEntryToken($value); |
|
139 | + } |
|
140 | + |
|
141 | + /** |
|
142 | + * Checks that argument array contains value |
|
143 | + * |
|
144 | + * @param mixed $value |
|
145 | + * |
|
146 | + * @return Token\ArrayEntryToken |
|
147 | + */ |
|
148 | + public static function containing($value) |
|
149 | + { |
|
150 | + return new Token\ArrayEntryToken(self::any(), $value); |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Checks that argument array has key |
|
155 | + * |
|
156 | + * @param mixed $key exact value or token |
|
157 | + * |
|
158 | + * @return Token\ArrayEntryToken |
|
159 | + */ |
|
160 | + public static function withKey($key) |
|
161 | + { |
|
162 | + return new Token\ArrayEntryToken($key, self::any()); |
|
163 | + } |
|
164 | + |
|
165 | + /** |
|
166 | + * Checks that argument does not match the value|token. |
|
167 | + * |
|
168 | + * @param mixed $value either exact value or argument token |
|
169 | + * |
|
170 | + * @return Token\LogicalNotToken |
|
171 | + */ |
|
172 | + public static function not($value) |
|
173 | + { |
|
174 | + return new Token\LogicalNotToken($value); |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * @param string $value |
|
179 | + * |
|
180 | + * @return Token\StringContainsToken |
|
181 | + */ |
|
182 | + public static function containingString($value) |
|
183 | + { |
|
184 | + return new Token\StringContainsToken($value); |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Checks that argument is identical value. |
|
189 | + * |
|
190 | + * @param mixed $value |
|
191 | + * |
|
192 | + * @return Token\IdenticalValueToken |
|
193 | + */ |
|
194 | + public static function is($value) |
|
195 | + { |
|
196 | + return new Token\IdenticalValueToken($value); |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * Check that argument is same value when rounding to the |
|
201 | + * given precision. |
|
202 | + * |
|
203 | + * @param float $value |
|
204 | + * @param float $precision |
|
205 | + * |
|
206 | + * @return Token\ApproximateValueToken |
|
207 | + */ |
|
208 | + public static function approximate($value, $precision = 0) |
|
209 | + { |
|
210 | + return new Token\ApproximateValueToken($value, $precision); |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * Checks that argument is in array. |
|
215 | + * |
|
216 | + * @param array $value |
|
217 | + * |
|
218 | + * @return Token\InArrayToken |
|
219 | + */ |
|
220 | + |
|
221 | + public static function in($value) |
|
222 | + { |
|
223 | + return new Token\InArrayToken($value); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * Checks that argument is not in array. |
|
228 | + * |
|
229 | + * @param array $value |
|
230 | + * |
|
231 | + * @return Token\NotInArrayToken |
|
232 | + */ |
|
233 | + |
|
234 | + public static function notIn($value) |
|
235 | + { |
|
236 | + return new Token\NotInArrayToken($value); |
|
237 | + } |
|
238 | 238 | |
239 | 239 | } |
@@ -21,190 +21,190 @@ |
||
21 | 21 | */ |
22 | 22 | class MethodNode |
23 | 23 | { |
24 | - private $name; |
|
25 | - private $code; |
|
26 | - private $visibility = 'public'; |
|
27 | - private $static = false; |
|
28 | - private $returnsReference = false; |
|
29 | - |
|
30 | - /** @var ReturnTypeNode */ |
|
31 | - private $returnTypeNode; |
|
32 | - |
|
33 | - /** |
|
34 | - * @var ArgumentNode[] |
|
35 | - */ |
|
36 | - private $arguments = array(); |
|
37 | - |
|
38 | - /** |
|
39 | - * @param string $name |
|
40 | - * @param string $code |
|
41 | - */ |
|
42 | - public function __construct($name, $code = null, TypeHintReference $typeHintReference = null) |
|
43 | - { |
|
44 | - $this->name = $name; |
|
45 | - $this->code = $code; |
|
46 | - $this->returnTypeNode = new ReturnTypeNode(); |
|
47 | - } |
|
48 | - |
|
49 | - public function getVisibility() |
|
50 | - { |
|
51 | - return $this->visibility; |
|
52 | - } |
|
53 | - |
|
54 | - /** |
|
55 | - * @param string $visibility |
|
56 | - */ |
|
57 | - public function setVisibility($visibility) |
|
58 | - { |
|
59 | - $visibility = strtolower($visibility); |
|
60 | - |
|
61 | - if (!in_array($visibility, array('public', 'private', 'protected'))) { |
|
62 | - throw new InvalidArgumentException(sprintf( |
|
63 | - '`%s` method visibility is not supported.', $visibility |
|
64 | - )); |
|
65 | - } |
|
66 | - |
|
67 | - $this->visibility = $visibility; |
|
68 | - } |
|
69 | - |
|
70 | - public function isStatic() |
|
71 | - { |
|
72 | - return $this->static; |
|
73 | - } |
|
74 | - |
|
75 | - public function setStatic($static = true) |
|
76 | - { |
|
77 | - $this->static = (bool) $static; |
|
78 | - } |
|
79 | - |
|
80 | - public function returnsReference() |
|
81 | - { |
|
82 | - return $this->returnsReference; |
|
83 | - } |
|
84 | - |
|
85 | - public function setReturnsReference() |
|
86 | - { |
|
87 | - $this->returnsReference = true; |
|
88 | - } |
|
89 | - |
|
90 | - public function getName() |
|
91 | - { |
|
92 | - return $this->name; |
|
93 | - } |
|
94 | - |
|
95 | - public function addArgument(ArgumentNode $argument) |
|
96 | - { |
|
97 | - $this->arguments[] = $argument; |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * @return ArgumentNode[] |
|
102 | - */ |
|
103 | - public function getArguments() |
|
104 | - { |
|
105 | - return $this->arguments; |
|
106 | - } |
|
107 | - |
|
108 | - /** |
|
109 | - * @deprecated use getReturnTypeNode instead |
|
110 | - * @return bool |
|
111 | - */ |
|
112 | - public function hasReturnType() |
|
113 | - { |
|
114 | - return (bool) $this->returnTypeNode->getNonNullTypes(); |
|
115 | - } |
|
116 | - |
|
117 | - public function setReturnTypeNode(ReturnTypeNode $returnTypeNode): void |
|
118 | - { |
|
119 | - $this->returnTypeNode = $returnTypeNode; |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @deprecated use setReturnTypeNode instead |
|
124 | - * @param string $type |
|
125 | - */ |
|
126 | - public function setReturnType($type = null) |
|
127 | - { |
|
128 | - $this->returnTypeNode = ($type === '' || $type === null) ? new ReturnTypeNode() : new ReturnTypeNode($type); |
|
129 | - } |
|
130 | - |
|
131 | - /** |
|
132 | - * @deprecated use setReturnTypeNode instead |
|
133 | - * @param bool $bool |
|
134 | - */ |
|
135 | - public function setNullableReturnType($bool = true) |
|
136 | - { |
|
137 | - if ($bool) { |
|
138 | - $this->returnTypeNode = new ReturnTypeNode('null', ...$this->returnTypeNode->getTypes()); |
|
139 | - } |
|
140 | - else { |
|
141 | - $this->returnTypeNode = new ReturnTypeNode(...$this->returnTypeNode->getNonNullTypes()); |
|
142 | - } |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @deprecated use getReturnTypeNode instead |
|
147 | - * @return string|null |
|
148 | - */ |
|
149 | - public function getReturnType() |
|
150 | - { |
|
151 | - if ($types = $this->returnTypeNode->getNonNullTypes()) |
|
152 | - { |
|
153 | - return $types[0]; |
|
154 | - } |
|
155 | - |
|
156 | - return null; |
|
157 | - } |
|
158 | - |
|
159 | - public function getReturnTypeNode() : ReturnTypeNode |
|
160 | - { |
|
161 | - return $this->returnTypeNode; |
|
162 | - } |
|
163 | - |
|
164 | - /** |
|
165 | - * @deprecated use getReturnTypeNode instead |
|
166 | - * @return bool |
|
167 | - */ |
|
168 | - public function hasNullableReturnType() |
|
169 | - { |
|
170 | - return $this->returnTypeNode->canUseNullShorthand(); |
|
171 | - } |
|
172 | - |
|
173 | - /** |
|
174 | - * @param string $code |
|
175 | - */ |
|
176 | - public function setCode($code) |
|
177 | - { |
|
178 | - $this->code = $code; |
|
179 | - } |
|
180 | - |
|
181 | - public function getCode() |
|
182 | - { |
|
183 | - if ($this->returnsReference) |
|
184 | - { |
|
185 | - return "throw new \Prophecy\Exception\Doubler\ReturnByReferenceException('Returning by reference not supported', get_class(\$this), '{$this->name}');"; |
|
186 | - } |
|
187 | - |
|
188 | - return (string) $this->code; |
|
189 | - } |
|
190 | - |
|
191 | - public function useParentCode() |
|
192 | - { |
|
193 | - $this->code = sprintf( |
|
194 | - 'return parent::%s(%s);', $this->getName(), implode(', ', |
|
195 | - array_map(array($this, 'generateArgument'), $this->arguments) |
|
196 | - ) |
|
197 | - ); |
|
198 | - } |
|
199 | - |
|
200 | - private function generateArgument(ArgumentNode $arg) |
|
201 | - { |
|
202 | - $argument = '$'.$arg->getName(); |
|
203 | - |
|
204 | - if ($arg->isVariadic()) { |
|
205 | - $argument = '...'.$argument; |
|
206 | - } |
|
207 | - |
|
208 | - return $argument; |
|
209 | - } |
|
24 | + private $name; |
|
25 | + private $code; |
|
26 | + private $visibility = 'public'; |
|
27 | + private $static = false; |
|
28 | + private $returnsReference = false; |
|
29 | + |
|
30 | + /** @var ReturnTypeNode */ |
|
31 | + private $returnTypeNode; |
|
32 | + |
|
33 | + /** |
|
34 | + * @var ArgumentNode[] |
|
35 | + */ |
|
36 | + private $arguments = array(); |
|
37 | + |
|
38 | + /** |
|
39 | + * @param string $name |
|
40 | + * @param string $code |
|
41 | + */ |
|
42 | + public function __construct($name, $code = null, TypeHintReference $typeHintReference = null) |
|
43 | + { |
|
44 | + $this->name = $name; |
|
45 | + $this->code = $code; |
|
46 | + $this->returnTypeNode = new ReturnTypeNode(); |
|
47 | + } |
|
48 | + |
|
49 | + public function getVisibility() |
|
50 | + { |
|
51 | + return $this->visibility; |
|
52 | + } |
|
53 | + |
|
54 | + /** |
|
55 | + * @param string $visibility |
|
56 | + */ |
|
57 | + public function setVisibility($visibility) |
|
58 | + { |
|
59 | + $visibility = strtolower($visibility); |
|
60 | + |
|
61 | + if (!in_array($visibility, array('public', 'private', 'protected'))) { |
|
62 | + throw new InvalidArgumentException(sprintf( |
|
63 | + '`%s` method visibility is not supported.', $visibility |
|
64 | + )); |
|
65 | + } |
|
66 | + |
|
67 | + $this->visibility = $visibility; |
|
68 | + } |
|
69 | + |
|
70 | + public function isStatic() |
|
71 | + { |
|
72 | + return $this->static; |
|
73 | + } |
|
74 | + |
|
75 | + public function setStatic($static = true) |
|
76 | + { |
|
77 | + $this->static = (bool) $static; |
|
78 | + } |
|
79 | + |
|
80 | + public function returnsReference() |
|
81 | + { |
|
82 | + return $this->returnsReference; |
|
83 | + } |
|
84 | + |
|
85 | + public function setReturnsReference() |
|
86 | + { |
|
87 | + $this->returnsReference = true; |
|
88 | + } |
|
89 | + |
|
90 | + public function getName() |
|
91 | + { |
|
92 | + return $this->name; |
|
93 | + } |
|
94 | + |
|
95 | + public function addArgument(ArgumentNode $argument) |
|
96 | + { |
|
97 | + $this->arguments[] = $argument; |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * @return ArgumentNode[] |
|
102 | + */ |
|
103 | + public function getArguments() |
|
104 | + { |
|
105 | + return $this->arguments; |
|
106 | + } |
|
107 | + |
|
108 | + /** |
|
109 | + * @deprecated use getReturnTypeNode instead |
|
110 | + * @return bool |
|
111 | + */ |
|
112 | + public function hasReturnType() |
|
113 | + { |
|
114 | + return (bool) $this->returnTypeNode->getNonNullTypes(); |
|
115 | + } |
|
116 | + |
|
117 | + public function setReturnTypeNode(ReturnTypeNode $returnTypeNode): void |
|
118 | + { |
|
119 | + $this->returnTypeNode = $returnTypeNode; |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @deprecated use setReturnTypeNode instead |
|
124 | + * @param string $type |
|
125 | + */ |
|
126 | + public function setReturnType($type = null) |
|
127 | + { |
|
128 | + $this->returnTypeNode = ($type === '' || $type === null) ? new ReturnTypeNode() : new ReturnTypeNode($type); |
|
129 | + } |
|
130 | + |
|
131 | + /** |
|
132 | + * @deprecated use setReturnTypeNode instead |
|
133 | + * @param bool $bool |
|
134 | + */ |
|
135 | + public function setNullableReturnType($bool = true) |
|
136 | + { |
|
137 | + if ($bool) { |
|
138 | + $this->returnTypeNode = new ReturnTypeNode('null', ...$this->returnTypeNode->getTypes()); |
|
139 | + } |
|
140 | + else { |
|
141 | + $this->returnTypeNode = new ReturnTypeNode(...$this->returnTypeNode->getNonNullTypes()); |
|
142 | + } |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @deprecated use getReturnTypeNode instead |
|
147 | + * @return string|null |
|
148 | + */ |
|
149 | + public function getReturnType() |
|
150 | + { |
|
151 | + if ($types = $this->returnTypeNode->getNonNullTypes()) |
|
152 | + { |
|
153 | + return $types[0]; |
|
154 | + } |
|
155 | + |
|
156 | + return null; |
|
157 | + } |
|
158 | + |
|
159 | + public function getReturnTypeNode() : ReturnTypeNode |
|
160 | + { |
|
161 | + return $this->returnTypeNode; |
|
162 | + } |
|
163 | + |
|
164 | + /** |
|
165 | + * @deprecated use getReturnTypeNode instead |
|
166 | + * @return bool |
|
167 | + */ |
|
168 | + public function hasNullableReturnType() |
|
169 | + { |
|
170 | + return $this->returnTypeNode->canUseNullShorthand(); |
|
171 | + } |
|
172 | + |
|
173 | + /** |
|
174 | + * @param string $code |
|
175 | + */ |
|
176 | + public function setCode($code) |
|
177 | + { |
|
178 | + $this->code = $code; |
|
179 | + } |
|
180 | + |
|
181 | + public function getCode() |
|
182 | + { |
|
183 | + if ($this->returnsReference) |
|
184 | + { |
|
185 | + return "throw new \Prophecy\Exception\Doubler\ReturnByReferenceException('Returning by reference not supported', get_class(\$this), '{$this->name}');"; |
|
186 | + } |
|
187 | + |
|
188 | + return (string) $this->code; |
|
189 | + } |
|
190 | + |
|
191 | + public function useParentCode() |
|
192 | + { |
|
193 | + $this->code = sprintf( |
|
194 | + 'return parent::%s(%s);', $this->getName(), implode(', ', |
|
195 | + array_map(array($this, 'generateArgument'), $this->arguments) |
|
196 | + ) |
|
197 | + ); |
|
198 | + } |
|
199 | + |
|
200 | + private function generateArgument(ArgumentNode $arg) |
|
201 | + { |
|
202 | + $argument = '$'.$arg->getName(); |
|
203 | + |
|
204 | + if ($arg->isVariadic()) { |
|
205 | + $argument = '...'.$argument; |
|
206 | + } |
|
207 | + |
|
208 | + return $argument; |
|
209 | + } |
|
210 | 210 | } |
@@ -58,7 +58,7 @@ |
||
58 | 58 | { |
59 | 59 | $visibility = strtolower($visibility); |
60 | 60 | |
61 | - if (!in_array($visibility, array('public', 'private', 'protected'))) { |
|
61 | + if ( ! in_array($visibility, array('public', 'private', 'protected'))) { |
|
62 | 62 | throw new InvalidArgumentException(sprintf( |
63 | 63 | '`%s` method visibility is not supported.', $visibility |
64 | 64 | )); |
@@ -136,8 +136,7 @@ |
||
136 | 136 | { |
137 | 137 | if ($bool) { |
138 | 138 | $this->returnTypeNode = new ReturnTypeNode('null', ...$this->returnTypeNode->getTypes()); |
139 | - } |
|
140 | - else { |
|
139 | + } else { |
|
141 | 140 | $this->returnTypeNode = new ReturnTypeNode(...$this->returnTypeNode->getNonNullTypes()); |
142 | 141 | } |
143 | 142 | } |
@@ -6,40 +6,40 @@ |
||
6 | 6 | |
7 | 7 | final class ReturnTypeNode extends TypeNodeAbstract |
8 | 8 | { |
9 | - protected function getRealType(string $type): string |
|
10 | - { |
|
11 | - switch ($type) { |
|
12 | - case 'void': |
|
13 | - case 'never': |
|
14 | - return $type; |
|
15 | - default: |
|
16 | - return parent::getRealType($type); |
|
17 | - } |
|
18 | - } |
|
9 | + protected function getRealType(string $type): string |
|
10 | + { |
|
11 | + switch ($type) { |
|
12 | + case 'void': |
|
13 | + case 'never': |
|
14 | + return $type; |
|
15 | + default: |
|
16 | + return parent::getRealType($type); |
|
17 | + } |
|
18 | + } |
|
19 | 19 | |
20 | - protected function guardIsValidType() |
|
21 | - { |
|
22 | - if (isset($this->types['void']) && count($this->types) !== 1) { |
|
23 | - throw new DoubleException('void cannot be part of a union'); |
|
24 | - } |
|
25 | - if (isset($this->types['never']) && count($this->types) !== 1) { |
|
26 | - throw new DoubleException('never cannot be part of a union'); |
|
27 | - } |
|
20 | + protected function guardIsValidType() |
|
21 | + { |
|
22 | + if (isset($this->types['void']) && count($this->types) !== 1) { |
|
23 | + throw new DoubleException('void cannot be part of a union'); |
|
24 | + } |
|
25 | + if (isset($this->types['never']) && count($this->types) !== 1) { |
|
26 | + throw new DoubleException('never cannot be part of a union'); |
|
27 | + } |
|
28 | 28 | |
29 | - parent::guardIsValidType(); |
|
30 | - } |
|
29 | + parent::guardIsValidType(); |
|
30 | + } |
|
31 | 31 | |
32 | - /** |
|
33 | - * @deprecated use hasReturnStatement |
|
34 | - */ |
|
35 | - public function isVoid() |
|
36 | - { |
|
37 | - return $this->types == ['void' => 'void']; |
|
38 | - } |
|
32 | + /** |
|
33 | + * @deprecated use hasReturnStatement |
|
34 | + */ |
|
35 | + public function isVoid() |
|
36 | + { |
|
37 | + return $this->types == ['void' => 'void']; |
|
38 | + } |
|
39 | 39 | |
40 | - public function hasReturnStatement(): bool |
|
41 | - { |
|
42 | - return $this->types !== ['void' => 'void'] |
|
43 | - && $this->types !== ['never' => 'never']; |
|
44 | - } |
|
40 | + public function hasReturnStatement(): bool |
|
41 | + { |
|
42 | + return $this->types !== ['void' => 'void'] |
|
43 | + && $this->types !== ['never' => 'never']; |
|
44 | + } |
|
45 | 45 | } |
@@ -6,92 +6,92 @@ |
||
6 | 6 | |
7 | 7 | abstract class TypeNodeAbstract |
8 | 8 | { |
9 | - /** @var string[] */ |
|
10 | - protected $types = []; |
|
11 | - |
|
12 | - public function __construct(string ...$types) |
|
13 | - { |
|
14 | - foreach ($types as $type) { |
|
15 | - $type = $this->getRealType($type); |
|
16 | - $this->types[$type] = $type; |
|
17 | - } |
|
18 | - |
|
19 | - $this->guardIsValidType(); |
|
20 | - } |
|
21 | - |
|
22 | - public function canUseNullShorthand(): bool |
|
23 | - { |
|
24 | - return isset($this->types['null']) && count($this->types) <= 2; |
|
25 | - } |
|
26 | - |
|
27 | - public function getTypes(): array |
|
28 | - { |
|
29 | - return array_values($this->types); |
|
30 | - } |
|
31 | - |
|
32 | - public function getNonNullTypes(): array |
|
33 | - { |
|
34 | - $nonNullTypes = $this->types; |
|
35 | - unset($nonNullTypes['null']); |
|
36 | - |
|
37 | - return array_values($nonNullTypes); |
|
38 | - } |
|
39 | - |
|
40 | - protected function prefixWithNsSeparator(string $type): string |
|
41 | - { |
|
42 | - return '\\' . ltrim($type, '\\'); |
|
43 | - } |
|
44 | - |
|
45 | - protected function getRealType(string $type): string |
|
46 | - { |
|
47 | - switch ($type) { |
|
48 | - // type aliases |
|
49 | - case 'double': |
|
50 | - case 'real': |
|
51 | - return 'float'; |
|
52 | - case 'boolean': |
|
53 | - return 'bool'; |
|
54 | - case 'integer': |
|
55 | - return 'int'; |
|
56 | - |
|
57 | - // built in types |
|
58 | - case 'self': |
|
59 | - case 'static': |
|
60 | - case 'array': |
|
61 | - case 'callable': |
|
62 | - case 'bool': |
|
63 | - case 'false': |
|
64 | - case 'float': |
|
65 | - case 'int': |
|
66 | - case 'string': |
|
67 | - case 'iterable': |
|
68 | - case 'object': |
|
69 | - case 'null': |
|
70 | - return $type; |
|
71 | - case 'mixed': |
|
72 | - return \PHP_VERSION_ID < 80000 ? $this->prefixWithNsSeparator($type) : $type; |
|
73 | - |
|
74 | - default: |
|
75 | - return $this->prefixWithNsSeparator($type); |
|
76 | - } |
|
77 | - } |
|
78 | - |
|
79 | - protected function guardIsValidType() |
|
80 | - { |
|
81 | - if ($this->types == ['null' => 'null']) { |
|
82 | - throw new DoubleException('Type cannot be standalone null'); |
|
83 | - } |
|
84 | - |
|
85 | - if ($this->types == ['false' => 'false']) { |
|
86 | - throw new DoubleException('Type cannot be standalone false'); |
|
87 | - } |
|
88 | - |
|
89 | - if ($this->types == ['false' => 'false', 'null' => 'null']) { |
|
90 | - throw new DoubleException('Type cannot be nullable false'); |
|
91 | - } |
|
92 | - |
|
93 | - if (\PHP_VERSION_ID >= 80000 && isset($this->types['mixed']) && count($this->types) !== 1) { |
|
94 | - throw new DoubleException('mixed cannot be part of a union'); |
|
95 | - } |
|
96 | - } |
|
9 | + /** @var string[] */ |
|
10 | + protected $types = []; |
|
11 | + |
|
12 | + public function __construct(string ...$types) |
|
13 | + { |
|
14 | + foreach ($types as $type) { |
|
15 | + $type = $this->getRealType($type); |
|
16 | + $this->types[$type] = $type; |
|
17 | + } |
|
18 | + |
|
19 | + $this->guardIsValidType(); |
|
20 | + } |
|
21 | + |
|
22 | + public function canUseNullShorthand(): bool |
|
23 | + { |
|
24 | + return isset($this->types['null']) && count($this->types) <= 2; |
|
25 | + } |
|
26 | + |
|
27 | + public function getTypes(): array |
|
28 | + { |
|
29 | + return array_values($this->types); |
|
30 | + } |
|
31 | + |
|
32 | + public function getNonNullTypes(): array |
|
33 | + { |
|
34 | + $nonNullTypes = $this->types; |
|
35 | + unset($nonNullTypes['null']); |
|
36 | + |
|
37 | + return array_values($nonNullTypes); |
|
38 | + } |
|
39 | + |
|
40 | + protected function prefixWithNsSeparator(string $type): string |
|
41 | + { |
|
42 | + return '\\' . ltrim($type, '\\'); |
|
43 | + } |
|
44 | + |
|
45 | + protected function getRealType(string $type): string |
|
46 | + { |
|
47 | + switch ($type) { |
|
48 | + // type aliases |
|
49 | + case 'double': |
|
50 | + case 'real': |
|
51 | + return 'float'; |
|
52 | + case 'boolean': |
|
53 | + return 'bool'; |
|
54 | + case 'integer': |
|
55 | + return 'int'; |
|
56 | + |
|
57 | + // built in types |
|
58 | + case 'self': |
|
59 | + case 'static': |
|
60 | + case 'array': |
|
61 | + case 'callable': |
|
62 | + case 'bool': |
|
63 | + case 'false': |
|
64 | + case 'float': |
|
65 | + case 'int': |
|
66 | + case 'string': |
|
67 | + case 'iterable': |
|
68 | + case 'object': |
|
69 | + case 'null': |
|
70 | + return $type; |
|
71 | + case 'mixed': |
|
72 | + return \PHP_VERSION_ID < 80000 ? $this->prefixWithNsSeparator($type) : $type; |
|
73 | + |
|
74 | + default: |
|
75 | + return $this->prefixWithNsSeparator($type); |
|
76 | + } |
|
77 | + } |
|
78 | + |
|
79 | + protected function guardIsValidType() |
|
80 | + { |
|
81 | + if ($this->types == ['null' => 'null']) { |
|
82 | + throw new DoubleException('Type cannot be standalone null'); |
|
83 | + } |
|
84 | + |
|
85 | + if ($this->types == ['false' => 'false']) { |
|
86 | + throw new DoubleException('Type cannot be standalone false'); |
|
87 | + } |
|
88 | + |
|
89 | + if ($this->types == ['false' => 'false', 'null' => 'null']) { |
|
90 | + throw new DoubleException('Type cannot be nullable false'); |
|
91 | + } |
|
92 | + |
|
93 | + if (\PHP_VERSION_ID >= 80000 && isset($this->types['mixed']) && count($this->types) !== 1) { |
|
94 | + throw new DoubleException('mixed cannot be part of a union'); |
|
95 | + } |
|
96 | + } |
|
97 | 97 | } |
@@ -39,7 +39,7 @@ |
||
39 | 39 | |
40 | 40 | protected function prefixWithNsSeparator(string $type): string |
41 | 41 | { |
42 | - return '\\' . ltrim($type, '\\'); |
|
42 | + return '\\'.ltrim($type, '\\'); |
|
43 | 43 | } |
44 | 44 | |
45 | 45 | protected function getRealType(string $type): string |
@@ -21,149 +21,149 @@ |
||
21 | 21 | */ |
22 | 22 | class ClassNode |
23 | 23 | { |
24 | - private $parentClass = 'stdClass'; |
|
25 | - private $interfaces = array(); |
|
26 | - private $properties = array(); |
|
27 | - private $unextendableMethods = array(); |
|
28 | - |
|
29 | - /** |
|
30 | - * @var MethodNode[] |
|
31 | - */ |
|
32 | - private $methods = array(); |
|
33 | - |
|
34 | - public function getParentClass() |
|
35 | - { |
|
36 | - return $this->parentClass; |
|
37 | - } |
|
38 | - |
|
39 | - /** |
|
40 | - * @param string $class |
|
41 | - */ |
|
42 | - public function setParentClass($class) |
|
43 | - { |
|
44 | - $this->parentClass = $class ?: 'stdClass'; |
|
45 | - } |
|
46 | - |
|
47 | - /** |
|
48 | - * @return string[] |
|
49 | - */ |
|
50 | - public function getInterfaces() |
|
51 | - { |
|
52 | - return $this->interfaces; |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * @param string $interface |
|
57 | - */ |
|
58 | - public function addInterface($interface) |
|
59 | - { |
|
60 | - if ($this->hasInterface($interface)) { |
|
61 | - return; |
|
62 | - } |
|
63 | - |
|
64 | - array_unshift($this->interfaces, $interface); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * @param string $interface |
|
69 | - * |
|
70 | - * @return bool |
|
71 | - */ |
|
72 | - public function hasInterface($interface) |
|
73 | - { |
|
74 | - return in_array($interface, $this->interfaces); |
|
75 | - } |
|
76 | - |
|
77 | - public function getProperties() |
|
78 | - { |
|
79 | - return $this->properties; |
|
80 | - } |
|
81 | - |
|
82 | - public function addProperty($name, $visibility = 'public') |
|
83 | - { |
|
84 | - $visibility = strtolower($visibility); |
|
85 | - |
|
86 | - if (!in_array($visibility, array('public', 'private', 'protected'))) { |
|
87 | - throw new InvalidArgumentException(sprintf( |
|
88 | - '`%s` property visibility is not supported.', $visibility |
|
89 | - )); |
|
90 | - } |
|
91 | - |
|
92 | - $this->properties[$name] = $visibility; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @return MethodNode[] |
|
97 | - */ |
|
98 | - public function getMethods() |
|
99 | - { |
|
100 | - return $this->methods; |
|
101 | - } |
|
102 | - |
|
103 | - public function addMethod(MethodNode $method, $force = false) |
|
104 | - { |
|
105 | - if (!$this->isExtendable($method->getName())){ |
|
106 | - $message = sprintf( |
|
107 | - 'Method `%s` is not extendable, so can not be added.', $method->getName() |
|
108 | - ); |
|
109 | - throw new MethodNotExtendableException($message, $this->getParentClass(), $method->getName()); |
|
110 | - } |
|
111 | - |
|
112 | - if ($force || !isset($this->methods[$method->getName()])) { |
|
113 | - $this->methods[$method->getName()] = $method; |
|
114 | - } |
|
115 | - } |
|
116 | - |
|
117 | - public function removeMethod($name) |
|
118 | - { |
|
119 | - unset($this->methods[$name]); |
|
120 | - } |
|
121 | - |
|
122 | - /** |
|
123 | - * @param string $name |
|
124 | - * |
|
125 | - * @return MethodNode|null |
|
126 | - */ |
|
127 | - public function getMethod($name) |
|
128 | - { |
|
129 | - return $this->hasMethod($name) ? $this->methods[$name] : null; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * @param string $name |
|
134 | - * |
|
135 | - * @return bool |
|
136 | - */ |
|
137 | - public function hasMethod($name) |
|
138 | - { |
|
139 | - return isset($this->methods[$name]); |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @return string[] |
|
144 | - */ |
|
145 | - public function getUnextendableMethods() |
|
146 | - { |
|
147 | - return $this->unextendableMethods; |
|
148 | - } |
|
149 | - |
|
150 | - /** |
|
151 | - * @param string $unextendableMethod |
|
152 | - */ |
|
153 | - public function addUnextendableMethod($unextendableMethod) |
|
154 | - { |
|
155 | - if (!$this->isExtendable($unextendableMethod)){ |
|
156 | - return; |
|
157 | - } |
|
158 | - $this->unextendableMethods[] = $unextendableMethod; |
|
159 | - } |
|
160 | - |
|
161 | - /** |
|
162 | - * @param string $method |
|
163 | - * @return bool |
|
164 | - */ |
|
165 | - public function isExtendable($method) |
|
166 | - { |
|
167 | - return !in_array($method, $this->unextendableMethods); |
|
168 | - } |
|
24 | + private $parentClass = 'stdClass'; |
|
25 | + private $interfaces = array(); |
|
26 | + private $properties = array(); |
|
27 | + private $unextendableMethods = array(); |
|
28 | + |
|
29 | + /** |
|
30 | + * @var MethodNode[] |
|
31 | + */ |
|
32 | + private $methods = array(); |
|
33 | + |
|
34 | + public function getParentClass() |
|
35 | + { |
|
36 | + return $this->parentClass; |
|
37 | + } |
|
38 | + |
|
39 | + /** |
|
40 | + * @param string $class |
|
41 | + */ |
|
42 | + public function setParentClass($class) |
|
43 | + { |
|
44 | + $this->parentClass = $class ?: 'stdClass'; |
|
45 | + } |
|
46 | + |
|
47 | + /** |
|
48 | + * @return string[] |
|
49 | + */ |
|
50 | + public function getInterfaces() |
|
51 | + { |
|
52 | + return $this->interfaces; |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * @param string $interface |
|
57 | + */ |
|
58 | + public function addInterface($interface) |
|
59 | + { |
|
60 | + if ($this->hasInterface($interface)) { |
|
61 | + return; |
|
62 | + } |
|
63 | + |
|
64 | + array_unshift($this->interfaces, $interface); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * @param string $interface |
|
69 | + * |
|
70 | + * @return bool |
|
71 | + */ |
|
72 | + public function hasInterface($interface) |
|
73 | + { |
|
74 | + return in_array($interface, $this->interfaces); |
|
75 | + } |
|
76 | + |
|
77 | + public function getProperties() |
|
78 | + { |
|
79 | + return $this->properties; |
|
80 | + } |
|
81 | + |
|
82 | + public function addProperty($name, $visibility = 'public') |
|
83 | + { |
|
84 | + $visibility = strtolower($visibility); |
|
85 | + |
|
86 | + if (!in_array($visibility, array('public', 'private', 'protected'))) { |
|
87 | + throw new InvalidArgumentException(sprintf( |
|
88 | + '`%s` property visibility is not supported.', $visibility |
|
89 | + )); |
|
90 | + } |
|
91 | + |
|
92 | + $this->properties[$name] = $visibility; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @return MethodNode[] |
|
97 | + */ |
|
98 | + public function getMethods() |
|
99 | + { |
|
100 | + return $this->methods; |
|
101 | + } |
|
102 | + |
|
103 | + public function addMethod(MethodNode $method, $force = false) |
|
104 | + { |
|
105 | + if (!$this->isExtendable($method->getName())){ |
|
106 | + $message = sprintf( |
|
107 | + 'Method `%s` is not extendable, so can not be added.', $method->getName() |
|
108 | + ); |
|
109 | + throw new MethodNotExtendableException($message, $this->getParentClass(), $method->getName()); |
|
110 | + } |
|
111 | + |
|
112 | + if ($force || !isset($this->methods[$method->getName()])) { |
|
113 | + $this->methods[$method->getName()] = $method; |
|
114 | + } |
|
115 | + } |
|
116 | + |
|
117 | + public function removeMethod($name) |
|
118 | + { |
|
119 | + unset($this->methods[$name]); |
|
120 | + } |
|
121 | + |
|
122 | + /** |
|
123 | + * @param string $name |
|
124 | + * |
|
125 | + * @return MethodNode|null |
|
126 | + */ |
|
127 | + public function getMethod($name) |
|
128 | + { |
|
129 | + return $this->hasMethod($name) ? $this->methods[$name] : null; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * @param string $name |
|
134 | + * |
|
135 | + * @return bool |
|
136 | + */ |
|
137 | + public function hasMethod($name) |
|
138 | + { |
|
139 | + return isset($this->methods[$name]); |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @return string[] |
|
144 | + */ |
|
145 | + public function getUnextendableMethods() |
|
146 | + { |
|
147 | + return $this->unextendableMethods; |
|
148 | + } |
|
149 | + |
|
150 | + /** |
|
151 | + * @param string $unextendableMethod |
|
152 | + */ |
|
153 | + public function addUnextendableMethod($unextendableMethod) |
|
154 | + { |
|
155 | + if (!$this->isExtendable($unextendableMethod)){ |
|
156 | + return; |
|
157 | + } |
|
158 | + $this->unextendableMethods[] = $unextendableMethod; |
|
159 | + } |
|
160 | + |
|
161 | + /** |
|
162 | + * @param string $method |
|
163 | + * @return bool |
|
164 | + */ |
|
165 | + public function isExtendable($method) |
|
166 | + { |
|
167 | + return !in_array($method, $this->unextendableMethods); |
|
168 | + } |
|
169 | 169 | } |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | /** |
30 | 30 | * @var MethodNode[] |
31 | 31 | */ |
32 | - private $methods = array(); |
|
32 | + private $methods = array(); |
|
33 | 33 | |
34 | 34 | public function getParentClass() |
35 | 35 | { |
@@ -83,7 +83,7 @@ discard block |
||
83 | 83 | { |
84 | 84 | $visibility = strtolower($visibility); |
85 | 85 | |
86 | - if (!in_array($visibility, array('public', 'private', 'protected'))) { |
|
86 | + if ( ! in_array($visibility, array('public', 'private', 'protected'))) { |
|
87 | 87 | throw new InvalidArgumentException(sprintf( |
88 | 88 | '`%s` property visibility is not supported.', $visibility |
89 | 89 | )); |
@@ -102,14 +102,14 @@ discard block |
||
102 | 102 | |
103 | 103 | public function addMethod(MethodNode $method, $force = false) |
104 | 104 | { |
105 | - if (!$this->isExtendable($method->getName())){ |
|
105 | + if ( ! $this->isExtendable($method->getName())) { |
|
106 | 106 | $message = sprintf( |
107 | 107 | 'Method `%s` is not extendable, so can not be added.', $method->getName() |
108 | 108 | ); |
109 | 109 | throw new MethodNotExtendableException($message, $this->getParentClass(), $method->getName()); |
110 | 110 | } |
111 | 111 | |
112 | - if ($force || !isset($this->methods[$method->getName()])) { |
|
112 | + if ($force || ! isset($this->methods[$method->getName()])) { |
|
113 | 113 | $this->methods[$method->getName()] = $method; |
114 | 114 | } |
115 | 115 | } |
@@ -152,7 +152,7 @@ discard block |
||
152 | 152 | */ |
153 | 153 | public function addUnextendableMethod($unextendableMethod) |
154 | 154 | { |
155 | - if (!$this->isExtendable($unextendableMethod)){ |
|
155 | + if ( ! $this->isExtendable($unextendableMethod)) { |
|
156 | 156 | return; |
157 | 157 | } |
158 | 158 | $this->unextendableMethods[] = $unextendableMethod; |
@@ -164,6 +164,6 @@ discard block |
||
164 | 164 | */ |
165 | 165 | public function isExtendable($method) |
166 | 166 | { |
167 | - return !in_array($method, $this->unextendableMethods); |
|
167 | + return ! in_array($method, $this->unextendableMethods); |
|
168 | 168 | } |
169 | 169 | } |
@@ -18,116 +18,116 @@ |
||
18 | 18 | */ |
19 | 19 | class ArgumentNode |
20 | 20 | { |
21 | - private $name; |
|
22 | - private $default; |
|
23 | - private $optional = false; |
|
24 | - private $byReference = false; |
|
25 | - private $isVariadic = false; |
|
26 | - |
|
27 | - /** @var ArgumentTypeNode */ |
|
28 | - private $typeNode; |
|
29 | - |
|
30 | - /** |
|
31 | - * @param string $name |
|
32 | - */ |
|
33 | - public function __construct($name) |
|
34 | - { |
|
35 | - $this->name = $name; |
|
36 | - $this->typeNode = new ArgumentTypeNode(); |
|
37 | - } |
|
38 | - |
|
39 | - public function getName() |
|
40 | - { |
|
41 | - return $this->name; |
|
42 | - } |
|
43 | - |
|
44 | - public function setTypeNode(ArgumentTypeNode $typeNode) |
|
45 | - { |
|
46 | - $this->typeNode = $typeNode; |
|
47 | - } |
|
48 | - |
|
49 | - public function getTypeNode() : ArgumentTypeNode |
|
50 | - { |
|
51 | - return $this->typeNode; |
|
52 | - } |
|
53 | - |
|
54 | - public function hasDefault() |
|
55 | - { |
|
56 | - return $this->isOptional() && !$this->isVariadic(); |
|
57 | - } |
|
58 | - |
|
59 | - public function getDefault() |
|
60 | - { |
|
61 | - return $this->default; |
|
62 | - } |
|
63 | - |
|
64 | - public function setDefault($default = null) |
|
65 | - { |
|
66 | - $this->optional = true; |
|
67 | - $this->default = $default; |
|
68 | - } |
|
69 | - |
|
70 | - public function isOptional() |
|
71 | - { |
|
72 | - return $this->optional; |
|
73 | - } |
|
74 | - |
|
75 | - public function setAsPassedByReference($byReference = true) |
|
76 | - { |
|
77 | - $this->byReference = $byReference; |
|
78 | - } |
|
79 | - |
|
80 | - public function isPassedByReference() |
|
81 | - { |
|
82 | - return $this->byReference; |
|
83 | - } |
|
84 | - |
|
85 | - public function setAsVariadic($isVariadic = true) |
|
86 | - { |
|
87 | - $this->isVariadic = $isVariadic; |
|
88 | - } |
|
89 | - |
|
90 | - public function isVariadic() |
|
91 | - { |
|
92 | - return $this->isVariadic; |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * @deprecated use getArgumentTypeNode instead |
|
97 | - * @return string|null |
|
98 | - */ |
|
99 | - public function getTypeHint() |
|
100 | - { |
|
101 | - $type = $this->typeNode->getNonNullTypes() ? $this->typeNode->getNonNullTypes()[0] : null; |
|
102 | - |
|
103 | - return $type ? ltrim($type, '\\') : null; |
|
104 | - } |
|
105 | - |
|
106 | - /** |
|
107 | - * @deprecated use setArgumentTypeNode instead |
|
108 | - * @param string|null $typeHint |
|
109 | - */ |
|
110 | - public function setTypeHint($typeHint = null) |
|
111 | - { |
|
112 | - $this->typeNode = ($typeHint === null) ? new ArgumentTypeNode() : new ArgumentTypeNode($typeHint); |
|
113 | - } |
|
114 | - |
|
115 | - /** |
|
116 | - * @deprecated use getArgumentTypeNode instead |
|
117 | - * @return bool |
|
118 | - */ |
|
119 | - public function isNullable() |
|
120 | - { |
|
121 | - return $this->typeNode->canUseNullShorthand(); |
|
122 | - } |
|
123 | - |
|
124 | - /** |
|
125 | - * @deprecated use getArgumentTypeNode instead |
|
126 | - * @param bool $isNullable |
|
127 | - */ |
|
128 | - public function setAsNullable($isNullable = true) |
|
129 | - { |
|
130 | - $nonNullTypes = $this->typeNode->getNonNullTypes(); |
|
131 | - $this->typeNode = $isNullable ? new ArgumentTypeNode('null', ...$nonNullTypes) : new ArgumentTypeNode(...$nonNullTypes); |
|
132 | - } |
|
21 | + private $name; |
|
22 | + private $default; |
|
23 | + private $optional = false; |
|
24 | + private $byReference = false; |
|
25 | + private $isVariadic = false; |
|
26 | + |
|
27 | + /** @var ArgumentTypeNode */ |
|
28 | + private $typeNode; |
|
29 | + |
|
30 | + /** |
|
31 | + * @param string $name |
|
32 | + */ |
|
33 | + public function __construct($name) |
|
34 | + { |
|
35 | + $this->name = $name; |
|
36 | + $this->typeNode = new ArgumentTypeNode(); |
|
37 | + } |
|
38 | + |
|
39 | + public function getName() |
|
40 | + { |
|
41 | + return $this->name; |
|
42 | + } |
|
43 | + |
|
44 | + public function setTypeNode(ArgumentTypeNode $typeNode) |
|
45 | + { |
|
46 | + $this->typeNode = $typeNode; |
|
47 | + } |
|
48 | + |
|
49 | + public function getTypeNode() : ArgumentTypeNode |
|
50 | + { |
|
51 | + return $this->typeNode; |
|
52 | + } |
|
53 | + |
|
54 | + public function hasDefault() |
|
55 | + { |
|
56 | + return $this->isOptional() && !$this->isVariadic(); |
|
57 | + } |
|
58 | + |
|
59 | + public function getDefault() |
|
60 | + { |
|
61 | + return $this->default; |
|
62 | + } |
|
63 | + |
|
64 | + public function setDefault($default = null) |
|
65 | + { |
|
66 | + $this->optional = true; |
|
67 | + $this->default = $default; |
|
68 | + } |
|
69 | + |
|
70 | + public function isOptional() |
|
71 | + { |
|
72 | + return $this->optional; |
|
73 | + } |
|
74 | + |
|
75 | + public function setAsPassedByReference($byReference = true) |
|
76 | + { |
|
77 | + $this->byReference = $byReference; |
|
78 | + } |
|
79 | + |
|
80 | + public function isPassedByReference() |
|
81 | + { |
|
82 | + return $this->byReference; |
|
83 | + } |
|
84 | + |
|
85 | + public function setAsVariadic($isVariadic = true) |
|
86 | + { |
|
87 | + $this->isVariadic = $isVariadic; |
|
88 | + } |
|
89 | + |
|
90 | + public function isVariadic() |
|
91 | + { |
|
92 | + return $this->isVariadic; |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * @deprecated use getArgumentTypeNode instead |
|
97 | + * @return string|null |
|
98 | + */ |
|
99 | + public function getTypeHint() |
|
100 | + { |
|
101 | + $type = $this->typeNode->getNonNullTypes() ? $this->typeNode->getNonNullTypes()[0] : null; |
|
102 | + |
|
103 | + return $type ? ltrim($type, '\\') : null; |
|
104 | + } |
|
105 | + |
|
106 | + /** |
|
107 | + * @deprecated use setArgumentTypeNode instead |
|
108 | + * @param string|null $typeHint |
|
109 | + */ |
|
110 | + public function setTypeHint($typeHint = null) |
|
111 | + { |
|
112 | + $this->typeNode = ($typeHint === null) ? new ArgumentTypeNode() : new ArgumentTypeNode($typeHint); |
|
113 | + } |
|
114 | + |
|
115 | + /** |
|
116 | + * @deprecated use getArgumentTypeNode instead |
|
117 | + * @return bool |
|
118 | + */ |
|
119 | + public function isNullable() |
|
120 | + { |
|
121 | + return $this->typeNode->canUseNullShorthand(); |
|
122 | + } |
|
123 | + |
|
124 | + /** |
|
125 | + * @deprecated use getArgumentTypeNode instead |
|
126 | + * @param bool $isNullable |
|
127 | + */ |
|
128 | + public function setAsNullable($isNullable = true) |
|
129 | + { |
|
130 | + $nonNullTypes = $this->typeNode->getNonNullTypes(); |
|
131 | + $this->typeNode = $isNullable ? new ArgumentTypeNode('null', ...$nonNullTypes) : new ArgumentTypeNode(...$nonNullTypes); |
|
132 | + } |
|
133 | 133 | } |
@@ -53,7 +53,7 @@ |
||
53 | 53 | |
54 | 54 | public function hasDefault() |
55 | 55 | { |
56 | - return $this->isOptional() && !$this->isVariadic(); |
|
56 | + return $this->isOptional() && ! $this->isVariadic(); |
|
57 | 57 | } |
58 | 58 | |
59 | 59 | public function getDefault() |
@@ -10,34 +10,34 @@ |
||
10 | 10 | */ |
11 | 11 | final class TypeHintReference |
12 | 12 | { |
13 | - public function isBuiltInParamTypeHint($type) |
|
14 | - { |
|
15 | - switch ($type) { |
|
16 | - case 'self': |
|
17 | - case 'array': |
|
18 | - case 'callable': |
|
19 | - case 'bool': |
|
20 | - case 'float': |
|
21 | - case 'int': |
|
22 | - case 'string': |
|
23 | - case 'iterable': |
|
24 | - case 'object': |
|
25 | - return true; |
|
13 | + public function isBuiltInParamTypeHint($type) |
|
14 | + { |
|
15 | + switch ($type) { |
|
16 | + case 'self': |
|
17 | + case 'array': |
|
18 | + case 'callable': |
|
19 | + case 'bool': |
|
20 | + case 'float': |
|
21 | + case 'int': |
|
22 | + case 'string': |
|
23 | + case 'iterable': |
|
24 | + case 'object': |
|
25 | + return true; |
|
26 | 26 | |
27 | - case 'mixed': |
|
28 | - return PHP_VERSION_ID >= 80000; |
|
27 | + case 'mixed': |
|
28 | + return PHP_VERSION_ID >= 80000; |
|
29 | 29 | |
30 | - default: |
|
31 | - return false; |
|
32 | - } |
|
33 | - } |
|
30 | + default: |
|
31 | + return false; |
|
32 | + } |
|
33 | + } |
|
34 | 34 | |
35 | - public function isBuiltInReturnTypeHint($type) |
|
36 | - { |
|
37 | - if ($type === 'void') { |
|
38 | - return true; |
|
39 | - } |
|
35 | + public function isBuiltInReturnTypeHint($type) |
|
36 | + { |
|
37 | + if ($type === 'void') { |
|
38 | + return true; |
|
39 | + } |
|
40 | 40 | |
41 | - return $this->isBuiltInParamTypeHint($type); |
|
42 | - } |
|
41 | + return $this->isBuiltInParamTypeHint($type); |
|
42 | + } |
|
43 | 43 | } |
@@ -22,89 +22,89 @@ |
||
22 | 22 | */ |
23 | 23 | class ClassCodeGenerator |
24 | 24 | { |
25 | - public function __construct(TypeHintReference $typeHintReference = null) |
|
26 | - { |
|
27 | - } |
|
28 | - |
|
29 | - /** |
|
30 | - * Generates PHP code for class node. |
|
31 | - * |
|
32 | - * @param string $classname |
|
33 | - * @param Node\ClassNode $class |
|
34 | - * |
|
35 | - * @return string |
|
36 | - */ |
|
37 | - public function generate($classname, Node\ClassNode $class) |
|
38 | - { |
|
39 | - $parts = explode('\\', $classname); |
|
40 | - $classname = array_pop($parts); |
|
41 | - $namespace = implode('\\', $parts); |
|
42 | - |
|
43 | - $code = sprintf("class %s extends \%s implements %s {\n", |
|
44 | - $classname, $class->getParentClass(), implode(', ', |
|
45 | - array_map(function ($interface) {return '\\'.$interface;}, $class->getInterfaces()) |
|
46 | - ) |
|
47 | - ); |
|
48 | - |
|
49 | - foreach ($class->getProperties() as $name => $visibility) { |
|
50 | - $code .= sprintf("%s \$%s;\n", $visibility, $name); |
|
51 | - } |
|
52 | - $code .= "\n"; |
|
53 | - |
|
54 | - foreach ($class->getMethods() as $method) { |
|
55 | - $code .= $this->generateMethod($method)."\n"; |
|
56 | - } |
|
57 | - $code .= "\n}"; |
|
58 | - |
|
59 | - return sprintf("namespace %s {\n%s\n}", $namespace, $code); |
|
60 | - } |
|
61 | - |
|
62 | - private function generateMethod(Node\MethodNode $method) |
|
63 | - { |
|
64 | - $php = sprintf("%s %s function %s%s(%s)%s {\n", |
|
65 | - $method->getVisibility(), |
|
66 | - $method->isStatic() ? 'static' : '', |
|
67 | - $method->returnsReference() ? '&':'', |
|
68 | - $method->getName(), |
|
69 | - implode(', ', $this->generateArguments($method->getArguments())), |
|
70 | - ($ret = $this->generateTypes($method->getReturnTypeNode())) ? ': '.$ret : '' |
|
71 | - ); |
|
72 | - $php .= $method->getCode()."\n"; |
|
73 | - |
|
74 | - return $php.'}'; |
|
75 | - } |
|
76 | - |
|
77 | - private function generateTypes(TypeNodeAbstract $typeNode): string |
|
78 | - { |
|
79 | - if (!$typeNode->getTypes()) { |
|
80 | - return ''; |
|
81 | - } |
|
82 | - |
|
83 | - // When we require PHP 8 we can stop generating ?foo nullables and remove this first block |
|
84 | - if ($typeNode->canUseNullShorthand()) { |
|
85 | - return sprintf( '?%s', $typeNode->getNonNullTypes()[0]); |
|
86 | - } else { |
|
87 | - return join('|', $typeNode->getTypes()); |
|
88 | - } |
|
89 | - } |
|
90 | - |
|
91 | - private function generateArguments(array $arguments) |
|
92 | - { |
|
93 | - return array_map(function (Node\ArgumentNode $argument){ |
|
94 | - |
|
95 | - $php = $this->generateTypes($argument->getTypeNode()); |
|
96 | - |
|
97 | - $php .= ' '.($argument->isPassedByReference() ? '&' : ''); |
|
98 | - |
|
99 | - $php .= $argument->isVariadic() ? '...' : ''; |
|
100 | - |
|
101 | - $php .= '$'.$argument->getName(); |
|
102 | - |
|
103 | - if ($argument->isOptional() && !$argument->isVariadic()) { |
|
104 | - $php .= ' = '.var_export($argument->getDefault(), true); |
|
105 | - } |
|
106 | - |
|
107 | - return $php; |
|
108 | - }, $arguments); |
|
109 | - } |
|
25 | + public function __construct(TypeHintReference $typeHintReference = null) |
|
26 | + { |
|
27 | + } |
|
28 | + |
|
29 | + /** |
|
30 | + * Generates PHP code for class node. |
|
31 | + * |
|
32 | + * @param string $classname |
|
33 | + * @param Node\ClassNode $class |
|
34 | + * |
|
35 | + * @return string |
|
36 | + */ |
|
37 | + public function generate($classname, Node\ClassNode $class) |
|
38 | + { |
|
39 | + $parts = explode('\\', $classname); |
|
40 | + $classname = array_pop($parts); |
|
41 | + $namespace = implode('\\', $parts); |
|
42 | + |
|
43 | + $code = sprintf("class %s extends \%s implements %s {\n", |
|
44 | + $classname, $class->getParentClass(), implode(', ', |
|
45 | + array_map(function ($interface) {return '\\'.$interface;}, $class->getInterfaces()) |
|
46 | + ) |
|
47 | + ); |
|
48 | + |
|
49 | + foreach ($class->getProperties() as $name => $visibility) { |
|
50 | + $code .= sprintf("%s \$%s;\n", $visibility, $name); |
|
51 | + } |
|
52 | + $code .= "\n"; |
|
53 | + |
|
54 | + foreach ($class->getMethods() as $method) { |
|
55 | + $code .= $this->generateMethod($method)."\n"; |
|
56 | + } |
|
57 | + $code .= "\n}"; |
|
58 | + |
|
59 | + return sprintf("namespace %s {\n%s\n}", $namespace, $code); |
|
60 | + } |
|
61 | + |
|
62 | + private function generateMethod(Node\MethodNode $method) |
|
63 | + { |
|
64 | + $php = sprintf("%s %s function %s%s(%s)%s {\n", |
|
65 | + $method->getVisibility(), |
|
66 | + $method->isStatic() ? 'static' : '', |
|
67 | + $method->returnsReference() ? '&':'', |
|
68 | + $method->getName(), |
|
69 | + implode(', ', $this->generateArguments($method->getArguments())), |
|
70 | + ($ret = $this->generateTypes($method->getReturnTypeNode())) ? ': '.$ret : '' |
|
71 | + ); |
|
72 | + $php .= $method->getCode()."\n"; |
|
73 | + |
|
74 | + return $php.'}'; |
|
75 | + } |
|
76 | + |
|
77 | + private function generateTypes(TypeNodeAbstract $typeNode): string |
|
78 | + { |
|
79 | + if (!$typeNode->getTypes()) { |
|
80 | + return ''; |
|
81 | + } |
|
82 | + |
|
83 | + // When we require PHP 8 we can stop generating ?foo nullables and remove this first block |
|
84 | + if ($typeNode->canUseNullShorthand()) { |
|
85 | + return sprintf( '?%s', $typeNode->getNonNullTypes()[0]); |
|
86 | + } else { |
|
87 | + return join('|', $typeNode->getTypes()); |
|
88 | + } |
|
89 | + } |
|
90 | + |
|
91 | + private function generateArguments(array $arguments) |
|
92 | + { |
|
93 | + return array_map(function (Node\ArgumentNode $argument){ |
|
94 | + |
|
95 | + $php = $this->generateTypes($argument->getTypeNode()); |
|
96 | + |
|
97 | + $php .= ' '.($argument->isPassedByReference() ? '&' : ''); |
|
98 | + |
|
99 | + $php .= $argument->isVariadic() ? '...' : ''; |
|
100 | + |
|
101 | + $php .= '$'.$argument->getName(); |
|
102 | + |
|
103 | + if ($argument->isOptional() && !$argument->isVariadic()) { |
|
104 | + $php .= ' = '.var_export($argument->getDefault(), true); |
|
105 | + } |
|
106 | + |
|
107 | + return $php; |
|
108 | + }, $arguments); |
|
109 | + } |
|
110 | 110 | } |
@@ -42,7 +42,7 @@ discard block |
||
42 | 42 | |
43 | 43 | $code = sprintf("class %s extends \%s implements %s {\n", |
44 | 44 | $classname, $class->getParentClass(), implode(', ', |
45 | - array_map(function ($interface) {return '\\'.$interface;}, $class->getInterfaces()) |
|
45 | + array_map(function($interface) {return '\\'.$interface; }, $class->getInterfaces()) |
|
46 | 46 | ) |
47 | 47 | ); |
48 | 48 | |
@@ -64,7 +64,7 @@ discard block |
||
64 | 64 | $php = sprintf("%s %s function %s%s(%s)%s {\n", |
65 | 65 | $method->getVisibility(), |
66 | 66 | $method->isStatic() ? 'static' : '', |
67 | - $method->returnsReference() ? '&':'', |
|
67 | + $method->returnsReference() ? '&' : '', |
|
68 | 68 | $method->getName(), |
69 | 69 | implode(', ', $this->generateArguments($method->getArguments())), |
70 | 70 | ($ret = $this->generateTypes($method->getReturnTypeNode())) ? ': '.$ret : '' |
@@ -76,13 +76,13 @@ discard block |
||
76 | 76 | |
77 | 77 | private function generateTypes(TypeNodeAbstract $typeNode): string |
78 | 78 | { |
79 | - if (!$typeNode->getTypes()) { |
|
79 | + if ( ! $typeNode->getTypes()) { |
|
80 | 80 | return ''; |
81 | 81 | } |
82 | 82 | |
83 | 83 | // When we require PHP 8 we can stop generating ?foo nullables and remove this first block |
84 | 84 | if ($typeNode->canUseNullShorthand()) { |
85 | - return sprintf( '?%s', $typeNode->getNonNullTypes()[0]); |
|
85 | + return sprintf('?%s', $typeNode->getNonNullTypes()[0]); |
|
86 | 86 | } else { |
87 | 87 | return join('|', $typeNode->getTypes()); |
88 | 88 | } |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | |
91 | 91 | private function generateArguments(array $arguments) |
92 | 92 | { |
93 | - return array_map(function (Node\ArgumentNode $argument){ |
|
93 | + return array_map(function(Node\ArgumentNode $argument) { |
|
94 | 94 | |
95 | 95 | $php = $this->generateTypes($argument->getTypeNode()); |
96 | 96 | |
@@ -100,7 +100,7 @@ discard block |
||
100 | 100 | |
101 | 101 | $php .= '$'.$argument->getName(); |
102 | 102 | |
103 | - if ($argument->isOptional() && !$argument->isVariadic()) { |
|
103 | + if ($argument->isOptional() && ! $argument->isVariadic()) { |
|
104 | 104 | $php .= ' = '.var_export($argument->getDefault(), true); |
105 | 105 | } |
106 | 106 |
@@ -31,224 +31,224 @@ |
||
31 | 31 | */ |
32 | 32 | class ClassMirror |
33 | 33 | { |
34 | - private static $reflectableMethods = array( |
|
35 | - '__construct', |
|
36 | - '__destruct', |
|
37 | - '__sleep', |
|
38 | - '__wakeup', |
|
39 | - '__toString', |
|
40 | - '__call', |
|
41 | - '__invoke' |
|
42 | - ); |
|
43 | - |
|
44 | - /** |
|
45 | - * Reflects provided arguments into class node. |
|
46 | - * |
|
47 | - * @param ReflectionClass|null $class |
|
48 | - * @param ReflectionClass[] $interfaces |
|
49 | - * |
|
50 | - * @return Node\ClassNode |
|
51 | - * |
|
52 | - */ |
|
53 | - public function reflect(?ReflectionClass $class, array $interfaces) |
|
54 | - { |
|
55 | - $node = new Node\ClassNode; |
|
56 | - |
|
57 | - if (null !== $class) { |
|
58 | - if (true === $class->isInterface()) { |
|
59 | - throw new InvalidArgumentException(sprintf( |
|
60 | - "Could not reflect %s as a class, because it\n". |
|
61 | - "is interface - use the second argument instead.", |
|
62 | - $class->getName() |
|
63 | - )); |
|
64 | - } |
|
65 | - |
|
66 | - $this->reflectClassToNode($class, $node); |
|
67 | - } |
|
68 | - |
|
69 | - foreach ($interfaces as $interface) { |
|
70 | - if (!$interface instanceof ReflectionClass) { |
|
71 | - throw new InvalidArgumentException(sprintf( |
|
72 | - "[ReflectionClass \$interface1 [, ReflectionClass \$interface2]] array expected as\n". |
|
73 | - "a second argument to `ClassMirror::reflect(...)`, but got %s.", |
|
74 | - is_object($interface) ? get_class($interface).' class' : gettype($interface) |
|
75 | - )); |
|
76 | - } |
|
77 | - if (false === $interface->isInterface()) { |
|
78 | - throw new InvalidArgumentException(sprintf( |
|
79 | - "Could not reflect %s as an interface, because it\n". |
|
80 | - "is class - use the first argument instead.", |
|
81 | - $interface->getName() |
|
82 | - )); |
|
83 | - } |
|
84 | - |
|
85 | - $this->reflectInterfaceToNode($interface, $node); |
|
86 | - } |
|
87 | - |
|
88 | - $node->addInterface('Prophecy\Doubler\Generator\ReflectionInterface'); |
|
89 | - |
|
90 | - return $node; |
|
91 | - } |
|
92 | - |
|
93 | - private function reflectClassToNode(ReflectionClass $class, Node\ClassNode $node) |
|
94 | - { |
|
95 | - if (true === $class->isFinal()) { |
|
96 | - throw new ClassMirrorException(sprintf( |
|
97 | - 'Could not reflect class %s as it is marked final.', $class->getName() |
|
98 | - ), $class); |
|
99 | - } |
|
100 | - |
|
101 | - $node->setParentClass($class->getName()); |
|
102 | - |
|
103 | - foreach ($class->getMethods(ReflectionMethod::IS_ABSTRACT) as $method) { |
|
104 | - if (false === $method->isProtected()) { |
|
105 | - continue; |
|
106 | - } |
|
107 | - |
|
108 | - $this->reflectMethodToNode($method, $node); |
|
109 | - } |
|
110 | - |
|
111 | - foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
|
112 | - if (0 === strpos($method->getName(), '_') |
|
113 | - && !in_array($method->getName(), self::$reflectableMethods)) { |
|
114 | - continue; |
|
115 | - } |
|
116 | - |
|
117 | - if (true === $method->isFinal()) { |
|
118 | - $node->addUnextendableMethod($method->getName()); |
|
119 | - continue; |
|
120 | - } |
|
121 | - |
|
122 | - $this->reflectMethodToNode($method, $node); |
|
123 | - } |
|
124 | - } |
|
125 | - |
|
126 | - private function reflectInterfaceToNode(ReflectionClass $interface, Node\ClassNode $node) |
|
127 | - { |
|
128 | - $node->addInterface($interface->getName()); |
|
129 | - |
|
130 | - foreach ($interface->getMethods() as $method) { |
|
131 | - $this->reflectMethodToNode($method, $node); |
|
132 | - } |
|
133 | - } |
|
134 | - |
|
135 | - private function reflectMethodToNode(ReflectionMethod $method, Node\ClassNode $classNode) |
|
136 | - { |
|
137 | - $node = new Node\MethodNode($method->getName()); |
|
138 | - |
|
139 | - if (true === $method->isProtected()) { |
|
140 | - $node->setVisibility('protected'); |
|
141 | - } |
|
142 | - |
|
143 | - if (true === $method->isStatic()) { |
|
144 | - $node->setStatic(); |
|
145 | - } |
|
146 | - |
|
147 | - if (true === $method->returnsReference()) { |
|
148 | - $node->setReturnsReference(); |
|
149 | - } |
|
150 | - |
|
151 | - if ($method->hasReturnType()) { |
|
152 | - $returnTypes = $this->getTypeHints($method->getReturnType(), $method->getDeclaringClass(), $method->getReturnType()->allowsNull()); |
|
153 | - $node->setReturnTypeNode(new ReturnTypeNode(...$returnTypes)); |
|
154 | - } |
|
155 | - elseif (method_exists($method, 'hasTentativeReturnType') && $method->hasTentativeReturnType()) { |
|
156 | - $returnTypes = $this->getTypeHints($method->getTentativeReturnType(), $method->getDeclaringClass(), $method->getTentativeReturnType()->allowsNull()); |
|
157 | - $node->setReturnTypeNode(new ReturnTypeNode(...$returnTypes)); |
|
158 | - } |
|
159 | - |
|
160 | - if (is_array($params = $method->getParameters()) && count($params)) { |
|
161 | - foreach ($params as $param) { |
|
162 | - $this->reflectArgumentToNode($param, $node); |
|
163 | - } |
|
164 | - } |
|
165 | - |
|
166 | - $classNode->addMethod($node); |
|
167 | - } |
|
168 | - |
|
169 | - private function reflectArgumentToNode(ReflectionParameter $parameter, Node\MethodNode $methodNode) |
|
170 | - { |
|
171 | - $name = $parameter->getName() == '...' ? '__dot_dot_dot__' : $parameter->getName(); |
|
172 | - $node = new Node\ArgumentNode($name); |
|
173 | - |
|
174 | - $typeHints = $this->getTypeHints($parameter->getType(), $parameter->getDeclaringClass(), $parameter->allowsNull()); |
|
175 | - |
|
176 | - $node->setTypeNode(new ArgumentTypeNode(...$typeHints)); |
|
177 | - |
|
178 | - if ($parameter->isVariadic()) { |
|
179 | - $node->setAsVariadic(); |
|
180 | - } |
|
181 | - |
|
182 | - if ($this->hasDefaultValue($parameter)) { |
|
183 | - $node->setDefault($this->getDefaultValue($parameter)); |
|
184 | - } |
|
185 | - |
|
186 | - if ($parameter->isPassedByReference()) { |
|
187 | - $node->setAsPassedByReference(); |
|
188 | - } |
|
189 | - |
|
190 | - |
|
191 | - $methodNode->addArgument($node); |
|
192 | - } |
|
193 | - |
|
194 | - private function hasDefaultValue(ReflectionParameter $parameter) |
|
195 | - { |
|
196 | - if ($parameter->isVariadic()) { |
|
197 | - return false; |
|
198 | - } |
|
199 | - |
|
200 | - if ($parameter->isDefaultValueAvailable()) { |
|
201 | - return true; |
|
202 | - } |
|
203 | - |
|
204 | - return $parameter->isOptional() || ($parameter->allowsNull() && $parameter->getType() && \PHP_VERSION_ID < 80100); |
|
205 | - } |
|
206 | - |
|
207 | - private function getDefaultValue(ReflectionParameter $parameter) |
|
208 | - { |
|
209 | - if (!$parameter->isDefaultValueAvailable()) { |
|
210 | - return null; |
|
211 | - } |
|
212 | - |
|
213 | - return $parameter->getDefaultValue(); |
|
214 | - } |
|
215 | - |
|
216 | - private function getTypeHints(?ReflectionType $type, ?ReflectionClass $class, bool $allowsNull) : array |
|
217 | - { |
|
218 | - $types = []; |
|
219 | - |
|
220 | - if ($type instanceof ReflectionNamedType) { |
|
221 | - $types = [$type->getName()]; |
|
222 | - |
|
223 | - } |
|
224 | - elseif ($type instanceof ReflectionUnionType) { |
|
225 | - $types = $type->getTypes(); |
|
226 | - } |
|
227 | - elseif ($type instanceof ReflectionIntersectionType) { |
|
228 | - throw new ClassMirrorException('Doubling intersection types is not supported', $class); |
|
229 | - } |
|
230 | - elseif(is_object($type)) { |
|
231 | - throw new ClassMirrorException('Unknown reflection type ' . get_class($type), $class); |
|
232 | - } |
|
233 | - |
|
234 | - $types = array_map( |
|
235 | - function(string $type) use ($class) { |
|
236 | - if ($type === 'self') { |
|
237 | - return $class->getName(); |
|
238 | - } |
|
239 | - if ($type === 'parent') { |
|
240 | - return $class->getParentClass()->getName(); |
|
241 | - } |
|
242 | - |
|
243 | - return $type; |
|
244 | - }, |
|
245 | - $types |
|
246 | - ); |
|
247 | - |
|
248 | - if ($types && $types != ['mixed'] && $allowsNull) { |
|
249 | - $types[] = 'null'; |
|
250 | - } |
|
251 | - |
|
252 | - return $types; |
|
253 | - } |
|
34 | + private static $reflectableMethods = array( |
|
35 | + '__construct', |
|
36 | + '__destruct', |
|
37 | + '__sleep', |
|
38 | + '__wakeup', |
|
39 | + '__toString', |
|
40 | + '__call', |
|
41 | + '__invoke' |
|
42 | + ); |
|
43 | + |
|
44 | + /** |
|
45 | + * Reflects provided arguments into class node. |
|
46 | + * |
|
47 | + * @param ReflectionClass|null $class |
|
48 | + * @param ReflectionClass[] $interfaces |
|
49 | + * |
|
50 | + * @return Node\ClassNode |
|
51 | + * |
|
52 | + */ |
|
53 | + public function reflect(?ReflectionClass $class, array $interfaces) |
|
54 | + { |
|
55 | + $node = new Node\ClassNode; |
|
56 | + |
|
57 | + if (null !== $class) { |
|
58 | + if (true === $class->isInterface()) { |
|
59 | + throw new InvalidArgumentException(sprintf( |
|
60 | + "Could not reflect %s as a class, because it\n". |
|
61 | + "is interface - use the second argument instead.", |
|
62 | + $class->getName() |
|
63 | + )); |
|
64 | + } |
|
65 | + |
|
66 | + $this->reflectClassToNode($class, $node); |
|
67 | + } |
|
68 | + |
|
69 | + foreach ($interfaces as $interface) { |
|
70 | + if (!$interface instanceof ReflectionClass) { |
|
71 | + throw new InvalidArgumentException(sprintf( |
|
72 | + "[ReflectionClass \$interface1 [, ReflectionClass \$interface2]] array expected as\n". |
|
73 | + "a second argument to `ClassMirror::reflect(...)`, but got %s.", |
|
74 | + is_object($interface) ? get_class($interface).' class' : gettype($interface) |
|
75 | + )); |
|
76 | + } |
|
77 | + if (false === $interface->isInterface()) { |
|
78 | + throw new InvalidArgumentException(sprintf( |
|
79 | + "Could not reflect %s as an interface, because it\n". |
|
80 | + "is class - use the first argument instead.", |
|
81 | + $interface->getName() |
|
82 | + )); |
|
83 | + } |
|
84 | + |
|
85 | + $this->reflectInterfaceToNode($interface, $node); |
|
86 | + } |
|
87 | + |
|
88 | + $node->addInterface('Prophecy\Doubler\Generator\ReflectionInterface'); |
|
89 | + |
|
90 | + return $node; |
|
91 | + } |
|
92 | + |
|
93 | + private function reflectClassToNode(ReflectionClass $class, Node\ClassNode $node) |
|
94 | + { |
|
95 | + if (true === $class->isFinal()) { |
|
96 | + throw new ClassMirrorException(sprintf( |
|
97 | + 'Could not reflect class %s as it is marked final.', $class->getName() |
|
98 | + ), $class); |
|
99 | + } |
|
100 | + |
|
101 | + $node->setParentClass($class->getName()); |
|
102 | + |
|
103 | + foreach ($class->getMethods(ReflectionMethod::IS_ABSTRACT) as $method) { |
|
104 | + if (false === $method->isProtected()) { |
|
105 | + continue; |
|
106 | + } |
|
107 | + |
|
108 | + $this->reflectMethodToNode($method, $node); |
|
109 | + } |
|
110 | + |
|
111 | + foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
|
112 | + if (0 === strpos($method->getName(), '_') |
|
113 | + && !in_array($method->getName(), self::$reflectableMethods)) { |
|
114 | + continue; |
|
115 | + } |
|
116 | + |
|
117 | + if (true === $method->isFinal()) { |
|
118 | + $node->addUnextendableMethod($method->getName()); |
|
119 | + continue; |
|
120 | + } |
|
121 | + |
|
122 | + $this->reflectMethodToNode($method, $node); |
|
123 | + } |
|
124 | + } |
|
125 | + |
|
126 | + private function reflectInterfaceToNode(ReflectionClass $interface, Node\ClassNode $node) |
|
127 | + { |
|
128 | + $node->addInterface($interface->getName()); |
|
129 | + |
|
130 | + foreach ($interface->getMethods() as $method) { |
|
131 | + $this->reflectMethodToNode($method, $node); |
|
132 | + } |
|
133 | + } |
|
134 | + |
|
135 | + private function reflectMethodToNode(ReflectionMethod $method, Node\ClassNode $classNode) |
|
136 | + { |
|
137 | + $node = new Node\MethodNode($method->getName()); |
|
138 | + |
|
139 | + if (true === $method->isProtected()) { |
|
140 | + $node->setVisibility('protected'); |
|
141 | + } |
|
142 | + |
|
143 | + if (true === $method->isStatic()) { |
|
144 | + $node->setStatic(); |
|
145 | + } |
|
146 | + |
|
147 | + if (true === $method->returnsReference()) { |
|
148 | + $node->setReturnsReference(); |
|
149 | + } |
|
150 | + |
|
151 | + if ($method->hasReturnType()) { |
|
152 | + $returnTypes = $this->getTypeHints($method->getReturnType(), $method->getDeclaringClass(), $method->getReturnType()->allowsNull()); |
|
153 | + $node->setReturnTypeNode(new ReturnTypeNode(...$returnTypes)); |
|
154 | + } |
|
155 | + elseif (method_exists($method, 'hasTentativeReturnType') && $method->hasTentativeReturnType()) { |
|
156 | + $returnTypes = $this->getTypeHints($method->getTentativeReturnType(), $method->getDeclaringClass(), $method->getTentativeReturnType()->allowsNull()); |
|
157 | + $node->setReturnTypeNode(new ReturnTypeNode(...$returnTypes)); |
|
158 | + } |
|
159 | + |
|
160 | + if (is_array($params = $method->getParameters()) && count($params)) { |
|
161 | + foreach ($params as $param) { |
|
162 | + $this->reflectArgumentToNode($param, $node); |
|
163 | + } |
|
164 | + } |
|
165 | + |
|
166 | + $classNode->addMethod($node); |
|
167 | + } |
|
168 | + |
|
169 | + private function reflectArgumentToNode(ReflectionParameter $parameter, Node\MethodNode $methodNode) |
|
170 | + { |
|
171 | + $name = $parameter->getName() == '...' ? '__dot_dot_dot__' : $parameter->getName(); |
|
172 | + $node = new Node\ArgumentNode($name); |
|
173 | + |
|
174 | + $typeHints = $this->getTypeHints($parameter->getType(), $parameter->getDeclaringClass(), $parameter->allowsNull()); |
|
175 | + |
|
176 | + $node->setTypeNode(new ArgumentTypeNode(...$typeHints)); |
|
177 | + |
|
178 | + if ($parameter->isVariadic()) { |
|
179 | + $node->setAsVariadic(); |
|
180 | + } |
|
181 | + |
|
182 | + if ($this->hasDefaultValue($parameter)) { |
|
183 | + $node->setDefault($this->getDefaultValue($parameter)); |
|
184 | + } |
|
185 | + |
|
186 | + if ($parameter->isPassedByReference()) { |
|
187 | + $node->setAsPassedByReference(); |
|
188 | + } |
|
189 | + |
|
190 | + |
|
191 | + $methodNode->addArgument($node); |
|
192 | + } |
|
193 | + |
|
194 | + private function hasDefaultValue(ReflectionParameter $parameter) |
|
195 | + { |
|
196 | + if ($parameter->isVariadic()) { |
|
197 | + return false; |
|
198 | + } |
|
199 | + |
|
200 | + if ($parameter->isDefaultValueAvailable()) { |
|
201 | + return true; |
|
202 | + } |
|
203 | + |
|
204 | + return $parameter->isOptional() || ($parameter->allowsNull() && $parameter->getType() && \PHP_VERSION_ID < 80100); |
|
205 | + } |
|
206 | + |
|
207 | + private function getDefaultValue(ReflectionParameter $parameter) |
|
208 | + { |
|
209 | + if (!$parameter->isDefaultValueAvailable()) { |
|
210 | + return null; |
|
211 | + } |
|
212 | + |
|
213 | + return $parameter->getDefaultValue(); |
|
214 | + } |
|
215 | + |
|
216 | + private function getTypeHints(?ReflectionType $type, ?ReflectionClass $class, bool $allowsNull) : array |
|
217 | + { |
|
218 | + $types = []; |
|
219 | + |
|
220 | + if ($type instanceof ReflectionNamedType) { |
|
221 | + $types = [$type->getName()]; |
|
222 | + |
|
223 | + } |
|
224 | + elseif ($type instanceof ReflectionUnionType) { |
|
225 | + $types = $type->getTypes(); |
|
226 | + } |
|
227 | + elseif ($type instanceof ReflectionIntersectionType) { |
|
228 | + throw new ClassMirrorException('Doubling intersection types is not supported', $class); |
|
229 | + } |
|
230 | + elseif(is_object($type)) { |
|
231 | + throw new ClassMirrorException('Unknown reflection type ' . get_class($type), $class); |
|
232 | + } |
|
233 | + |
|
234 | + $types = array_map( |
|
235 | + function(string $type) use ($class) { |
|
236 | + if ($type === 'self') { |
|
237 | + return $class->getName(); |
|
238 | + } |
|
239 | + if ($type === 'parent') { |
|
240 | + return $class->getParentClass()->getName(); |
|
241 | + } |
|
242 | + |
|
243 | + return $type; |
|
244 | + }, |
|
245 | + $types |
|
246 | + ); |
|
247 | + |
|
248 | + if ($types && $types != ['mixed'] && $allowsNull) { |
|
249 | + $types[] = 'null'; |
|
250 | + } |
|
251 | + |
|
252 | + return $types; |
|
253 | + } |
|
254 | 254 | } |
@@ -67,7 +67,7 @@ discard block |
||
67 | 67 | } |
68 | 68 | |
69 | 69 | foreach ($interfaces as $interface) { |
70 | - if (!$interface instanceof ReflectionClass) { |
|
70 | + if ( ! $interface instanceof ReflectionClass) { |
|
71 | 71 | throw new InvalidArgumentException(sprintf( |
72 | 72 | "[ReflectionClass \$interface1 [, ReflectionClass \$interface2]] array expected as\n". |
73 | 73 | "a second argument to `ClassMirror::reflect(...)`, but got %s.", |
@@ -110,7 +110,7 @@ discard block |
||
110 | 110 | |
111 | 111 | foreach ($class->getMethods(ReflectionMethod::IS_PUBLIC) as $method) { |
112 | 112 | if (0 === strpos($method->getName(), '_') |
113 | - && !in_array($method->getName(), self::$reflectableMethods)) { |
|
113 | + && ! in_array($method->getName(), self::$reflectableMethods)) { |
|
114 | 114 | continue; |
115 | 115 | } |
116 | 116 | |
@@ -206,7 +206,7 @@ discard block |
||
206 | 206 | |
207 | 207 | private function getDefaultValue(ReflectionParameter $parameter) |
208 | 208 | { |
209 | - if (!$parameter->isDefaultValueAvailable()) { |
|
209 | + if ( ! $parameter->isDefaultValueAvailable()) { |
|
210 | 210 | return null; |
211 | 211 | } |
212 | 212 | |
@@ -227,8 +227,8 @@ discard block |
||
227 | 227 | elseif ($type instanceof ReflectionIntersectionType) { |
228 | 228 | throw new ClassMirrorException('Doubling intersection types is not supported', $class); |
229 | 229 | } |
230 | - elseif(is_object($type)) { |
|
231 | - throw new ClassMirrorException('Unknown reflection type ' . get_class($type), $class); |
|
230 | + elseif (is_object($type)) { |
|
231 | + throw new ClassMirrorException('Unknown reflection type '.get_class($type), $class); |
|
232 | 232 | } |
233 | 233 | |
234 | 234 | $types = array_map( |
@@ -151,8 +151,7 @@ discard block |
||
151 | 151 | if ($method->hasReturnType()) { |
152 | 152 | $returnTypes = $this->getTypeHints($method->getReturnType(), $method->getDeclaringClass(), $method->getReturnType()->allowsNull()); |
153 | 153 | $node->setReturnTypeNode(new ReturnTypeNode(...$returnTypes)); |
154 | - } |
|
155 | - elseif (method_exists($method, 'hasTentativeReturnType') && $method->hasTentativeReturnType()) { |
|
154 | + } elseif (method_exists($method, 'hasTentativeReturnType') && $method->hasTentativeReturnType()) { |
|
156 | 155 | $returnTypes = $this->getTypeHints($method->getTentativeReturnType(), $method->getDeclaringClass(), $method->getTentativeReturnType()->allowsNull()); |
157 | 156 | $node->setReturnTypeNode(new ReturnTypeNode(...$returnTypes)); |
158 | 157 | } |
@@ -220,14 +219,11 @@ discard block |
||
220 | 219 | if ($type instanceof ReflectionNamedType) { |
221 | 220 | $types = [$type->getName()]; |
222 | 221 | |
223 | - } |
|
224 | - elseif ($type instanceof ReflectionUnionType) { |
|
222 | + } elseif ($type instanceof ReflectionUnionType) { |
|
225 | 223 | $types = $type->getTypes(); |
226 | - } |
|
227 | - elseif ($type instanceof ReflectionIntersectionType) { |
|
224 | + } elseif ($type instanceof ReflectionIntersectionType) { |
|
228 | 225 | throw new ClassMirrorException('Doubling intersection types is not supported', $class); |
229 | - } |
|
230 | - elseif(is_object($type)) { |
|
226 | + } elseif(is_object($type)) { |
|
231 | 227 | throw new ClassMirrorException('Unknown reflection type ' . get_class($type), $class); |
232 | 228 | } |
233 | 229 |