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