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. |
||
63 | * This is path where config from yml will be stored. |
||
64 | * Path is relative to project root. |
||
65 | * @var string |
||
66 | */ |
||
67 | public $runtimePath = 'runtime'; |
||
68 | |||
69 | /** |
||
70 | * This aliases will be searched for SlotFor and SignalFor annotations |
||
71 | * TODO Autodetect based on composer autoload |
||
72 | * @var string[] |
||
73 | */ |
||
74 | public $paths = [ |
||
75 | 'vendor', |
||
76 | ]; |
||
77 | |||
78 | /** |
||
79 | * Directories to ignore while scanning |
||
80 | * @var string[] |
||
81 | */ |
||
82 | public $ignoreDirs = [ |
||
83 | 'vendor', // Vendors in vendors |
||
84 | 'generated', // Generated data, including signals |
||
85 | 'runtime', // Runtime data |
||
86 | ]; |
||
87 | |||
88 | /** |
||
89 | * Filters configuration. |
||
90 | * This filters will be applied to every emit. |
||
91 | * @var string[]|object[] |
||
92 | */ |
||
93 | public $filters = []; |
||
94 | |||
95 | /** |
||
96 | * Sorters configuration. |
||
97 | * @var string[]|object[] |
||
98 | */ |
||
99 | public $sorters = []; |
||
100 | |||
101 | /** |
||
102 | * Extractor configuration |
||
103 | * @var string|[]|object |
||
104 | */ |
||
105 | public $extractor = Addendum::class; |
||
106 | |||
107 | /** |
||
108 | * Input/Output configuration |
||
109 | * @var string|[]|object |
||
110 | */ |
||
111 | public $io = PhpFile::class; |
||
112 | |||
113 | /** |
||
114 | * Whenever component is initialized |
||
115 | * @var bool |
||
116 | */ |
||
117 | public $isInitialized = false; |
||
118 | |||
119 | /** |
||
120 | * Configuration of signals and slots |
||
121 | * @var string[][] |
||
122 | */ |
||
123 | private static $config = []; |
||
124 | |||
125 | /** |
||
126 | * Logger instance holder |
||
127 | * NOTE: There is property annotation with `logger` name, |
||
128 | * thus this name is a bit longer |
||
129 | * @var LoggerInterface |
||
130 | */ |
||
131 | private $loggerInstance = null; |
||
132 | |||
133 | /** |
||
134 | * Embedded dependency injection |
||
135 | * @var EmbeDi |
||
136 | */ |
||
137 | private $di = null; |
||
138 | |||
139 | /** |
||
140 | * Version |
||
141 | * @var string |
||
142 | */ |
||
143 | private $version = null; |
||
144 | |||
145 | /** |
||
146 | * Current filters |
||
147 | * @var PreFilterInterface[]|PostFilterInterface[] |
||
148 | */ |
||
149 | private $currentFilters = []; |
||
150 | |||
151 | 25 | public function __construct($configName = self::ConfigName) |
|
163 | |||
164 | /** |
||
165 | * Getter |
||
166 | * @param string $name |
||
167 | * @return mixed |
||
168 | */ |
||
169 | public function __get($name) |
||
173 | |||
174 | /** |
||
175 | * Setter |
||
176 | * @param string $name |
||
177 | * @param mixed $value |
||
178 | * @return mixed |
||
179 | */ |
||
180 | public function __set($name, $value) |
||
184 | |||
185 | /** |
||
186 | * Get current signals version |
||
187 | * |
||
188 | * @codeCoverageIgnore |
||
189 | * @return string |
||
190 | */ |
||
191 | public function getVersion() |
||
199 | |||
200 | public function init() |
||
211 | |||
212 | /** |
||
213 | * Apply filter to current emit. |
||
214 | * @param FilterInterface|string|mixed $filter |
||
215 | * @return Signal |
||
216 | * @throws UnexpectedValueException |
||
217 | */ |
||
218 | 4 | public function filter($filter) |
|
232 | |||
233 | /** |
||
234 | * Emit signal to inform slots |
||
235 | * @param object|string $signal |
||
236 | * @return object[] |
||
237 | */ |
||
238 | 24 | public function emit($signal) |
|
286 | |||
287 | /** |
||
288 | * Call for signals from slot |
||
289 | * @param object $slot |
||
290 | * @param string $interface Interface or class name which must be implemented, instanceof or sub class of to get into slot |
||
291 | */ |
||
292 | 7 | public function gather($slot, $interface = null) |
|
378 | |||
379 | /** |
||
380 | * Get filters |
||
381 | * @param string $interface |
||
382 | * @return PreFilterInterface[]|PostFilterInterface[] |
||
383 | */ |
||
384 | 24 | public function getFilters($interface) |
|
397 | |||
398 | /** |
||
399 | * Get logger |
||
400 | * @codeCoverageIgnore |
||
401 | * @return LoggerInterface |
||
402 | */ |
||
403 | public function getLogger() |
||
407 | |||
408 | /** |
||
409 | * Set logger |
||
410 | * @codeCoverageIgnore |
||
411 | * @param LoggerInterface $logger |
||
412 | * @return Signal |
||
413 | */ |
||
414 | public function setLogger(LoggerInterface $logger) |
||
419 | |||
420 | /** |
||
421 | * Get dependency injection container |
||
422 | * @return EmbeDi |
||
423 | */ |
||
424 | 24 | public function getDi() |
|
428 | |||
429 | public function setDi(EmbeDi $di) |
||
434 | |||
435 | /** |
||
436 | * Get Input/Output adapter |
||
437 | * @codeCoverageIgnore |
||
438 | * @return BuilderIOInterface I/O Adapter |
||
439 | */ |
||
440 | public function getIO() |
||
444 | |||
445 | /** |
||
446 | * Set Input/Output interface |
||
447 | * @codeCoverageIgnore |
||
448 | * @param BuilderIOInterface $io |
||
449 | * @return Signal |
||
450 | */ |
||
451 | public function setIO(BuilderIOInterface $io) |
||
455 | |||
456 | /** |
||
457 | * @codeCoverageIgnore |
||
458 | * @return ExtractorInterface |
||
459 | */ |
||
460 | public function getExtractor() |
||
464 | |||
465 | /** |
||
466 | * @codeCoverageIgnore |
||
467 | * @param ExtractorInterface $extractor |
||
468 | * @return Signal |
||
469 | */ |
||
470 | public function setExtractor(ExtractorInterface $extractor) |
||
474 | |||
475 | /** |
||
476 | * Reloads signals cache and reinitializes component. |
||
477 | */ |
||
478 | public function resetCache() |
||
482 | |||
483 | private function reload() |
||
487 | |||
488 | /** |
||
489 | * Get configured property |
||
490 | * @param string $property |
||
491 | * @return SignalAwareInterface |
||
492 | */ |
||
493 | 1 | private function getConfigured($property) |
|
509 | |||
510 | /** |
||
511 | * Set signal aware property |
||
512 | * @param SignalAwareInterface $object |
||
513 | * @param string $property |
||
514 | * @return Signal |
||
515 | */ |
||
516 | private function setConfigured(SignalAwareInterface $object, $property) |
||
522 | |||
523 | } |
||
524 |