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 |
||
| 52 | class Simple implements ApplicationInterface |
||
| 53 | { |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The default style to write messages to the symfony console. |
||
| 57 | * |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | const DEFAULT_STYLE = 'info'; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * The TechDivision company name as ANSI art. |
||
| 64 | * |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | protected $ansiArt = ' _______ _ _____ _ _ _ |
||
| 68 | |__ __| | | | __ \(_) (_) (_) |
||
| 69 | | | ___ ___| |__ | | | |___ ___ ___ _ ___ _ __ |
||
| 70 | | |/ _ \/ __| \'_ \| | | | \ \ / / / __| |/ _ \| \'_ \ |
||
| 71 | | | __/ (__| | | | |__| | |\ V /| \__ \ | (_) | | | | |
||
| 72 | |_|\___|\___|_| |_|_____/|_| \_/ |_|___/_|\___/|_| |_| |
||
| 73 | '; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * The log level => console style mapping. |
||
| 77 | * |
||
| 78 | * @var array |
||
| 79 | */ |
||
| 80 | protected $logLevelStyleMapping = array( |
||
| 81 | LogLevel::INFO => 'info', |
||
| 82 | LogLevel::DEBUG => 'comment', |
||
| 83 | LogLevel::ERROR => 'error', |
||
| 84 | LogLevel::ALERT => 'error', |
||
| 85 | LogLevel::CRITICAL => 'error', |
||
| 86 | LogLevel::EMERGENCY => 'error', |
||
| 87 | LogLevel::WARNING => 'error', |
||
| 88 | LogLevel::NOTICE => 'info' |
||
| 89 | ); |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The PID for the running processes. |
||
| 93 | * |
||
| 94 | * @var array |
||
| 95 | */ |
||
| 96 | protected $pid; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * The actions unique serial. |
||
| 100 | * |
||
| 101 | * @var string |
||
| 102 | */ |
||
| 103 | protected $serial; |
||
| 104 | |||
| 105 | /** |
||
| 106 | * The array with the system logger instances. |
||
| 107 | * |
||
| 108 | * @var array |
||
| 109 | */ |
||
| 110 | protected $systemLoggers; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * The RegistryProcessor instance to handle running threads. |
||
| 114 | * |
||
| 115 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 116 | */ |
||
| 117 | protected $registryProcessor; |
||
| 118 | |||
| 119 | /** |
||
| 120 | * The processor to read/write the necessary import data. |
||
| 121 | * |
||
| 122 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
| 123 | */ |
||
| 124 | protected $importProcessor; |
||
| 125 | |||
| 126 | /** |
||
| 127 | * The system configuration. |
||
| 128 | * |
||
| 129 | * @var \TechDivision\Import\ConfigurationInterface |
||
| 130 | */ |
||
| 131 | protected $configuration; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * The input stream to read console information from. |
||
| 135 | * |
||
| 136 | * @var \Symfony\Component\Console\Input\InputInterface |
||
| 137 | */ |
||
| 138 | protected $input; |
||
| 139 | |||
| 140 | /** |
||
| 141 | * The output stream to write console information to. |
||
| 142 | * |
||
| 143 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
| 144 | */ |
||
| 145 | protected $output; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * The plugins to be processed. |
||
| 149 | * |
||
| 150 | * @var array |
||
| 151 | */ |
||
| 152 | protected $plugins = array(); |
||
| 153 | |||
| 154 | /** |
||
| 155 | * The flag that stop's processing the operation. |
||
| 156 | * |
||
| 157 | * @var boolean |
||
| 158 | */ |
||
| 159 | protected $stopped = false; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * The filehandle for the PID file. |
||
| 163 | * |
||
| 164 | * @var resource |
||
| 165 | */ |
||
| 166 | protected $fh; |
||
| 167 | |||
| 168 | /** |
||
| 169 | * The constructor to initialize the instance. |
||
| 170 | * |
||
| 171 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 172 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 173 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 174 | * @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
||
| 175 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
| 176 | * @param array $systemLoggers The array with the system logger instances |
||
| 177 | */ |
||
| 178 | 1 | public function __construct( |
|
| 198 | |||
| 199 | /** |
||
| 200 | * The shutdown handler to catch fatal errors. |
||
| 201 | * |
||
| 202 | * This method is need to make sure, that an existing PID file will be removed |
||
| 203 | * if a fatal error has been triggered. |
||
| 204 | * |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | public function shutdown() |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Return's the logger with the passed name, by default the system logger. |
||
| 229 | * |
||
| 230 | * @param string $name The name of the requested system logger |
||
| 231 | * |
||
| 232 | * @return \Psr\Log\LoggerInterface The logger instance |
||
| 233 | * @throws \Exception Is thrown, if the requested logger is NOT available |
||
| 234 | */ |
||
| 235 | public function getSystemLogger($name = LoggerKeys::SYSTEM) |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Query whether or not the system logger with the passed name is available. |
||
| 249 | * |
||
| 250 | * @param string $name The name of the requested system logger |
||
| 251 | * |
||
| 252 | * @return boolean TRUE if the logger with the passed name exists, else FALSE |
||
| 253 | */ |
||
| 254 | public function hasSystemLogger($name = LoggerKeys::SYSTEM) |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Return's the array with the system logger instances. |
||
| 261 | * |
||
| 262 | * @return array The logger instance |
||
| 263 | */ |
||
| 264 | public function getSystemLoggers() |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 271 | * |
||
| 272 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
| 273 | */ |
||
| 274 | public function getRegistryProcessor() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Return's the import processor instance. |
||
| 281 | * |
||
| 282 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
| 283 | */ |
||
| 284 | public function getImportProcessor() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Return's the system configuration. |
||
| 291 | * |
||
| 292 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
| 293 | */ |
||
| 294 | public function getConfiguration() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Return's the input stream to read console information from. |
||
| 301 | * |
||
| 302 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
| 303 | */ |
||
| 304 | public function getInput() |
||
| 308 | |||
| 309 | /** |
||
| 310 | * Return's the output stream to write console information to. |
||
| 311 | * |
||
| 312 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
| 313 | */ |
||
| 314 | 1 | public function getOutput() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Return's the unique serial for this import process. |
||
| 321 | * |
||
| 322 | * @return string The unique serial |
||
| 323 | */ |
||
| 324 | public function getSerial() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Persist the UUID of the actual import process to the PID file. |
||
| 331 | * |
||
| 332 | * @return void |
||
| 333 | * @throws \Exception Is thrown, if the PID can not be locked or the PID can not be added |
||
| 334 | * @throws \TechDivision\Import\Exceptions\ImportAlreadyRunningException Is thrown, if a import process is already running |
||
| 335 | */ |
||
| 336 | public function lock() |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Remove's the UUID of the actual import process from the PID file. |
||
| 363 | * |
||
| 364 | * @return void |
||
| 365 | * @throws \Exception Is thrown, if the PID can not be removed |
||
| 366 | */ |
||
| 367 | public function unlock() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Remove's the passed line from the file with the passed name. |
||
| 396 | * |
||
| 397 | * @param string $line The line to be removed |
||
| 398 | * @param resource $fh The file handle of the file the line has to be removed |
||
| 399 | * |
||
| 400 | * @return void |
||
| 401 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
| 402 | */ |
||
| 403 | public function removeLineFromFile($line, $fh) |
||
| 445 | |||
| 446 | /** |
||
| 447 | * Process the given operation. |
||
| 448 | * |
||
| 449 | * @return void |
||
| 450 | * @throws \Exception Is thrown if the operation can't be finished successfully |
||
| 451 | */ |
||
| 452 | public function process() |
||
| 555 | |||
| 556 | /** |
||
| 557 | * Stop processing the operation. |
||
| 558 | * |
||
| 559 | * @param string $reason The reason why the operation has been stopped |
||
| 560 | * |
||
| 561 | * @return void |
||
| 562 | */ |
||
| 563 | public function stop($reason) |
||
| 572 | |||
| 573 | /** |
||
| 574 | * Return's TRUE if the operation has been stopped, else FALSE. |
||
| 575 | * |
||
| 576 | * @return boolean TRUE if the process has been stopped, else FALSE |
||
| 577 | */ |
||
| 578 | public function isStopped() |
||
| 582 | |||
| 583 | /** |
||
| 584 | * Factory method to create new plugin instances. |
||
| 585 | * |
||
| 586 | * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance |
||
| 587 | * |
||
| 588 | * @return object The plugin instance |
||
| 589 | */ |
||
| 590 | protected function pluginFactory(PluginConfigurationInterface $pluginConfiguration) |
||
| 599 | |||
| 600 | /** |
||
| 601 | * Lifecycle callback that will be inovked before the |
||
| 602 | * import process has been started. |
||
| 603 | * |
||
| 604 | * @return void |
||
| 605 | */ |
||
| 606 | protected function setUp() |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Lifecycle callback that will be inovked after the |
||
| 653 | * import process has been finished. |
||
| 654 | * |
||
| 655 | * @return void |
||
| 656 | */ |
||
| 657 | protected function tearDown() |
||
| 661 | |||
| 662 | /** |
||
| 663 | * Simple method that writes the passed method the the console and the |
||
| 664 | * system logger, if configured and a log level has been passed. |
||
| 665 | * |
||
| 666 | * @param string $msg The message to log |
||
| 667 | * @param string $logLevel The log level to use |
||
| 668 | * |
||
| 669 | * @return void |
||
| 670 | */ |
||
| 671 | protected function log($msg, $logLevel = null) |
||
| 688 | |||
| 689 | /** |
||
| 690 | * Map's the passed log level to a valid symfony console style. |
||
| 691 | * |
||
| 692 | * @param string $logLevel The log level to map |
||
| 693 | * |
||
| 694 | * @return string The apropriate symfony console style |
||
| 695 | */ |
||
| 696 | protected function mapLogLevelToStyle($logLevel) |
||
| 707 | |||
| 708 | /** |
||
| 709 | * Return's the PID filename to use. |
||
| 710 | * |
||
| 711 | * @return string The PID filename |
||
| 712 | */ |
||
| 713 | protected function getPidFilename() |
||
| 717 | } |
||
| 718 |
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..