Conditions | 26 |
Paths | 216 |
Total Lines | 181 |
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 /** @noinspection PhpUndefinedClassInspection */ |
||
178 | public function processFile(string $file, string $contents) |
||
179 | { |
||
180 | $this->getLogger()->debug("Processing `$file`"); |
||
181 | $file = realpath($file); |
||
182 | |||
183 | self::$file = $file; |
||
184 | |||
185 | $ignoreFile = sprintf('%s/.signalignore', dirname($file)); |
||
186 | |||
187 | if (file_exists($ignoreFile)) |
||
188 | { |
||
189 | $this->getLogger()->notice("Skipping `$file` because of `$ignoreFile`" . PHP_EOL); |
||
190 | return; |
||
191 | } |
||
192 | |||
193 | $this->paths[] = $file; |
||
194 | // Remove initial `\` from namespace |
||
195 | try |
||
196 | { |
||
197 | $annotated = AnnotationUtility::rawAnnotate($file); |
||
198 | } |
||
199 | catch(NoClassInFileException $e) |
||
200 | { |
||
201 | $this->log($e, $file); |
||
202 | return; |
||
203 | } |
||
204 | catch (ClassNotFoundException $e) |
||
205 | { |
||
206 | $this->log($e, $file); |
||
207 | return; |
||
208 | } |
||
209 | catch (ParseException $e) |
||
210 | { |
||
211 | $this->err($e, $file); |
||
212 | return; |
||
213 | } |
||
214 | catch (UnexpectedValueException $e) |
||
215 | { |
||
216 | $this->err($e, $file); |
||
217 | return; |
||
218 | } |
||
219 | catch (Exception $e) |
||
220 | { |
||
221 | $this->err($e, $file); |
||
222 | return; |
||
223 | } |
||
224 | |||
225 | $namespace = preg_replace('~^\\\\+~', '', $annotated['namespace']); |
||
226 | $className = $annotated['className']; |
||
227 | |||
228 | |||
229 | // Use fully qualified name, class must autoload |
||
230 | $fqn = $namespace . '\\' . $className; |
||
231 | NameNormalizer::normalize($fqn); |
||
232 | |||
233 | try |
||
234 | { |
||
235 | // NOTE: This autoloader must be registered on ReflectionClass |
||
236 | // creation ONLY! That's why register/unregister. |
||
237 | // This will detect not found depending classes |
||
238 | // (base classes,interfaces,traits etc.) |
||
239 | $autoload = static::class . '::autoloadHandler'; |
||
240 | spl_autoload_register($autoload); |
||
241 | eval('$info = new ReflectionClass($fqn);'); |
||
242 | spl_autoload_unregister($autoload); |
||
243 | } |
||
244 | catch (ParseError $e) |
||
245 | { |
||
246 | $this->err($e, $file); |
||
247 | return; |
||
248 | } |
||
249 | catch (ClassNotFoundException $e) |
||
250 | { |
||
251 | $this->log($e, $file); |
||
252 | return; |
||
253 | } |
||
254 | catch (ReflectionException $e) |
||
255 | { |
||
256 | $this->err($e, $file); |
||
257 | return; |
||
258 | } |
||
259 | // $info is created in `eval` |
||
260 | /* @var $info ReflectionClass */ |
||
261 | $isAnnotated = $info->implementsInterface(AnnotatedInterface::class); |
||
262 | $hasSignals = $this->hasSignals($contents); |
||
263 | $isAbstract = $info->isAbstract() || $info->isInterface(); |
||
264 | |||
265 | if ($isAnnotated) |
||
266 | { |
||
267 | $this->getLogger()->debug("Annotated: $info->name"); |
||
268 | } |
||
269 | else |
||
270 | { |
||
271 | $this->getLogger()->debug("Not annotated: $info->name"); |
||
272 | } |
||
273 | |||
274 | // Old classes must now implement interface |
||
275 | // Brake BC! |
||
276 | if ($hasSignals && !$isAnnotated && !$isAbstract) |
||
277 | { |
||
278 | throw new UnexpectedValueException(sprintf('Class %s must implement %s to use signals', $fqn, AnnotatedInterface::class)); |
||
279 | } |
||
280 | |||
281 | // Skip not annotated class |
||
282 | if (!$isAnnotated) |
||
283 | { |
||
284 | return; |
||
285 | } |
||
286 | |||
287 | // Skip abstract classes |
||
288 | if ($isAbstract) |
||
289 | { |
||
290 | return; |
||
291 | } |
||
292 | try |
||
293 | { |
||
294 | // Discard notices (might be the case when outdated cache?) |
||
295 | $level = error_reporting(); |
||
296 | error_reporting(E_WARNING); |
||
297 | $meta = SignalsMeta::create($fqn); |
||
298 | error_reporting($level); |
||
299 | } |
||
300 | catch (ParseException $e) |
||
301 | { |
||
302 | $this->err($e, $file); |
||
303 | return; |
||
304 | } |
||
305 | catch (ClassNotFoundException $e) |
||
306 | { |
||
307 | $this->log($e, $file); |
||
308 | return; |
||
309 | } |
||
310 | catch (UnexpectedValueException $e) |
||
311 | { |
||
312 | $this->err($e, $file); |
||
313 | return; |
||
314 | } |
||
315 | |||
316 | /* @var $typeMeta DocumentTypeMeta */ |
||
317 | $typeMeta = $meta->type(); |
||
318 | |||
319 | // Signals |
||
320 | foreach ($typeMeta->signalFor as $slot) |
||
321 | { |
||
322 | $this->getLogger()->debug("Signal: $slot:$fqn"); |
||
323 | $this->data[Signal::Slots][$slot][$fqn] = true; |
||
324 | } |
||
325 | |||
326 | // Slots |
||
327 | // For constructor injection |
||
328 | foreach ($typeMeta->slotFor as $slot) |
||
329 | { |
||
330 | $key = implode('::', [$fqn, '__construct']) . '()'; |
||
331 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
332 | $this->data[Signal::Signals][$slot][$fqn][$key] = true; |
||
333 | } |
||
334 | |||
335 | // For method injection |
||
336 | foreach ($meta->methods() as $methodName => $method) |
||
337 | { |
||
338 | /* @var $method DocumentMethodMeta */ |
||
339 | foreach ($method->slotFor as $slot) |
||
340 | { |
||
341 | $key = implode('::', [$fqn, $methodName]) . '()'; |
||
342 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
343 | $this->data[Signal::Signals][$slot][$fqn][$key] = sprintf('%s()', $methodName); |
||
344 | } |
||
345 | } |
||
346 | |||
347 | // For property injection |
||
348 | foreach ($meta->fields() as $fieldName => $field) |
||
349 | { |
||
350 | /* @var $field DocumentPropertyMeta */ |
||
351 | foreach ($field->slotFor as $slot) |
||
352 | { |
||
353 | $key = implode('::$', [$fqn, $fieldName]); |
||
354 | $this->getLogger()->debug("Slot: $slot:$fqn$key"); |
||
355 | $this->data[Signal::Signals][$slot][$fqn][$key] = sprintf('%s', $fieldName); |
||
356 | } |
||
357 | } |
||
358 | } |
||
359 | |||
404 |