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 | * Logger instance holder |
||
| 138 | * NOTE: There is property annotation with `logger` name, |
||
| 139 | * thus this name is a bit longer |
||
| 140 | * @var LoggerInterface |
||
| 141 | */ |
||
| 142 | private $loggerInstance = null; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Embedded dependency injection |
||
| 146 | * @var EmbeDi |
||
| 147 | */ |
||
| 148 | private $di = null; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Version |
||
| 152 | * @var string |
||
| 153 | */ |
||
| 154 | private $version = null; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Current filters |
||
| 158 | * @var PreFilterInterface[]|PostFilterInterface[] |
||
| 159 | */ |
||
| 160 | private $currentFilters = []; |
||
| 161 | |||
| 162 | 25 | public function __construct($configName = self::ConfigName) |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Getter |
||
| 177 | * @param string $name |
||
| 178 | * @return mixed |
||
| 179 | */ |
||
| 180 | public function __get($name) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Setter |
||
| 187 | * @param string $name |
||
| 188 | * @param mixed $value |
||
| 189 | * @return mixed |
||
| 190 | */ |
||
| 191 | public function __set($name, $value) |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get current signals version |
||
| 198 | * |
||
| 199 | * @codeCoverageIgnore |
||
| 200 | * @return string |
||
| 201 | */ |
||
| 202 | public function getVersion() |
||
| 210 | |||
| 211 | public function init() |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Apply filter to current emit. |
||
| 225 | * |
||
| 226 | * Pass false as param to disable all filters. |
||
| 227 | * |
||
| 228 | * @param FilterInterface|string|mixed $filter |
||
| 229 | * @return Signal |
||
| 230 | * @throws UnexpectedValueException |
||
| 231 | */ |
||
| 232 | 4 | public function filter($filter) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Emit signal to inform slots |
||
| 255 | * @param object|string $signal |
||
| 256 | * @return object[] |
||
| 257 | */ |
||
| 258 | 17 | public function emit($signal) |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Call for signals from slot |
||
| 309 | * @param object $slot |
||
| 310 | * @param string $interface Interface or class name which must be implemented, instanceof or sub class of to get into slot |
||
| 311 | */ |
||
| 312 | 7 | public function gather($slot, $interface = null) |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Get filters |
||
| 401 | * @param string $interface |
||
| 402 | * @return PreFilterInterface[]|PostFilterInterface[] |
||
| 403 | */ |
||
| 404 | 24 | public function getFilters($interface) |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Get logger |
||
| 420 | * @codeCoverageIgnore |
||
| 421 | * @return LoggerInterface |
||
| 422 | */ |
||
| 423 | public function getLogger() |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Set logger |
||
| 430 | * @codeCoverageIgnore |
||
| 431 | * @param LoggerInterface $logger |
||
| 432 | * @return Signal |
||
| 433 | */ |
||
| 434 | public function setLogger(LoggerInterface $logger) |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Get dependency injection container |
||
| 442 | * @return EmbeDi |
||
| 443 | */ |
||
| 444 | 24 | public function getDi() |
|
| 448 | |||
| 449 | public function setDi(EmbeDi $di) |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Get Input/Output adapter |
||
| 457 | * @codeCoverageIgnore |
||
| 458 | * @return BuilderIOInterface I/O Adapter |
||
| 459 | */ |
||
| 460 | public function getIO() |
||
| 464 | |||
| 465 | /** |
||
| 466 | * Set Input/Output interface |
||
| 467 | * @codeCoverageIgnore |
||
| 468 | * @param BuilderIOInterface $io |
||
| 469 | * @return Signal |
||
| 470 | */ |
||
| 471 | public function setIO(BuilderIOInterface $io) |
||
| 475 | |||
| 476 | /** |
||
| 477 | * @codeCoverageIgnore |
||
| 478 | * @return ExtractorInterface |
||
| 479 | */ |
||
| 480 | public function getExtractor() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @codeCoverageIgnore |
||
| 487 | * @param ExtractorInterface $extractor |
||
| 488 | * @return Signal |
||
| 489 | */ |
||
| 490 | public function setExtractor(ExtractorInterface $extractor) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Reloads signals cache and reinitializes component. |
||
| 497 | */ |
||
| 498 | public function resetCache() |
||
| 502 | |||
| 503 | private function reload() |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Get configured property |
||
| 510 | * @param string $property |
||
| 511 | * @return SignalAwareInterface |
||
| 512 | */ |
||
| 513 | 1 | private function getConfigured($property) |
|
| 529 | |||
| 530 | /** |
||
| 531 | * Set signal aware property |
||
| 532 | * @param SignalAwareInterface $object |
||
| 533 | * @param string $property |
||
| 534 | * @return Signal |
||
| 535 | */ |
||
| 536 | private function setConfigured(SignalAwareInterface $object, $property) |
||
| 542 | |||
| 543 | } |
||
| 544 |