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 |
||
42 | class Signal implements LoggerAwareInterface |
||
43 | { |
||
44 | |||
45 | const Slots = 'slots'; |
||
46 | const Signals = 'signals'; |
||
47 | |||
48 | /** |
||
49 | * Generated signals name. |
||
50 | * Name of this constant is confusing. |
||
51 | * @internal description |
||
52 | */ |
||
53 | const ConfigFilename = 'signals-definition.php'; |
||
54 | |||
55 | /** |
||
56 | * Config file name |
||
57 | */ |
||
58 | const ConfigName = "signals"; |
||
59 | |||
60 | /** |
||
61 | * Runtime path. |
||
62 | * This is path where config from yml will be stored. |
||
63 | * Path is relative to project root. |
||
64 | * @var string |
||
65 | */ |
||
66 | public $runtimePath = 'runtime'; |
||
67 | |||
68 | /** |
||
69 | * This aliases will be searched for SlotFor and SignalFor annotations |
||
70 | * TODO Autodetect based on composer autoload |
||
71 | * @var string[] |
||
72 | */ |
||
73 | public $paths = [ |
||
74 | 'vendor', |
||
75 | ]; |
||
76 | |||
77 | /** |
||
78 | * Filters configuration. |
||
79 | * This filters will be applied to every emit. |
||
80 | * @var string[]|object[] |
||
81 | */ |
||
82 | public $filters = []; |
||
83 | |||
84 | /** |
||
85 | * Sorters configuration. |
||
86 | * @var string[]|object[] |
||
87 | */ |
||
88 | public $sorters = []; |
||
89 | |||
90 | /** |
||
91 | * Extractor configuration |
||
92 | * @var string|[]|object |
||
93 | */ |
||
94 | public $extractor = Addendum::class; |
||
95 | |||
96 | /** |
||
97 | * Input/Output configuration |
||
98 | * @var string|[]|object |
||
99 | */ |
||
100 | public $io = PhpFile::class; |
||
101 | |||
102 | /** |
||
103 | * Whenever component is initialized |
||
104 | * @var bool |
||
105 | */ |
||
106 | public $isInitialized = false; |
||
107 | |||
108 | /** |
||
109 | * Configuration of signals and slots |
||
110 | * @var string[][] |
||
111 | */ |
||
112 | private static $config = []; |
||
113 | |||
114 | /** |
||
115 | * Logger |
||
116 | * @var LoggerInterface |
||
117 | */ |
||
118 | private $logger = null; |
||
119 | |||
120 | /** |
||
121 | * |
||
122 | * @var EmbeDi |
||
123 | */ |
||
124 | private $di = null; |
||
125 | |||
126 | /** |
||
127 | * Version |
||
128 | * @var string |
||
129 | */ |
||
130 | private $version = null; |
||
131 | |||
132 | /** |
||
133 | * Current filters |
||
134 | * @var PreFilterInterface[]|PostFilterInterface[] |
||
135 | */ |
||
136 | private $currentFilters = []; |
||
137 | |||
138 | 17 | public function __construct($configName = self::ConfigName) |
|
150 | |||
151 | /** |
||
152 | * Getter |
||
153 | * @param string $name |
||
154 | * @return mixed |
||
155 | */ |
||
156 | public function __get($name) |
||
160 | |||
161 | /** |
||
162 | * Setter |
||
163 | * @param string $name |
||
164 | * @param mixed $value |
||
165 | * @return mixed |
||
166 | */ |
||
167 | public function __set($name, $value) |
||
171 | |||
172 | /** |
||
173 | * Get current signals version |
||
174 | * |
||
175 | * @codeCoverageIgnore |
||
176 | * @return string |
||
177 | */ |
||
178 | public function getVersion() |
||
186 | |||
187 | public function init() |
||
198 | |||
199 | /** |
||
200 | * Apply filter to current emit. |
||
201 | * @param FilterInterface|string|mixed $filter |
||
202 | * @return Signal |
||
203 | * @throws UnexpectedValueException |
||
204 | */ |
||
205 | 3 | public function filter($filter) |
|
219 | |||
220 | /** |
||
221 | * Emit signal to inform slots |
||
222 | * @param object|string $signal |
||
223 | * @return object[] |
||
224 | */ |
||
225 | 16 | public function emit($signal) |
|
273 | |||
274 | /** |
||
275 | * Call for signals from slot |
||
276 | * @param object $slot |
||
277 | * @param string $interface Interface, which must be implemented to get into slot |
||
278 | */ |
||
279 | public function gather($slot, $interface = null) |
||
319 | |||
320 | /** |
||
321 | * Get filters |
||
322 | * @param string $interface |
||
323 | * @return PreFilterInterface[]|PostFilterInterface[] |
||
324 | */ |
||
325 | 16 | public function getFilters($interface) |
|
338 | |||
339 | /** |
||
340 | * Get logger |
||
341 | * @codeCoverageIgnore |
||
342 | * @return LoggerInterface |
||
343 | */ |
||
344 | public function getLogger() |
||
348 | |||
349 | /** |
||
350 | * Set logger |
||
351 | * @codeCoverageIgnore |
||
352 | * @param LoggerInterface $logger |
||
353 | * @return Signal |
||
354 | */ |
||
355 | public function setLogger(LoggerInterface $logger) |
||
360 | |||
361 | /** |
||
362 | * Get dependency injection container |
||
363 | * @return EmbeDi |
||
364 | */ |
||
365 | 16 | public function getDi() |
|
369 | |||
370 | public function setDi(EmbeDi $di) |
||
375 | |||
376 | /** |
||
377 | * Get Input/Output adapter |
||
378 | * @codeCoverageIgnore |
||
379 | * @return BuilderIOInterface I/O Adapter |
||
380 | */ |
||
381 | public function getIO() |
||
385 | |||
386 | /** |
||
387 | * Set Input/Output interface |
||
388 | * @codeCoverageIgnore |
||
389 | * @param BuilderIOInterface $io |
||
390 | * @return Signal |
||
391 | */ |
||
392 | public function setIO(BuilderIOInterface $io) |
||
396 | |||
397 | /** |
||
398 | * @codeCoverageIgnore |
||
399 | * @return ExtractorInterface |
||
400 | */ |
||
401 | public function getExtractor() |
||
405 | |||
406 | /** |
||
407 | * @codeCoverageIgnore |
||
408 | * @param ExtractorInterface $extractor |
||
409 | * @return Signal |
||
410 | */ |
||
411 | public function setExtractor(ExtractorInterface $extractor) |
||
415 | |||
416 | /** |
||
417 | * Reloads signals cache and reinitializes component. |
||
418 | */ |
||
419 | public function resetCache() |
||
423 | |||
424 | private function reload() |
||
428 | |||
429 | /** |
||
430 | * Get configured property |
||
431 | * @param string $property |
||
432 | * @return SignalAwareInterface |
||
433 | */ |
||
434 | 1 | private function getConfigured($property) |
|
450 | |||
451 | /** |
||
452 | * Set signal aware property |
||
453 | * @param SignalAwareInterface $object |
||
454 | * @param string $property |
||
455 | * @return Signal |
||
456 | */ |
||
457 | private function setConfigured(SignalAwareInterface $object, $property) |
||
463 | |||
464 | } |
||
465 |