Total Lines | 219 |
Code Lines | 87 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
86 | private function createInternalVisitor(SplObjectStorage $storage, Scope $scope) : Visitor |
||
87 | { |
||
88 | return new class ( |
||
89 | $this->metadataCollection, |
||
90 | $this->referenceResolver, |
||
91 | $this->constructor, |
||
92 | $this->classReflectionProvider, |
||
93 | $this->referenceAcceptor, |
||
94 | $this->constantResolver, |
||
95 | $scope, |
||
96 | $storage |
||
97 | ) implements Visitor { |
||
98 | /** @var MetadataCollection */ |
||
99 | private $metadataCollection; |
||
100 | |||
101 | /** @var ReferenceResolver */ |
||
102 | private $referenceResolver; |
||
103 | |||
104 | /** @var Constructor */ |
||
105 | private $constructor; |
||
106 | |||
107 | /** @var ClassReflectionProvider */ |
||
108 | private $classReflectionProvider; |
||
109 | |||
110 | /** @var ReferenceAcceptor */ |
||
111 | private $referenceAcceptor; |
||
112 | |||
113 | /** @var ConstantResolver */ |
||
114 | private $constantResolver; |
||
115 | |||
116 | /** @var Scope */ |
||
117 | private $scope; |
||
118 | |||
119 | /** @var SplObjectStorage<object> */ |
||
120 | private $storage; |
||
121 | |||
122 | /** @var SplStack<mixed> */ |
||
123 | private $stack; |
||
124 | |||
125 | 9 | public function __construct( |
|
126 | MetadataCollection $metadataCollection, |
||
127 | ReferenceResolver $referenceResolver, |
||
128 | Constructor $constructor, |
||
129 | ClassReflectionProvider $classReflectionProvider, |
||
130 | ReferenceAcceptor $referenceAcceptor, |
||
131 | ConstantResolver $constantResolver, |
||
132 | Scope $scope, |
||
133 | SplObjectStorage $storage |
||
134 | ) { |
||
135 | 9 | $this->metadataCollection = $metadataCollection; |
|
136 | 9 | $this->referenceResolver = $referenceResolver; |
|
137 | 9 | $this->constructor = $constructor; |
|
138 | 9 | $this->classReflectionProvider = $classReflectionProvider; |
|
139 | 9 | $this->referenceAcceptor = $referenceAcceptor; |
|
140 | 9 | $this->constantResolver = $constantResolver; |
|
141 | 9 | $this->scope = $scope; |
|
142 | 9 | $this->storage = $storage; |
|
143 | 9 | $this->stack = new SplStack(); |
|
144 | 9 | } |
|
145 | |||
146 | 9 | public function visitAnnotations(Annotations $annotations) : void |
|
147 | { |
||
148 | 9 | foreach ($annotations as $annotation) { |
|
149 | 9 | $stackSize = $this->stack->count(); |
|
150 | |||
151 | 9 | $annotation->dispatch($this); |
|
152 | |||
153 | 9 | if ($this->stack->count() === $stackSize) { |
|
154 | 5 | continue; |
|
155 | } |
||
156 | |||
157 | 9 | $this->storage->attach($this->stack->pop()); |
|
158 | } |
||
159 | |||
160 | 9 | assert($this->stack->isEmpty()); |
|
161 | 9 | } |
|
162 | |||
163 | 9 | public function visitAnnotation(Annotation $annotation) : void |
|
164 | { |
||
165 | 9 | if (! $this->referenceAcceptor->accepts($annotation->getName(), $this->scope)) { |
|
166 | 5 | return; |
|
167 | } |
||
168 | |||
169 | 9 | $this->scope->increaseNestingLevel(); |
|
170 | |||
171 | 9 | $annotation->getParameters()->dispatch($this); |
|
172 | 9 | $annotation->getName()->dispatch($this); |
|
173 | |||
174 | 9 | $this->stack->push( |
|
175 | 9 | $this->constructor->construct( |
|
176 | 9 | $this->metadataCollection[$this->stack->pop()], |
|
177 | 9 | $this->scope, |
|
178 | 9 | $this->stack->pop() |
|
179 | ) |
||
180 | ); |
||
181 | |||
182 | 9 | $this->scope->decreaseNestingLevel(); |
|
183 | 9 | } |
|
184 | |||
185 | 9 | public function visitReference(Reference $reference) : void |
|
186 | { |
||
187 | 9 | $this->stack->push($this->referenceResolver->resolve($reference, $this->scope)); |
|
188 | 9 | } |
|
189 | |||
190 | 9 | public function visitParameters(Parameters $parameters) : void |
|
191 | { |
||
192 | 9 | $new = []; |
|
193 | |||
194 | 9 | foreach ($parameters as $parameter) { |
|
195 | 9 | $parameter->dispatch($this); |
|
196 | |||
197 | 9 | assert(! array_key_exists($this->stack->current(), $new)); |
|
198 | |||
199 | 9 | $new[$this->stack->pop()] = $this->stack->pop(); |
|
200 | } |
||
201 | |||
202 | 9 | $this->stack->push($new); |
|
203 | 9 | } |
|
204 | |||
205 | 6 | public function visitUnnamedParameter(UnnamedParameter $parameter) : void |
|
206 | { |
||
207 | 6 | $parameter->getValue()->dispatch($this); |
|
208 | |||
209 | 6 | $this->stack->push($this->stack->pop()); |
|
210 | 6 | $this->stack->push(null); |
|
211 | 6 | } |
|
212 | |||
213 | 4 | public function visitNamedParameter(NamedParameter $parameter) : void |
|
214 | { |
||
215 | 4 | $parameter->getValue()->dispatch($this); |
|
216 | 4 | $parameter->getName()->dispatch($this); |
|
217 | // pass through |
||
218 | 4 | } |
|
219 | |||
220 | 4 | public function visitIdentifier(Identifier $identifier) : void |
|
221 | { |
||
222 | 4 | $this->stack->push($identifier->getValue()); |
|
223 | 4 | } |
|
224 | |||
225 | 1 | public function visitPair(Pair $pair) : void |
|
226 | { |
||
227 | 1 | $pair->getValue()->dispatch($this); |
|
228 | 1 | $pair->getKey()->dispatch($this); |
|
229 | // pass through |
||
230 | 1 | } |
|
231 | |||
232 | 1 | public function visitBooleanScalar(BooleanScalar $booleanScalar) : void |
|
233 | { |
||
234 | 1 | $this->stack->push($booleanScalar->getValue()); |
|
235 | 1 | } |
|
236 | |||
237 | 3 | public function visitIntegerScalar(IntegerScalar $integerScalar) : void |
|
238 | { |
||
239 | 3 | $this->stack->push($integerScalar->getValue()); |
|
240 | 3 | } |
|
241 | |||
242 | 1 | public function visitFloatScalar(FloatScalar $floatScalar) : void |
|
243 | { |
||
244 | 1 | $this->stack->push($floatScalar->getValue()); |
|
245 | 1 | } |
|
246 | |||
247 | 7 | public function visitStringScalar(StringScalar $stringScalar) : void |
|
248 | { |
||
249 | 7 | $this->stack->push($stringScalar->getValue()); |
|
250 | 7 | } |
|
251 | |||
252 | 1 | public function visitNullScalar(NullScalar $nullScalar) : void |
|
253 | { |
||
254 | 1 | $this->stack->push($nullScalar->getValue()); |
|
255 | 1 | } |
|
256 | |||
257 | 3 | public function visitListCollection(ListCollection $listCollection) : void |
|
258 | { |
||
259 | 3 | $list = []; |
|
260 | |||
261 | 3 | foreach ($listCollection as $listItem) { |
|
262 | 3 | $listItem->dispatch($this); |
|
263 | |||
264 | 3 | $list[] = $this->stack->pop(); |
|
265 | } |
||
266 | |||
267 | 3 | $this->stack->push($list); |
|
268 | 3 | } |
|
269 | |||
270 | 1 | public function visitMapCollection(MapCollection $mapCollection) : void |
|
271 | { |
||
272 | 1 | $map = []; |
|
273 | |||
274 | 1 | foreach ($mapCollection as $mapItem) { |
|
275 | 1 | $mapItem->dispatch($this); |
|
276 | 1 | $map[$this->stack->pop()] = $this->stack->pop(); |
|
277 | } |
||
278 | |||
279 | 1 | $this->stack->push($map); |
|
280 | 1 | } |
|
281 | |||
282 | public function visitConstantFetch(ConstantFetch $constantFetch) : void |
||
283 | { |
||
284 | $constantFetch->getName()->dispatch($this); |
||
285 | |||
286 | $constantName = $this->stack->pop(); |
||
287 | |||
288 | $this->stack->push($this->constantResolver->resolveStandaloneConstant($constantName)); |
||
289 | } |
||
290 | |||
291 | 1 | public function visitClassConstantFetch(ClassConstantFetch $classConstantFetch) : void |
|
292 | { |
||
293 | 1 | $classConstantFetch->getName()->dispatch($this); |
|
294 | 1 | $classConstantFetch->getClass()->dispatch($this); |
|
295 | |||
296 | 1 | $className = $this->stack->pop(); |
|
297 | 1 | $constantName = $this->stack->pop(); |
|
298 | |||
299 | 1 | if (strtolower($constantName) === 'class') { |
|
300 | $this->stack->push($className); |
||
301 | return; |
||
302 | } |
||
303 | |||
304 | 1 | $this->stack->push($this->constantResolver->resolveClassOrInterfaceConstant($className, $constantName)); |
|
305 | 1 | } |
|
309 |