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 | * Extra configurations of signals and slots |
||
138 | * @var array |
||
139 | */ |
||
140 | private static $configs = []; |
||
141 | |||
142 | /** |
||
143 | * Logger instance holder |
||
144 | * NOTE: There is property annotation with `logger` name, |
||
145 | * thus this name is a bit longer |
||
146 | * @var LoggerInterface |
||
147 | */ |
||
148 | private $loggerInstance = null; |
||
149 | |||
150 | /** |
||
151 | * Embedded dependency injection |
||
152 | * @var EmbeDi |
||
153 | */ |
||
154 | private $di = null; |
||
155 | |||
156 | /** |
||
157 | * Version |
||
158 | * @var string |
||
159 | */ |
||
160 | private $version = null; |
||
161 | |||
162 | /** |
||
163 | * Current filters |
||
164 | * @var PreFilterInterface[]|PostFilterInterface[] |
||
165 | */ |
||
166 | private $currentFilters = []; |
||
167 | |||
168 | 25 | public function __construct($configName = self::ConfigName) |
|
180 | |||
181 | /** |
||
182 | * Getter |
||
183 | * @param string $name |
||
184 | * @return mixed |
||
185 | */ |
||
186 | public function __get($name) |
||
190 | |||
191 | /** |
||
192 | * Setter |
||
193 | * @param string $name |
||
194 | * @param mixed $value |
||
195 | * @return mixed |
||
196 | */ |
||
197 | public function __set($name, $value) |
||
201 | |||
202 | /** |
||
203 | * Get current signals version |
||
204 | * |
||
205 | * @codeCoverageIgnore |
||
206 | * @return string |
||
207 | */ |
||
208 | public function getVersion() |
||
216 | |||
217 | public function init() |
||
228 | |||
229 | /** |
||
230 | * Attach additional signals and slots configuration |
||
231 | * @param $config |
||
232 | * @param bool $reload |
||
233 | */ |
||
234 | public function attach($config, $reload = true) |
||
242 | |||
243 | /** |
||
244 | * Apply filter to current emit. |
||
245 | * |
||
246 | * Pass false as param to disable all filters. |
||
247 | * |
||
248 | * @param FilterInterface|string|mixed $filter |
||
249 | * @return Signal |
||
250 | * @throws UnexpectedValueException |
||
251 | */ |
||
252 | 4 | public function filter($filter) |
|
272 | |||
273 | /** |
||
274 | * Emit signal to inform slots |
||
275 | * @param object|string $signal |
||
276 | * @return object[] |
||
277 | */ |
||
278 | 17 | public function emit($signal) |
|
326 | |||
327 | /** |
||
328 | * Call for signals from slot |
||
329 | * @param object $slot |
||
330 | * @param string $interface Interface or class name which must be implemented, instanceof or sub class of to get into slot |
||
331 | */ |
||
332 | 7 | public function gather($slot, $interface = null) |
|
418 | |||
419 | /** |
||
420 | * Get filters |
||
421 | * @param string $interface |
||
422 | * @return PreFilterInterface[]|PostFilterInterface[] |
||
423 | */ |
||
424 | 24 | public function getFilters($interface) |
|
437 | |||
438 | /** |
||
439 | * Get logger |
||
440 | * @codeCoverageIgnore |
||
441 | * @return LoggerInterface |
||
442 | */ |
||
443 | public function getLogger() |
||
447 | |||
448 | /** |
||
449 | * Set logger |
||
450 | * @codeCoverageIgnore |
||
451 | * @param LoggerInterface $logger |
||
452 | * @return Signal |
||
453 | */ |
||
454 | public function setLogger(LoggerInterface $logger) |
||
459 | |||
460 | /** |
||
461 | * Get dependency injection container |
||
462 | * @return EmbeDi |
||
463 | */ |
||
464 | 24 | public function getDi() |
|
468 | |||
469 | public function setDi(EmbeDi $di) |
||
474 | |||
475 | /** |
||
476 | * Get Input/Output adapter |
||
477 | * @codeCoverageIgnore |
||
478 | * @return BuilderIOInterface I/O Adapter |
||
479 | */ |
||
480 | public function getIO() |
||
484 | |||
485 | /** |
||
486 | * Set Input/Output interface |
||
487 | * @codeCoverageIgnore |
||
488 | * @param BuilderIOInterface $io |
||
489 | * @return Signal |
||
490 | */ |
||
491 | public function setIO(BuilderIOInterface $io) |
||
495 | |||
496 | /** |
||
497 | * @codeCoverageIgnore |
||
498 | * @return ExtractorInterface |
||
499 | */ |
||
500 | public function getExtractor() |
||
504 | |||
505 | /** |
||
506 | * @codeCoverageIgnore |
||
507 | * @param ExtractorInterface $extractor |
||
508 | * @return Signal |
||
509 | */ |
||
510 | public function setExtractor(ExtractorInterface $extractor) |
||
514 | |||
515 | /** |
||
516 | * Reloads signals cache and reinitializes component. |
||
517 | */ |
||
518 | public function resetCache() |
||
522 | |||
523 | private function reload() |
||
532 | |||
533 | /** |
||
534 | * Get configured property |
||
535 | * @param string $property |
||
536 | * @return SignalAwareInterface |
||
537 | */ |
||
538 | 1 | private function getConfigured($property) |
|
554 | |||
555 | /** |
||
556 | * Set signal aware property |
||
557 | * @param SignalAwareInterface $object |
||
558 | * @param string $property |
||
559 | * @return Signal |
||
560 | */ |
||
561 | private function setConfigured(SignalAwareInterface $object, $property) |
||
567 | |||
568 | } |
||
569 |