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 constructor to initialize the instance. |
||
| 155 | * |
||
| 156 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger |
||
| 157 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 158 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 159 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 160 | * @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
||
| 161 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
| 162 | */ |
||
| 163 | 1 | public function __construct( |
|
| 183 | |||
| 184 | /** |
||
| 185 | * The shutdown handler to catch fatal errors. |
||
| 186 | * |
||
| 187 | * This method is need to make sure, that an existing PID file will be removed |
||
| 188 | * if a fatal error has been triggered. |
||
| 189 | * |
||
| 190 | * @return void |
||
| 191 | */ |
||
| 192 | public function shutdown() |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Return's the system logger. |
||
| 214 | * |
||
| 215 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
| 216 | */ |
||
| 217 | public function getSystemLogger() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 224 | * |
||
| 225 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
| 226 | */ |
||
| 227 | public function getRegistryProcessor() |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Return's the import processor instance. |
||
| 234 | * |
||
| 235 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
| 236 | */ |
||
| 237 | public function getImportProcessor() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * Return's the system configuration. |
||
| 244 | * |
||
| 245 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
| 246 | */ |
||
| 247 | public function getConfiguration() |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Return's the input stream to read console information from. |
||
| 254 | * |
||
| 255 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
| 256 | */ |
||
| 257 | public function getInput() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Return's the output stream to write console information to. |
||
| 264 | * |
||
| 265 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
| 266 | */ |
||
| 267 | 1 | public function getOutput() |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Return's the unique serial for this import process. |
||
| 274 | * |
||
| 275 | * @return string The unique serial |
||
| 276 | */ |
||
| 277 | public function getSerial() |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Persist the UUID of the actual import process to the PID file. |
||
| 284 | * |
||
| 285 | * @return void |
||
| 286 | * @throws \Exception Is thrown, if the PID can not be added |
||
| 287 | */ |
||
| 288 | public function lock() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Remove's the UUID of the actual import process from the PID file. |
||
| 313 | * |
||
| 314 | * @return void |
||
| 315 | * @throws \Exception Is thrown, if the PID can not be removed |
||
| 316 | */ |
||
| 317 | public function unlock() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * Remove's the passed line from the file with the passed name. |
||
| 336 | * |
||
| 337 | * @param string $line The line to be removed |
||
| 338 | * @param string $filename The name of the file the line has to be removed |
||
| 339 | * |
||
| 340 | * @return void |
||
| 341 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
| 342 | */ |
||
| 343 | public function removeLineFromFile($line, $filename) |
||
| 400 | |||
| 401 | /** |
||
| 402 | * Process the given operation. |
||
| 403 | * |
||
| 404 | * @return void |
||
| 405 | * @throws \Exception Is thrown if the operation can't be finished successfully |
||
| 406 | */ |
||
| 407 | public function process() |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Factory method to create new plugin instances. |
||
| 477 | * |
||
| 478 | * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance |
||
| 479 | * |
||
| 480 | * @return object The plugin instance |
||
| 481 | */ |
||
| 482 | protected function pluginFactory(PluginConfigurationInterface $pluginConfiguration) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Lifecycle callback that will be inovked before the |
||
| 494 | * import process has been started. |
||
| 495 | * |
||
| 496 | * @return void |
||
| 497 | */ |
||
| 498 | protected function setUp() |
||
| 548 | |||
| 549 | /** |
||
| 550 | * Lifecycle callback that will be inovked after the |
||
| 551 | * import process has been finished. |
||
| 552 | * |
||
| 553 | * @return void |
||
| 554 | */ |
||
| 555 | protected function tearDown() |
||
| 559 | |||
| 560 | /** |
||
| 561 | * Simple method that writes the passed method the the console and the |
||
| 562 | * system logger, if configured and a log level has been passed. |
||
| 563 | * |
||
| 564 | * @param string $msg The message to log |
||
| 565 | * @param string $logLevel The log level to use |
||
| 566 | * |
||
| 567 | * @return void |
||
| 568 | */ |
||
| 569 | protected function log($msg, $logLevel = null) |
||
| 586 | |||
| 587 | /** |
||
| 588 | * Map's the passed log level to a valid symfony console style. |
||
| 589 | * |
||
| 590 | * @param string $logLevel The log level to map |
||
| 591 | * |
||
| 592 | * @return string The apropriate symfony console style |
||
| 593 | */ |
||
| 594 | protected function mapLogLevelToStyle($logLevel) |
||
| 605 | |||
| 606 | /** |
||
| 607 | * Return's the PID filename to use. |
||
| 608 | * |
||
| 609 | * @return string The PID filename |
||
| 610 | */ |
||
| 611 | protected function getPidFilename() |
||
| 615 | } |
||
| 616 |
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..