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 | * Directories to ignore while scanning |
||
| 79 | * @var string[] |
||
| 80 | */ |
||
| 81 | public $ignoreDirs = [ |
||
| 82 | 'vendor', // Vendors in vendors |
||
| 83 | 'generated', // Generated data, including signals |
||
| 84 | 'runtime', // Runtime data |
||
| 85 | ]; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Filters configuration. |
||
| 89 | * This filters will be applied to every emit. |
||
| 90 | * @var string[]|object[] |
||
| 91 | */ |
||
| 92 | public $filters = []; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Sorters configuration. |
||
| 96 | * @var string[]|object[] |
||
| 97 | */ |
||
| 98 | public $sorters = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Extractor configuration |
||
| 102 | * @var string|[]|object |
||
| 103 | */ |
||
| 104 | public $extractor = Addendum::class; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Input/Output configuration |
||
| 108 | * @var string|[]|object |
||
| 109 | */ |
||
| 110 | public $io = PhpFile::class; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Whenever component is initialized |
||
| 114 | * @var bool |
||
| 115 | */ |
||
| 116 | public $isInitialized = false; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Configuration of signals and slots |
||
| 120 | * @var string[][] |
||
| 121 | */ |
||
| 122 | private static $config = []; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Logger instance holder |
||
| 126 | * NOTE: There is property annotation with `logger` name, |
||
| 127 | * thus this name is a bit longer |
||
| 128 | * @var LoggerInterface |
||
| 129 | */ |
||
| 130 | private $loggerInstance = null; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Embedded dependency injection |
||
| 134 | * @var EmbeDi |
||
| 135 | */ |
||
| 136 | private $di = null; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Version |
||
| 140 | * @var string |
||
| 141 | */ |
||
| 142 | private $version = null; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Current filters |
||
| 146 | * @var PreFilterInterface[]|PostFilterInterface[] |
||
| 147 | */ |
||
| 148 | private $currentFilters = []; |
||
| 149 | |||
| 150 | 17 | public function __construct($configName = self::ConfigName) |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Getter |
||
| 165 | * @param string $name |
||
| 166 | * @return mixed |
||
| 167 | */ |
||
| 168 | public function __get($name) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Setter |
||
| 175 | * @param string $name |
||
| 176 | * @param mixed $value |
||
| 177 | * @return mixed |
||
| 178 | */ |
||
| 179 | public function __set($name, $value) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Get current signals version |
||
| 186 | * |
||
| 187 | * @codeCoverageIgnore |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getVersion() |
||
| 198 | |||
| 199 | public function init() |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Apply filter to current emit. |
||
| 213 | * @param FilterInterface|string|mixed $filter |
||
| 214 | * @return Signal |
||
| 215 | * @throws UnexpectedValueException |
||
| 216 | */ |
||
| 217 | 3 | public function filter($filter) |
|
| 231 | |||
| 232 | /** |
||
| 233 | * Emit signal to inform slots |
||
| 234 | * @param object|string $signal |
||
| 235 | * @return object[] |
||
| 236 | */ |
||
| 237 | 17 | public function emit($signal) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Call for signals from slot |
||
| 288 | * @param object $slot |
||
| 289 | * @param string $interface Interface, which must be implemented to get into slot |
||
| 290 | */ |
||
| 291 | public function gather($slot, $interface = null) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Get filters |
||
| 334 | * @param string $interface |
||
| 335 | * @return PreFilterInterface[]|PostFilterInterface[] |
||
| 336 | */ |
||
| 337 | 16 | public function getFilters($interface) |
|
| 350 | |||
| 351 | /** |
||
| 352 | * Get logger |
||
| 353 | * @codeCoverageIgnore |
||
| 354 | * @return LoggerInterface |
||
| 355 | */ |
||
| 356 | public function getLogger() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Set logger |
||
| 363 | * @codeCoverageIgnore |
||
| 364 | * @param LoggerInterface $logger |
||
| 365 | * @return Signal |
||
| 366 | */ |
||
| 367 | public function setLogger(LoggerInterface $logger) |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Get dependency injection container |
||
| 375 | * @return EmbeDi |
||
| 376 | */ |
||
| 377 | 16 | public function getDi() |
|
| 381 | |||
| 382 | public function setDi(EmbeDi $di) |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Get Input/Output adapter |
||
| 390 | * @codeCoverageIgnore |
||
| 391 | * @return BuilderIOInterface I/O Adapter |
||
| 392 | */ |
||
| 393 | public function getIO() |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Set Input/Output interface |
||
| 400 | * @codeCoverageIgnore |
||
| 401 | * @param BuilderIOInterface $io |
||
| 402 | * @return Signal |
||
| 403 | */ |
||
| 404 | public function setIO(BuilderIOInterface $io) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @codeCoverageIgnore |
||
| 411 | * @return ExtractorInterface |
||
| 412 | */ |
||
| 413 | public function getExtractor() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @codeCoverageIgnore |
||
| 420 | * @param ExtractorInterface $extractor |
||
| 421 | * @return Signal |
||
| 422 | */ |
||
| 423 | public function setExtractor(ExtractorInterface $extractor) |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Reloads signals cache and reinitializes component. |
||
| 430 | */ |
||
| 431 | public function resetCache() |
||
| 435 | |||
| 436 | private function reload() |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Get configured property |
||
| 443 | * @param string $property |
||
| 444 | * @return SignalAwareInterface |
||
| 445 | */ |
||
| 446 | 1 | private function getConfigured($property) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Set signal aware property |
||
| 465 | * @param SignalAwareInterface $object |
||
| 466 | * @param string $property |
||
| 467 | * @return Signal |
||
| 468 | */ |
||
| 469 | private function setConfigured(SignalAwareInterface $object, $property) |
||
| 475 | |||
| 476 | } |
||
| 477 |