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 |
||
44 | class Signal implements LoggerAwareInterface |
||
45 | { |
||
46 | |||
47 | const Slots = 'slots'; |
||
48 | const Signals = 'signals'; |
||
49 | |||
50 | /** |
||
51 | * Generated signals name. |
||
52 | * Name of this constant is confusing. |
||
53 | * @internal description |
||
54 | */ |
||
55 | const ConfigFilename = 'signals-definition.php'; |
||
56 | |||
57 | /** |
||
58 | * Config file name |
||
59 | */ |
||
60 | const ConfigName = "signals"; |
||
61 | |||
62 | /** |
||
63 | * Runtime path is directory where config cache from yml file will |
||
64 | * be stored. Path is relative to project root. This must be writable |
||
65 | * by command line user. |
||
66 | * |
||
67 | * @var string |
||
68 | */ |
||
69 | public $runtimePath = 'runtime'; |
||
70 | |||
71 | /** |
||
72 | * This paths will be searched for `SlotFor` and `SignalFor` annotations. |
||
73 | * |
||
74 | * |
||
75 | * |
||
76 | * TODO Autodetect based on composer autoload |
||
77 | * |
||
78 | * @var string[] |
||
79 | */ |
||
80 | public $paths = [ |
||
81 | 'vendor', |
||
82 | ]; |
||
83 | |||
84 | /** |
||
85 | * Directories to ignore while scanning |
||
86 | * @var string[] |
||
87 | */ |
||
88 | public $ignoreDirs = [ |
||
89 | 'vendor', // Vendors in vendors |
||
90 | 'generated', // Generated data, including signals |
||
91 | 'runtime', // Runtime data |
||
92 | ]; |
||
93 | |||
94 | /** |
||
95 | * Filters configuration. |
||
96 | * This filters will be applied to every emit. This property |
||
97 | * should contain array of class names implementing filters. |
||
98 | * @var string[]|object[] |
||
99 | */ |
||
100 | public $filters = []; |
||
101 | |||
102 | /** |
||
103 | * Sorters configuration. |
||
104 | * @var string[]|object[] |
||
105 | */ |
||
106 | public $sorters = []; |
||
107 | |||
108 | /** |
||
109 | * Extractor configuration |
||
110 | * @var string|[]|object |
||
111 | */ |
||
112 | public $extractor = Addendum::class; |
||
113 | |||
114 | /** |
||
115 | * Input/Output configuration, at minimum it should |
||
116 | * contain class name for builder input output interface. |
||
117 | * It can also contain array [configurable options for IO class](php-io/). |
||
118 | * |
||
119 | * |
||
120 | * |
||
121 | * @var string|[]|object |
||
122 | */ |
||
123 | public $io = PhpFile::class; |
||
124 | |||
125 | /** |
||
126 | * Whenever component is initialized |
||
127 | * @var bool |
||
128 | */ |
||
129 | private $isInitialized = false; |
||
130 | |||
131 | /** |
||
132 | * Configuration of signals and slots |
||
133 | * @var string[][] |
||
134 | */ |
||
135 | private static $config = []; |
||
136 | |||
137 | /** |
||
138 | * Extra configurations of signals and slots |
||
139 | * @var array |
||
140 | */ |
||
141 | private static $configs = []; |
||
142 | |||
143 | /** |
||
144 | * Logger instance holder |
||
145 | * NOTE: There is property annotation with `logger` name, |
||
146 | * thus this name is a bit longer |
||
147 | * @var LoggerInterface |
||
148 | */ |
||
149 | private $loggerInstance = null; |
||
150 | |||
151 | /** |
||
152 | * Embedded dependency injection |
||
153 | * @var EmbeDi |
||
154 | */ |
||
155 | private $di = null; |
||
156 | |||
157 | /** |
||
158 | * Version |
||
159 | * @var string |
||
160 | */ |
||
161 | private $version = null; |
||
162 | |||
163 | /** |
||
164 | * Current filters |
||
165 | * @var PreFilterInterface[]|PostFilterInterface[] |
||
166 | */ |
||
167 | private $currentFilters = []; |
||
168 | |||
169 | 25 | public function __construct($configName = self::ConfigName) |
|
178 | |||
179 | /** |
||
180 | * Getter |
||
181 | * @param string $name |
||
182 | * @return mixed |
||
183 | */ |
||
184 | public function __get($name) |
||
188 | |||
189 | /** |
||
190 | * Setter |
||
191 | * @param string $name |
||
192 | * @param mixed $value |
||
193 | * @return mixed |
||
194 | */ |
||
195 | public function __set($name, $value) |
||
199 | |||
200 | /** |
||
201 | * Get current signals version |
||
202 | * |
||
203 | * @codeCoverageIgnore |
||
204 | * @return string |
||
205 | */ |
||
206 | public function getVersion() |
||
214 | |||
215 | public function init() |
||
226 | |||
227 | /** |
||
228 | * Attach additional signals and slots configuration |
||
229 | * @param $config |
||
230 | * @param bool $reload |
||
231 | */ |
||
232 | public function attach($config, $reload = true) |
||
240 | |||
241 | /** |
||
242 | * Apply filter to current emit. |
||
243 | * |
||
244 | * Pass false as param to disable all filters. |
||
245 | * |
||
246 | * @param FilterInterface|string|mixed $filter |
||
247 | * @return Signal |
||
248 | * @throws UnexpectedValueException |
||
249 | */ |
||
250 | 4 | public function filter($filter) |
|
270 | |||
271 | /** |
||
272 | * Emit signal to inform slots |
||
273 | * @param object|string $signal |
||
274 | * @return object[] |
||
275 | */ |
||
276 | 17 | public function emit($signal) |
|
324 | |||
325 | /** |
||
326 | * Call for signals from slot |
||
327 | * @param object $slot |
||
328 | * @param string $interface Interface or class name which must be implemented, instanceof or sub class of to get |
||
329 | * into slot |
||
330 | * @return array |
||
331 | */ |
||
332 | 7 | public function gather($slot, $interface = null) |
|
419 | |||
420 | /** |
||
421 | * Get filters |
||
422 | * @param string $interface |
||
423 | * @return PreFilterInterface[]|PostFilterInterface[] |
||
424 | */ |
||
425 | 24 | public function getFilters($interface) |
|
438 | |||
439 | /** |
||
440 | * Get logger |
||
441 | * @codeCoverageIgnore |
||
442 | * @return LoggerInterface |
||
443 | */ |
||
444 | public function getLogger() |
||
448 | |||
449 | /** |
||
450 | * Set logger |
||
451 | * @codeCoverageIgnore |
||
452 | * @param LoggerInterface $logger |
||
453 | * @return Signal |
||
454 | */ |
||
455 | public function setLogger(LoggerInterface $logger) |
||
460 | |||
461 | /** |
||
462 | * Get dependency injection container |
||
463 | * @return EmbeDi |
||
464 | */ |
||
465 | 24 | public function getDi() |
|
469 | |||
470 | public function setDi(EmbeDi $di) |
||
475 | |||
476 | /** |
||
477 | * Get Input/Output adapter |
||
478 | * @codeCoverageIgnore |
||
479 | * @return BuilderIOInterface I/O Adapter |
||
480 | */ |
||
481 | public function getIO() |
||
485 | |||
486 | /** |
||
487 | * Set Input/Output interface |
||
488 | * @codeCoverageIgnore |
||
489 | * @param BuilderIOInterface $io |
||
490 | * @return Signal |
||
491 | */ |
||
492 | public function setIO(BuilderIOInterface $io) |
||
496 | |||
497 | /** |
||
498 | * @codeCoverageIgnore |
||
499 | * @return ExtractorInterface |
||
500 | */ |
||
501 | public function getExtractor() |
||
505 | |||
506 | /** |
||
507 | * @codeCoverageIgnore |
||
508 | * @param ExtractorInterface $extractor |
||
509 | * @return Signal |
||
510 | */ |
||
511 | public function setExtractor(ExtractorInterface $extractor) |
||
515 | |||
516 | /** |
||
517 | * Reloads signals cache and re-initializes component. |
||
518 | */ |
||
519 | public function resetCache() |
||
523 | |||
524 | private function reload() |
||
533 | |||
534 | /** |
||
535 | * Get configured property |
||
536 | * @param string $property |
||
537 | * @return SignalAwareInterface|ExtractorInterface|BuilderIOInterface |
||
538 | */ |
||
539 | 1 | private function getConfigured($property) |
|
555 | |||
556 | /** |
||
557 | * Set signal aware property |
||
558 | * @param SignalAwareInterface $object |
||
559 | * @param string $property |
||
560 | * @return Signal |
||
561 | */ |
||
562 | private function setConfigured(SignalAwareInterface $object, $property) |
||
568 | |||
569 | } |
||
570 |