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) |
|
181 | |||
182 | /** |
||
183 | * Getter |
||
184 | * @param string $name |
||
185 | * @return mixed |
||
186 | */ |
||
187 | public function __get($name) |
||
191 | |||
192 | /** |
||
193 | * Setter |
||
194 | * @param string $name |
||
195 | * @param mixed $value |
||
196 | * @return mixed |
||
197 | */ |
||
198 | public function __set($name, $value) |
||
202 | |||
203 | /** |
||
204 | * Get current signals version |
||
205 | * |
||
206 | * @codeCoverageIgnore |
||
207 | * @return string |
||
208 | */ |
||
209 | public function getVersion() |
||
217 | |||
218 | public function init() |
||
229 | |||
230 | /** |
||
231 | * Attach additional signals and slots configuration |
||
232 | * @param $config |
||
233 | * @param bool $reload |
||
234 | */ |
||
235 | public function attach($config, $reload = true) |
||
243 | |||
244 | /** |
||
245 | * Apply filter to current emit. |
||
246 | * |
||
247 | * Pass false as param to disable all filters. |
||
248 | * |
||
249 | * @param FilterInterface|string|mixed $filter |
||
250 | * @return Signal |
||
251 | * @throws UnexpectedValueException |
||
252 | */ |
||
253 | 4 | public function filter($filter) |
|
273 | |||
274 | /** |
||
275 | * Emit signal to inform slots |
||
276 | * @param object|string $signal |
||
277 | * @return object[] |
||
278 | */ |
||
279 | 17 | public function emit($signal) |
|
327 | |||
328 | /** |
||
329 | * Call for signals from slot |
||
330 | * @param object $slot |
||
331 | * @param string $interface Interface or class name which must be implemented, instanceof or sub class of to get |
||
332 | * into slot |
||
333 | * @return array |
||
334 | */ |
||
335 | 7 | public function gather($slot, $interface = null) |
|
422 | |||
423 | /** |
||
424 | * Get filters |
||
425 | * @param string $interface |
||
426 | * @return PreFilterInterface[]|PostFilterInterface[] |
||
427 | */ |
||
428 | 24 | public function getFilters($interface) |
|
441 | |||
442 | /** |
||
443 | * Get logger |
||
444 | * @codeCoverageIgnore |
||
445 | * @return LoggerInterface |
||
446 | */ |
||
447 | public function getLogger() |
||
451 | |||
452 | /** |
||
453 | * Set logger |
||
454 | * @codeCoverageIgnore |
||
455 | * @param LoggerInterface $logger |
||
456 | * @return Signal |
||
457 | */ |
||
458 | public function setLogger(LoggerInterface $logger) |
||
463 | |||
464 | /** |
||
465 | * Get dependency injection container |
||
466 | * @return EmbeDi |
||
467 | */ |
||
468 | 24 | public function getDi() |
|
472 | |||
473 | public function setDi(EmbeDi $di) |
||
478 | |||
479 | /** |
||
480 | * Get Input/Output adapter |
||
481 | * @codeCoverageIgnore |
||
482 | * @return BuilderIOInterface I/O Adapter |
||
483 | */ |
||
484 | public function getIO() |
||
488 | |||
489 | /** |
||
490 | * Set Input/Output interface |
||
491 | * @codeCoverageIgnore |
||
492 | * @param BuilderIOInterface $io |
||
493 | * @return Signal |
||
494 | */ |
||
495 | public function setIO(BuilderIOInterface $io) |
||
499 | |||
500 | /** |
||
501 | * @codeCoverageIgnore |
||
502 | * @return ExtractorInterface |
||
503 | */ |
||
504 | public function getExtractor() |
||
508 | |||
509 | /** |
||
510 | * @codeCoverageIgnore |
||
511 | * @param ExtractorInterface $extractor |
||
512 | * @return Signal |
||
513 | */ |
||
514 | public function setExtractor(ExtractorInterface $extractor) |
||
518 | |||
519 | /** |
||
520 | * Reloads signals cache and re-initializes component. |
||
521 | */ |
||
522 | public function resetCache() |
||
526 | |||
527 | private function reload() |
||
536 | |||
537 | /** |
||
538 | * Get configured property |
||
539 | * @param string $property |
||
540 | * @return SignalAwareInterface |
||
541 | */ |
||
542 | 1 | private function getConfigured($property) |
|
558 | |||
559 | /** |
||
560 | * Set signal aware property |
||
561 | * @param SignalAwareInterface $object |
||
562 | * @param string $property |
||
563 | * @return Signal |
||
564 | */ |
||
565 | private function setConfigured(SignalAwareInterface $object, $property) |
||
571 | |||
572 | } |
||
573 |