Complex classes like Simple 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 Simple, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 51 | class Simple implements ApplicationInterface |
||
| 52 | { |
||
| 53 | |||
| 54 | /** |
||
| 55 | * The default style to write messages to the symfony console. |
||
| 56 | * |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | const DEFAULT_STYLE = 'info'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * The TechDivision company name as ANSI art. |
||
| 63 | * |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | protected $ansiArt = ' _______ _ _____ _ _ _ |
||
| 67 | |__ __| | | | __ \(_) (_) (_) |
||
| 68 | | | ___ ___| |__ | | | |___ ___ ___ _ ___ _ __ |
||
| 69 | | |/ _ \/ __| \'_ \| | | | \ \ / / / __| |/ _ \| \'_ \ |
||
| 70 | | | __/ (__| | | | |__| | |\ V /| \__ \ | (_) | | | | |
||
| 71 | |_|\___|\___|_| |_|_____/|_| \_/ |_|___/_|\___/|_| |_| |
||
| 72 | '; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * The log level => console style mapping. |
||
| 76 | * |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $logLevelStyleMapping = array( |
||
| 80 | LogLevel::INFO => 'info', |
||
| 81 | LogLevel::DEBUG => 'comment', |
||
| 82 | LogLevel::ERROR => 'error', |
||
| 83 | LogLevel::ALERT => 'error', |
||
| 84 | LogLevel::CRITICAL => 'error', |
||
| 85 | LogLevel::EMERGENCY => 'error', |
||
| 86 | LogLevel::WARNING => 'error', |
||
| 87 | LogLevel::NOTICE => 'info' |
||
| 88 | ); |
||
| 89 | |||
| 90 | /** |
||
| 91 | * The PID for the running processes. |
||
| 92 | * |
||
| 93 | * @var array |
||
| 94 | */ |
||
| 95 | protected $pid; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * The actions unique serial. |
||
| 99 | * |
||
| 100 | * @var string |
||
| 101 | */ |
||
| 102 | protected $serial; |
||
| 103 | |||
| 104 | /** |
||
| 105 | * The system logger implementation. |
||
| 106 | * |
||
| 107 | * @var \Psr\Log\LoggerInterface |
||
| 108 | */ |
||
| 109 | protected $systemLogger; |
||
| 110 | |||
| 111 | /** |
||
| 112 | * The RegistryProcessor instance to handle running threads. |
||
| 113 | * |
||
| 114 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 115 | */ |
||
| 116 | protected $registryProcessor; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * The processor to read/write the necessary import data. |
||
| 120 | * |
||
| 121 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
| 122 | */ |
||
| 123 | protected $importProcessor; |
||
| 124 | |||
| 125 | /** |
||
| 126 | * The system configuration. |
||
| 127 | * |
||
| 128 | * @var \TechDivision\Import\ConfigurationInterface |
||
| 129 | */ |
||
| 130 | protected $configuration; |
||
| 131 | |||
| 132 | /** |
||
| 133 | * The input stream to read console information from. |
||
| 134 | * |
||
| 135 | * @var \Symfony\Component\Console\Input\InputInterface |
||
| 136 | */ |
||
| 137 | protected $input; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * The output stream to write console information to. |
||
| 141 | * |
||
| 142 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
| 143 | */ |
||
| 144 | protected $output; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * The plugins to be processed. |
||
| 148 | * |
||
| 149 | * @var array |
||
| 150 | */ |
||
| 151 | protected $plugins = array(); |
||
| 152 | |||
| 153 | /** |
||
| 154 | * The flag that stop's processing the operation. |
||
| 155 | * |
||
| 156 | * @var boolean |
||
| 157 | */ |
||
| 158 | protected $stopped = false; |
||
| 159 | |||
| 160 | /** |
||
| 161 | * The constructor to initialize the instance. |
||
| 162 | * |
||
| 163 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger |
||
| 164 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 165 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 166 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 167 | * @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
||
| 168 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
| 169 | */ |
||
| 170 | 1 | public function __construct( |
|
| 190 | |||
| 191 | /** |
||
| 192 | * The shutdown handler to catch fatal errors. |
||
| 193 | * |
||
| 194 | * This method is need to make sure, that an existing PID file will be removed |
||
| 195 | * if a fatal error has been triggered. |
||
| 196 | * |
||
| 197 | * @return void |
||
| 198 | */ |
||
| 199 | public function shutdown() |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Return's the system logger. |
||
| 221 | * |
||
| 222 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
| 223 | */ |
||
| 224 | public function getSystemLogger() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 231 | * |
||
| 232 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
| 233 | */ |
||
| 234 | public function getRegistryProcessor() |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Return's the import processor instance. |
||
| 241 | * |
||
| 242 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
| 243 | */ |
||
| 244 | public function getImportProcessor() |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Return's the system configuration. |
||
| 251 | * |
||
| 252 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
| 253 | */ |
||
| 254 | public function getConfiguration() |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Return's the input stream to read console information from. |
||
| 261 | * |
||
| 262 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
| 263 | */ |
||
| 264 | public function getInput() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Return's the output stream to write console information to. |
||
| 271 | * |
||
| 272 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
| 273 | */ |
||
| 274 | 1 | public function getOutput() |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Return's the unique serial for this import process. |
||
| 281 | * |
||
| 282 | * @return string The unique serial |
||
| 283 | */ |
||
| 284 | public function getSerial() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Persist the UUID of the actual import process to the PID file. |
||
| 291 | * |
||
| 292 | * @return void |
||
| 293 | * @throws \Exception Is thrown, if the PID can not be added |
||
| 294 | */ |
||
| 295 | public function lock() |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Remove's the UUID of the actual import process from the PID file. |
||
| 320 | * |
||
| 321 | * @return void |
||
| 322 | * @throws \Exception Is thrown, if the PID can not be removed |
||
| 323 | */ |
||
| 324 | public function unlock() |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Remove's the passed line from the file with the passed name. |
||
| 343 | * |
||
| 344 | * @param string $line The line to be removed |
||
| 345 | * @param string $filename The name of the file the line has to be removed |
||
| 346 | * |
||
| 347 | * @return void |
||
| 348 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
| 349 | */ |
||
| 350 | public function removeLineFromFile($line, $filename) |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Process the given operation. |
||
| 410 | * |
||
| 411 | * @return void |
||
| 412 | * @throws \Exception Is thrown if the operation can't be finished successfully |
||
| 413 | */ |
||
| 414 | public function process() |
||
| 486 | |||
| 487 | /** |
||
| 488 | * Stop processing the operation. |
||
| 489 | * |
||
| 490 | * @param string $reason The reason why the operation has been stopped |
||
| 491 | * |
||
| 492 | * @return void |
||
| 493 | */ |
||
| 494 | public function stop($reason) |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Return's TRUE if the operation has been stopped, else FALSE. |
||
| 506 | * |
||
| 507 | * @return boolean TRUE if the process has been stopped, else FALSE |
||
| 508 | */ |
||
| 509 | public function isStopped() |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Factory method to create new plugin instances. |
||
| 516 | * |
||
| 517 | * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance |
||
| 518 | * |
||
| 519 | * @return object The plugin instance |
||
| 520 | */ |
||
| 521 | protected function pluginFactory(PluginConfigurationInterface $pluginConfiguration) |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Lifecycle callback that will be inovked before the |
||
| 533 | * import process has been started. |
||
| 534 | * |
||
| 535 | * @return void |
||
| 536 | */ |
||
| 537 | protected function setUp() |
||
| 587 | |||
| 588 | /** |
||
| 589 | * Lifecycle callback that will be inovked after the |
||
| 590 | * import process has been finished. |
||
| 591 | * |
||
| 592 | * @return void |
||
| 593 | */ |
||
| 594 | protected function tearDown() |
||
| 598 | |||
| 599 | /** |
||
| 600 | * Simple method that writes the passed method the the console and the |
||
| 601 | * system logger, if configured and a log level has been passed. |
||
| 602 | * |
||
| 603 | * @param string $msg The message to log |
||
| 604 | * @param string $logLevel The log level to use |
||
| 605 | * |
||
| 606 | * @return void |
||
| 607 | */ |
||
| 608 | protected function log($msg, $logLevel = null) |
||
| 625 | |||
| 626 | /** |
||
| 627 | * Map's the passed log level to a valid symfony console style. |
||
| 628 | * |
||
| 629 | * @param string $logLevel The log level to map |
||
| 630 | * |
||
| 631 | * @return string The apropriate symfony console style |
||
| 632 | */ |
||
| 633 | protected function mapLogLevelToStyle($logLevel) |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Return's the PID filename to use. |
||
| 647 | * |
||
| 648 | * @return string The PID filename |
||
| 649 | */ |
||
| 650 | protected function getPidFilename() |
||
| 654 | } |
||
| 655 |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..