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 |
||
| 53 | class Simple implements ApplicationInterface |
||
| 54 | { |
||
| 55 | |||
| 56 | /** |
||
| 57 | * The default style to write messages to the symfony console. |
||
| 58 | * |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | const DEFAULT_STYLE = 'info'; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * The TechDivision company name as ANSI art. |
||
| 65 | * |
||
| 66 | * @var string |
||
| 67 | */ |
||
| 68 | protected $ansiArt = ' _______ _ _____ _ _ _ |
||
| 69 | |__ __| | | | __ \(_) (_) (_) |
||
| 70 | | | ___ ___| |__ | | | |___ ___ ___ _ ___ _ __ |
||
| 71 | | |/ _ \/ __| \'_ \| | | | \ \ / / / __| |/ _ \| \'_ \ |
||
| 72 | | | __/ (__| | | | |__| | |\ V /| \__ \ | (_) | | | | |
||
| 73 | |_|\___|\___|_| |_|_____/|_| \_/ |_|___/_|\___/|_| |_| |
||
| 74 | '; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * The log level => console style mapping. |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | protected $logLevelStyleMapping = array( |
||
| 82 | LogLevel::INFO => 'info', |
||
| 83 | LogLevel::DEBUG => 'comment', |
||
| 84 | LogLevel::ERROR => 'error', |
||
| 85 | LogLevel::ALERT => 'error', |
||
| 86 | LogLevel::CRITICAL => 'error', |
||
| 87 | LogLevel::EMERGENCY => 'error', |
||
| 88 | LogLevel::WARNING => 'error', |
||
| 89 | LogLevel::NOTICE => 'info' |
||
| 90 | ); |
||
| 91 | |||
| 92 | /** |
||
| 93 | * The PID for the running processes. |
||
| 94 | * |
||
| 95 | * @var array |
||
| 96 | */ |
||
| 97 | protected $pid; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * The actions unique serial. |
||
| 101 | * |
||
| 102 | * @var string |
||
| 103 | */ |
||
| 104 | protected $serial; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * The array with the system logger instances. |
||
| 108 | * |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | protected $systemLoggers; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * The RegistryProcessor instance to handle running threads. |
||
| 115 | * |
||
| 116 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 117 | */ |
||
| 118 | protected $registryProcessor; |
||
| 119 | |||
| 120 | /** |
||
| 121 | * The processor to read/write the necessary import data. |
||
| 122 | * |
||
| 123 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
| 124 | */ |
||
| 125 | protected $importProcessor; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * The DI container builder instance. |
||
| 129 | * |
||
| 130 | * @var \Symfony\Component\DependencyInjection\TaggedContainerInterface |
||
| 131 | */ |
||
| 132 | protected $container; |
||
| 133 | |||
| 134 | /** |
||
| 135 | * The system configuration. |
||
| 136 | * |
||
| 137 | * @var \TechDivision\Import\ConfigurationInterface |
||
| 138 | */ |
||
| 139 | protected $configuration; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * The output stream to write console information to. |
||
| 143 | * |
||
| 144 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
| 145 | */ |
||
| 146 | protected $output; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * The plugins to be processed. |
||
| 150 | * |
||
| 151 | * @var array |
||
| 152 | */ |
||
| 153 | protected $plugins = array(); |
||
| 154 | |||
| 155 | /** |
||
| 156 | * The flag that stop's processing the operation. |
||
| 157 | * |
||
| 158 | * @var boolean |
||
| 159 | */ |
||
| 160 | protected $stopped = false; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * The filehandle for the PID file. |
||
| 164 | * |
||
| 165 | * @var resource |
||
| 166 | */ |
||
| 167 | protected $fh; |
||
| 168 | |||
| 169 | /** |
||
| 170 | * The constructor to initialize the instance. |
||
| 171 | * |
||
| 172 | * @param \Symfony\Component\DependencyInjection\TaggedContainerInterface $container The DI container instance |
||
| 173 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 174 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 175 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 176 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
| 177 | * @param array $systemLoggers The array with the system logger instances |
||
| 178 | */ |
||
| 179 | public function __construct( |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Set's the container instance. |
||
| 202 | * |
||
| 203 | * @param \Symfony\Component\DependencyInjection\TaggedContainerInterface $container The container instance |
||
| 204 | * |
||
| 205 | * @return void |
||
| 206 | */ |
||
| 207 | public function setContainer(TaggedContainerInterface $container) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Return's the container instance. |
||
| 214 | * |
||
| 215 | * @return \Symfony\Component\DependencyInjection\TaggedContainerInterface The container instance |
||
| 216 | */ |
||
| 217 | public function getContainer() |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Set's the output stream to write console information to. |
||
| 224 | * |
||
| 225 | * @param \Symfony\Component\Console\Output\OutputInterface $output The output stream |
||
| 226 | * |
||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | public function setOutput(OutputInterface $output) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Return's the output stream to write console information to. |
||
| 236 | * |
||
| 237 | * @return \Symfony\Component\Console\Output\OutputInterface The output stream |
||
| 238 | */ |
||
| 239 | public function getOutput() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * Set's the system configuration. |
||
| 246 | * |
||
| 247 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 248 | * |
||
| 249 | * @return void |
||
| 250 | */ |
||
| 251 | public function setConfiguration(ConfigurationInterface $configuration) |
||
| 255 | |||
| 256 | /** |
||
| 257 | * Return's the system configuration. |
||
| 258 | * |
||
| 259 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
| 260 | */ |
||
| 261 | public function getConfiguration() |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Set's the RegistryProcessor instance to handle the running threads. |
||
| 268 | * |
||
| 269 | * @param \TechDivision\Import\Services\RegistryProcessor $registryProcessor The registry processor instance |
||
| 270 | * |
||
| 271 | * @return void |
||
| 272 | */ |
||
| 273 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 280 | * |
||
| 281 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
| 282 | */ |
||
| 283 | public function getRegistryProcessor() |
||
| 287 | |||
| 288 | /** |
||
| 289 | * Set's the import processor instance. |
||
| 290 | * |
||
| 291 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 292 | * |
||
| 293 | * @return void |
||
| 294 | */ |
||
| 295 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Return's the import processor instance. |
||
| 302 | * |
||
| 303 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
| 304 | */ |
||
| 305 | public function getImportProcessor() |
||
| 309 | |||
| 310 | /** |
||
| 311 | * The array with the system loggers. |
||
| 312 | * |
||
| 313 | * @param array $systemLoggers The system logger instances |
||
| 314 | * |
||
| 315 | * @return void |
||
| 316 | */ |
||
| 317 | public function setSystemLoggers(array $systemLoggers) |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Return's the logger with the passed name, by default the system logger. |
||
| 324 | * |
||
| 325 | * @param string $name The name of the requested system logger |
||
| 326 | * |
||
| 327 | * @return \Psr\Log\LoggerInterface The logger instance |
||
| 328 | * @throws \Exception Is thrown, if the requested logger is NOT available |
||
| 329 | */ |
||
| 330 | public function getSystemLogger($name = LoggerKeys::SYSTEM) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Query whether or not the system logger with the passed name is available. |
||
| 349 | * |
||
| 350 | * @param string $name The name of the requested system logger |
||
| 351 | * |
||
| 352 | * @return boolean TRUE if the logger with the passed name exists, else FALSE |
||
| 353 | */ |
||
| 354 | public function hasSystemLogger($name = LoggerKeys::SYSTEM) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * Return's the array with the system logger instances. |
||
| 361 | * |
||
| 362 | * @return array The logger instance |
||
| 363 | */ |
||
| 364 | public function getSystemLoggers() |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Return's the unique serial for this import process. |
||
| 371 | * |
||
| 372 | * @return string The unique serial |
||
| 373 | */ |
||
| 374 | public function getSerial() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * The shutdown handler to catch fatal errors. |
||
| 381 | * |
||
| 382 | * This method is need to make sure, that an existing PID file will be removed |
||
| 383 | * if a fatal error has been triggered. |
||
| 384 | * |
||
| 385 | * @return void |
||
| 386 | */ |
||
| 387 | public function shutdown() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Persist the UUID of the actual import process to the PID file. |
||
| 409 | * |
||
| 410 | * @return void |
||
| 411 | * @throws \Exception Is thrown, if the PID can not be locked or the PID can not be added |
||
| 412 | * @throws \TechDivision\Import\Exceptions\ImportAlreadyRunningException Is thrown, if a import process is already running |
||
| 413 | */ |
||
| 414 | public function lock() |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Remove's the UUID of the actual import process from the PID file. |
||
| 441 | * |
||
| 442 | * @return void |
||
| 443 | * @throws \Exception Is thrown, if the PID can not be removed |
||
| 444 | */ |
||
| 445 | public function unlock() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Remove's the passed line from the file with the passed name. |
||
| 474 | * |
||
| 475 | * @param string $line The line to be removed |
||
| 476 | * @param resource $fh The file handle of the file the line has to be removed |
||
| 477 | * |
||
| 478 | * @return void |
||
| 479 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
| 480 | */ |
||
| 481 | public function removeLineFromFile($line, $fh) |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Process the given operation. |
||
| 526 | * |
||
| 527 | * @return void |
||
| 528 | * @throws \Exception Is thrown if the operation can't be finished successfully |
||
| 529 | */ |
||
| 530 | public function process() |
||
| 633 | |||
| 634 | /** |
||
| 635 | * Stop processing the operation. |
||
| 636 | * |
||
| 637 | * @param string $reason The reason why the operation has been stopped |
||
| 638 | * |
||
| 639 | * @return void |
||
| 640 | */ |
||
| 641 | public function stop($reason) |
||
| 650 | |||
| 651 | /** |
||
| 652 | * Return's TRUE if the operation has been stopped, else FALSE. |
||
| 653 | * |
||
| 654 | * @return boolean TRUE if the process has been stopped, else FALSE |
||
| 655 | */ |
||
| 656 | public function isStopped() |
||
| 660 | |||
| 661 | /** |
||
| 662 | * Factory method to create new plugin instances. |
||
| 663 | * |
||
| 664 | * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance |
||
| 665 | * |
||
| 666 | * @return object The plugin instance |
||
| 667 | */ |
||
| 668 | protected function pluginFactory(PluginConfigurationInterface $pluginConfiguration) |
||
| 673 | |||
| 674 | /** |
||
| 675 | * Lifecycle callback that will be inovked before the |
||
| 676 | * import process has been started. |
||
| 677 | * |
||
| 678 | * @return void |
||
| 679 | */ |
||
| 680 | protected function setUp() |
||
| 728 | |||
| 729 | /** |
||
| 730 | * Lifecycle callback that will be inovked after the |
||
| 731 | * import process has been finished. |
||
| 732 | * |
||
| 733 | * @return void |
||
| 734 | */ |
||
| 735 | protected function tearDown() |
||
| 739 | |||
| 740 | /** |
||
| 741 | * Simple method that writes the passed method the the console and the |
||
| 742 | * system logger, if configured and a log level has been passed. |
||
| 743 | * |
||
| 744 | * @param string $msg The message to log |
||
| 745 | * @param string $logLevel The log level to use |
||
| 746 | * |
||
| 747 | * @return void |
||
| 748 | */ |
||
| 749 | protected function log($msg, $logLevel = null) |
||
| 766 | |||
| 767 | /** |
||
| 768 | * Map's the passed log level to a valid symfony console style. |
||
| 769 | * |
||
| 770 | * @param string $logLevel The log level to map |
||
| 771 | * |
||
| 772 | * @return string The apropriate symfony console style |
||
| 773 | */ |
||
| 774 | protected function mapLogLevelToStyle($logLevel) |
||
| 785 | |||
| 786 | /** |
||
| 787 | * Return's the PID filename to use. |
||
| 788 | * |
||
| 789 | * @return string The PID filename |
||
| 790 | */ |
||
| 791 | protected function getPidFilename() |
||
| 795 | } |
||
| 796 |
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..