Complex classes like Signal often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Signal, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
43 | class Signal implements LoggerAwareInterface |
||
44 | { |
||
45 | |||
46 | const Slots = 'slots'; |
||
47 | const Signals = 'signals'; |
||
48 | |||
49 | /** |
||
50 | * Generated signals name. |
||
51 | * Name of this constant is confusing. |
||
52 | * @internal description |
||
53 | */ |
||
54 | const ConfigFilename = 'signals-definition.php'; |
||
55 | |||
56 | /** |
||
57 | * Config file name |
||
58 | */ |
||
59 | const ConfigName = "signals"; |
||
60 | |||
61 | /** |
||
62 | * Runtime path is directory where config cache from yml file will |
||
63 | * be stored. Path is relative to project root. This must be writable |
||
64 | * by command line user. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | public $runtimePath = 'runtime'; |
||
69 | |||
70 | /** |
||
71 | * This paths will be searched for `SlotFor` and `SignalFor` annotations. |
||
72 | * |
||
73 | * |
||
74 | * |
||
75 | * TODO Autodetect based on composer autoload |
||
76 | * |
||
77 | * @var string[] |
||
78 | */ |
||
79 | public $paths = [ |
||
80 | 'vendor', |
||
81 | ]; |
||
82 | |||
83 | /** |
||
84 | * Directories to ignore while scanning |
||
85 | * @var string[] |
||
86 | */ |
||
87 | public $ignoreDirs = [ |
||
88 | 'vendor', // Vendors in vendors |
||
89 | 'generated', // Generated data, including signals |
||
90 | 'runtime', // Runtime data |
||
91 | ]; |
||
92 | |||
93 | /** |
||
94 | * Filters configuration. |
||
95 | * This filters will be applied to every emit. This property |
||
96 | * should contain array of class names implementing filters. |
||
97 | * @var string[]|object[] |
||
98 | */ |
||
99 | public $filters = []; |
||
100 | |||
101 | /** |
||
102 | * Sorters configuration. |
||
103 | * @var string[]|object[] |
||
104 | */ |
||
105 | public $sorters = []; |
||
106 | |||
107 | /** |
||
108 | * Extractor configuration |
||
109 | * @var string|[]|object |
||
110 | */ |
||
111 | public $extractor = Addendum::class; |
||
112 | |||
113 | /** |
||
114 | * Input/Output configuration, at minimum it should |
||
115 | * contain class name for builder input output interface. |
||
116 | * It can also contain array [configurable options for IO class](php-io/). |
||
117 | * |
||
118 | * |
||
119 | * |
||
120 | * @var string|[]|object |
||
121 | */ |
||
122 | public $io = PhpFile::class; |
||
123 | |||
124 | /** |
||
125 | * Whenever component is initialized |
||
126 | * @var bool |
||
127 | */ |
||
128 | private $isInitialized = false; |
||
129 | |||
130 | /** |
||
131 | * Configuration of signals and slots |
||
132 | * @var string[][] |
||
133 | */ |
||
134 | private static $config = []; |
||
135 | |||
136 | /** |
||
137 | * Logger instance holder |
||
138 | * NOTE: There is property annotation with `logger` name, |
||
139 | * thus this name is a bit longer |
||
140 | * @var LoggerInterface |
||
141 | */ |
||
142 | private $loggerInstance = null; |
||
143 | |||
144 | /** |
||
145 | * Embedded dependency injection |
||
146 | * @var EmbeDi |
||
147 | */ |
||
148 | private $di = null; |
||
149 | |||
150 | /** |
||
151 | * Version |
||
152 | * @var string |
||
153 | */ |
||
154 | private $version = null; |
||
155 | |||
156 | /** |
||
157 | * Current filters |
||
158 | * @var PreFilterInterface[]|PostFilterInterface[] |
||
159 | */ |
||
160 | private $currentFilters = []; |
||
161 | |||
162 | 25 | public function __construct($configName = self::ConfigName) |
|
174 | |||
175 | /** |
||
176 | * Getter |
||
177 | * @param string $name |
||
178 | * @return mixed |
||
179 | */ |
||
180 | public function __get($name) |
||
184 | |||
185 | /** |
||
186 | * Setter |
||
187 | * @param string $name |
||
188 | * @param mixed $value |
||
189 | * @return mixed |
||
190 | */ |
||
191 | public function __set($name, $value) |
||
195 | |||
196 | /** |
||
197 | * Get current signals version |
||
198 | * |
||
199 | * @codeCoverageIgnore |
||
200 | * @return string |
||
201 | */ |
||
202 | public function getVersion() |
||
210 | |||
211 | public function init() |
||
212 | { |
||
213 | if (!$this->isInitialized) |
||
214 | { |
||
215 | $this->reload(); |
||
216 | } |
||
217 | if (!$this->di->isStored($this)) |
||
218 | { |
||
219 | $this->di->store($this); |
||
220 | } |
||
221 | } |
||
222 | |||
223 | /** |
||
224 | * Apply filter to current emit. |
||
225 | * |
||
226 | * Pass false as param to disable all filters. |
||
227 | * |
||
228 | * @param FilterInterface|string|mixed $filter |
||
229 | * @return Signal |
||
230 | * @throws UnexpectedValueException |
||
231 | */ |
||
232 | 4 | public function filter($filter) |
|
233 | { |
||
234 | // disable filters |
||
235 | 4 | if (is_bool($filter) && false === $filter) |
|
236 | 4 | { |
|
237 | $this->currentFilters = []; |
||
238 | return $this; |
||
239 | } |
||
240 | // Instantiate from string or array |
||
241 | 4 | if (!is_object($filter)) |
|
242 | 4 | { |
|
243 | 1 | $filter = $this->di->apply($filter); |
|
244 | 1 | } |
|
245 | 4 | if (!$filter instanceof PreFilterInterface && !$filter instanceof PostFilterInterface) |
|
246 | 4 | { |
|
247 | throw new UnexpectedValueException(sprintf('$filter must implement either `%s` or `%s` interface', PreFilterInterface::class, PostFilterInterface::class)); |
||
248 | } |
||
249 | 4 | $this->currentFilters[] = $filter; |
|
250 | 4 | return $this; |
|
251 | } |
||
252 | |||
253 | /** |
||
254 | * Emit signal to inform slots |
||
255 | * @param object|string $signal |
||
256 | * @return object[] |
||
257 | */ |
||
258 | 24 | public function emit($signal) |
|
259 | { |
||
260 | 17 | $result = []; |
|
261 | 17 | if (is_string($signal)) |
|
262 | 17 | { |
|
263 | $signal = new $signal; |
||
264 | 22 | } |
|
265 | 17 | $name = get_class($signal); |
|
266 | 24 | NameNormalizer::normalize($name); |
|
267 | 17 | if (empty(self::$config)) |
|
268 | 17 | { |
|
269 | $this->init(); |
||
270 | } |
||
271 | 17 | if (!isset(self::$config[self::Signals][$name])) |
|
272 | 17 | { |
|
273 | self::$config[self::Signals][$name] = []; |
||
274 | $this->loggerInstance->debug('No slots found for signal `{name}`, skipping', ['name' => $name]); |
||
275 | return $result; |
||
276 | } |
||
277 | |||
278 | 17 | foreach (self::$config[self::Signals][$name] as $fqn => $injections) |
|
|
|||
279 | { |
||
280 | // Skip |
||
281 | 18 | if (false === $injections || count($injections) == 0) |
|
282 | 17 | { |
|
283 | continue; |
||
284 | } |
||
285 | 17 | if (!PreFilter::filter($this, $fqn, $signal)) |
|
286 | 17 | { |
|
287 | 3 | continue; |
|
288 | } |
||
289 | 14 | foreach ($injections as $injection) |
|
290 | { |
||
291 | 14 | $injected = SlotFactory::create($this, $signal, $fqn, $injection); |
|
292 | 14 | if (false === $injected) |
|
293 | 14 | { |
|
294 | continue; |
||
295 | } |
||
296 | 14 | if (!PostFilter::filter($this, $injected, $signal)) |
|
297 | 14 | { |
|
298 | 2 | continue; |
|
299 | } |
||
300 | 12 | $result[] = $injected; |
|
301 | 14 | } |
|
302 | 17 | } |
|
303 | 17 | $this->currentFilters = []; |
|
304 | 17 | return $result; |
|
305 | } |
||
306 | |||
307 | /** |
||
308 | * Call for signals from slot |
||
309 | * @param object $slot |
||
310 | * @param string $interface Interface or class name which must be implemented, instanceof or sub class of to get into slot |
||
311 | */ |
||
312 | 7 | public function gather($slot, $interface = null) |
|
313 | { |
||
314 | 7 | $name = get_class($slot); |
|
315 | 7 | NameNormalizer::normalize($name); |
|
316 | 7 | if (!empty($interface)) |
|
317 | 7 | { |
|
318 | 3 | NameNormalizer::normalize($interface); |
|
319 | 3 | } |
|
320 | 7 | if (empty(self::$config)) |
|
321 | 7 | { |
|
322 | $this->init(); |
||
323 | } |
||
324 | 7 | if (!isset(self::$config[self::Slots][$name])) |
|
325 | 7 | { |
|
326 | self::$config[self::Slots][$name] = []; |
||
327 | $this->loggerInstance->debug('No signals found for slot `{name}`, skipping', ['name' => $name]); |
||
328 | } |
||
329 | 7 | $result = []; |
|
330 | 7 | foreach ((array) self::$config[self::Slots][$name] as $fqn => $emit) |
|
331 | { |
||
332 | 7 | if (false === $emit) |
|
333 | 7 | { |
|
334 | continue; |
||
335 | } |
||
336 | 7 | if (!PreFilter::filter($this, $fqn, $slot)) |
|
337 | 7 | { |
|
338 | continue; |
||
339 | } |
||
340 | // Check if class exists and log if doesn't |
||
341 | 7 | if (!ClassChecker::exists($fqn)) |
|
342 | 7 | { |
|
343 | $this->loggerInstance->debug(sprintf("Class `%s` not found while gathering slot `%s`", $fqn, get_class($slot))); |
||
344 | continue; |
||
345 | } |
||
346 | 7 | if (null === $interface) |
|
347 | 7 | { |
|
348 | 4 | $injected = new $fqn; |
|
349 | 4 | if (!PostFilter::filter($this, $injected, $slot)) |
|
350 | 4 | { |
|
351 | 2 | continue; |
|
352 | } |
||
353 | 2 | $result[] = $injected; |
|
354 | 2 | continue; |
|
355 | } |
||
356 | |||
357 | // Check if it's same as interface |
||
358 | 3 | if ($fqn === $interface) |
|
359 | 3 | { |
|
360 | 1 | $injected = new $fqn; |
|
361 | 1 | if (!PostFilter::filter($this, $injected, $slot)) |
|
362 | 1 | { |
|
363 | continue; |
||
364 | } |
||
365 | 1 | $result[] = $injected; |
|
366 | 1 | continue; |
|
367 | } |
||
368 | |||
369 | 3 | $info = new ReflectionClass($fqn); |
|
370 | |||
371 | // Check if class is instance of base class |
||
372 | 3 | if ($info->isSubclassOf($interface)) |
|
373 | 3 | { |
|
374 | 2 | $injected = new $fqn; |
|
375 | 2 | if (!PostFilter::filter($this, $injected, $slot)) |
|
376 | 2 | { |
|
377 | continue; |
||
378 | } |
||
379 | 2 | $result[] = $injected; |
|
380 | 2 | continue; |
|
381 | } |
||
382 | |||
383 | 3 | $interfaceInfo = new ReflectionClass($interface); |
|
384 | // Check if class implements interface |
||
385 | 3 | if ($interfaceInfo->isInterface() && $info->implementsInterface($interface)) |
|
386 | 3 | { |
|
387 | $injected = new $fqn; |
||
388 | if (!PostFilter::filter($this, $injected, $slot)) |
||
389 | { |
||
390 | continue; |
||
391 | } |
||
392 | $result[] = $injected; |
||
393 | continue; |
||
394 | } |
||
395 | 7 | } |
|
396 | 7 | return $result; |
|
397 | } |
||
398 | |||
399 | /** |
||
400 | * Get filters |
||
401 | * @param string $interface |
||
402 | * @return PreFilterInterface[]|PostFilterInterface[] |
||
403 | */ |
||
404 | 24 | public function getFilters($interface) |
|
405 | { |
||
406 | 24 | $filters = FilterFactory::create($this, $interface); |
|
407 | 24 | foreach ($this->currentFilters as $filter) |
|
408 | { |
||
409 | 4 | if (!$filter instanceof $interface) |
|
410 | 4 | { |
|
411 | 2 | continue; |
|
412 | } |
||
413 | 4 | $filters[] = $filter; |
|
414 | 24 | } |
|
415 | 24 | return $filters; |
|
416 | } |
||
417 | |||
418 | /** |
||
419 | * Get logger |
||
420 | * @codeCoverageIgnore |
||
421 | * @return LoggerInterface |
||
422 | */ |
||
423 | public function getLogger() |
||
427 | |||
428 | /** |
||
429 | * Set logger |
||
430 | * @codeCoverageIgnore |
||
431 | * @param LoggerInterface $logger |
||
432 | * @return Signal |
||
433 | */ |
||
434 | public function setLogger(LoggerInterface $logger) |
||
439 | |||
440 | /** |
||
441 | * Get dependency injection container |
||
442 | * @return EmbeDi |
||
443 | */ |
||
444 | 24 | public function getDi() |
|
448 | |||
449 | public function setDi(EmbeDi $di) |
||
454 | |||
455 | /** |
||
456 | * Get Input/Output adapter |
||
457 | * @codeCoverageIgnore |
||
458 | * @return BuilderIOInterface I/O Adapter |
||
459 | */ |
||
460 | public function getIO() |
||
464 | |||
465 | /** |
||
466 | * Set Input/Output interface |
||
467 | * @codeCoverageIgnore |
||
468 | * @param BuilderIOInterface $io |
||
469 | * @return Signal |
||
470 | */ |
||
471 | public function setIO(BuilderIOInterface $io) |
||
475 | |||
476 | /** |
||
477 | * @codeCoverageIgnore |
||
478 | * @return ExtractorInterface |
||
479 | */ |
||
480 | public function getExtractor() |
||
484 | |||
485 | /** |
||
486 | * @codeCoverageIgnore |
||
487 | * @param ExtractorInterface $extractor |
||
488 | * @return Signal |
||
489 | */ |
||
490 | public function setExtractor(ExtractorInterface $extractor) |
||
494 | |||
495 | /** |
||
496 | * Reloads signals cache and reinitializes component. |
||
497 | */ |
||
498 | public function resetCache() |
||
502 | |||
503 | private function reload() |
||
507 | |||
508 | /** |
||
509 | * Get configured property |
||
510 | * @param string $property |
||
511 | * @return SignalAwareInterface |
||
512 | */ |
||
513 | 1 | private function getConfigured($property) |
|
514 | { |
||
515 | 1 | if (is_object($this->$property)) |
|
516 | 1 | { |
|
517 | 1 | $object = $this->$property; |
|
518 | 1 | } |
|
519 | else |
||
520 | { |
||
521 | $object = $this->di->apply($this->$property); |
||
522 | } |
||
523 | 1 | if ($object instanceof SignalAwareInterface) |
|
524 | 1 | { |
|
525 | 1 | $object->setSignal($this); |
|
526 | 1 | } |
|
527 | 1 | return $object; |
|
528 | } |
||
529 | |||
530 | /** |
||
531 | * Set signal aware property |
||
532 | * @param SignalAwareInterface $object |
||
533 | * @param string $property |
||
534 | * @return Signal |
||
535 | */ |
||
536 | private function setConfigured(SignalAwareInterface $object, $property) |
||
542 | |||
543 | } |
||
544 |