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 |
||
| 54 | class Simple implements ApplicationInterface |
||
| 55 | { |
||
| 56 | |||
| 57 | /** |
||
| 58 | * The default style to write messages to the symfony console. |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | const DEFAULT_STYLE = 'info'; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * The TechDivision company name as ANSI art. |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $ansiArt = ' _______ _ _____ _ _ _ |
||
| 70 | |__ __| | | | __ \(_) (_) (_) |
||
| 71 | | | ___ ___| |__ | | | |___ ___ ___ _ ___ _ __ |
||
| 72 | | |/ _ \/ __| \'_ \| | | | \ \ / / / __| |/ _ \| \'_ \ |
||
| 73 | | | __/ (__| | | | |__| | |\ V /| \__ \ | (_) | | | | |
||
| 74 | |_|\___|\___|_| |_|_____/|_| \_/ |_|___/_|\___/|_| |_| |
||
| 75 | '; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The log level => console style mapping. |
||
| 79 | * |
||
| 80 | * @var array |
||
| 81 | */ |
||
| 82 | protected $logLevelStyleMapping = array( |
||
| 83 | LogLevel::INFO => 'info', |
||
| 84 | LogLevel::DEBUG => 'comment', |
||
| 85 | LogLevel::ERROR => 'error', |
||
| 86 | LogLevel::ALERT => 'error', |
||
| 87 | LogLevel::CRITICAL => 'error', |
||
| 88 | LogLevel::EMERGENCY => 'error', |
||
| 89 | LogLevel::WARNING => 'error', |
||
| 90 | LogLevel::NOTICE => 'info' |
||
| 91 | ); |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The PID for the running processes. |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | protected $pid; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The actions unique serial. |
||
| 102 | * |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | protected $serial; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The array with the system logger instances. |
||
| 109 | * |
||
| 110 | * @var array |
||
| 111 | */ |
||
| 112 | protected $systemLoggers; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The RegistryProcessor instance to handle running threads. |
||
| 116 | * |
||
| 117 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 118 | */ |
||
| 119 | protected $registryProcessor; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * The processor to read/write the necessary import data. |
||
| 123 | * |
||
| 124 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
| 125 | */ |
||
| 126 | protected $importProcessor; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * The DI container builder instance. |
||
| 130 | * |
||
| 131 | * @var \Symfony\Component\DependencyInjection\TaggedContainerInterface |
||
| 132 | */ |
||
| 133 | protected $container; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * The system configuration. |
||
| 137 | * |
||
| 138 | * @var \TechDivision\Import\ConfigurationInterface |
||
| 139 | */ |
||
| 140 | protected $configuration; |
||
| 141 | |||
| 142 | /** |
||
| 143 | * The output stream to write console information to. |
||
| 144 | * |
||
| 145 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
| 146 | */ |
||
| 147 | protected $output; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * The plugins to be processed. |
||
| 151 | * |
||
| 152 | * @var array |
||
| 153 | */ |
||
| 154 | protected $plugins = array(); |
||
| 155 | |||
| 156 | /** |
||
| 157 | * The flag that stop's processing the operation. |
||
| 158 | * |
||
| 159 | * @var boolean |
||
| 160 | */ |
||
| 161 | protected $stopped = false; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * The filehandle for the PID file. |
||
| 165 | * |
||
| 166 | * @var resource |
||
| 167 | */ |
||
| 168 | protected $fh; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * The plugin factory instance. |
||
| 172 | * |
||
| 173 | * @var \TechDivision\Import\Plugins\PluginFactoryInterface |
||
| 174 | */ |
||
| 175 | protected $pluginFactory; |
||
| 176 | |||
| 177 | /** |
||
| 178 | * The constructor to initialize the instance. |
||
| 179 | * |
||
| 180 | * @param \Symfony\Component\DependencyInjection\TaggedContainerInterface $container The DI container instance |
||
| 181 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 182 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 183 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 184 | * @param \TechDivision\Import\Plugins\PluginFactoryInterface $pluginFactory The plugin factory instance |
||
| 185 | * @param \Symfony\Component\Console\Output\OutputInterface $output The output instance |
||
| 186 | * @param array $systemLoggers The array with the system logger instances |
||
| 187 | */ |
||
| 188 | public function __construct( |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Set's the container instance. |
||
| 213 | * |
||
| 214 | * @param \Symfony\Component\DependencyInjection\TaggedContainerInterface $container The container instance |
||
| 215 | * |
||
| 216 | * @return void |
||
| 217 | */ |
||
| 218 | public function setContainer(TaggedContainerInterface $container) |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Return's the container instance. |
||
| 225 | * |
||
| 226 | * @return \Symfony\Component\DependencyInjection\TaggedContainerInterface The container instance |
||
| 227 | */ |
||
| 228 | public function getContainer() |
||
| 232 | |||
| 233 | /** |
||
| 234 | * Set's the output stream to write console information to. |
||
| 235 | * |
||
| 236 | * @param \Symfony\Component\Console\Output\OutputInterface $output The output stream |
||
| 237 | * |
||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | public function setOutput(OutputInterface $output) |
||
| 244 | |||
| 245 | /** |
||
| 246 | * Return's the output stream to write console information to. |
||
| 247 | * |
||
| 248 | * @return \Symfony\Component\Console\Output\OutputInterface The output stream |
||
| 249 | */ |
||
| 250 | public function getOutput() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Set's the system configuration. |
||
| 257 | * |
||
| 258 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 259 | * |
||
| 260 | * @return void |
||
| 261 | */ |
||
| 262 | public function setConfiguration(ConfigurationInterface $configuration) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Return's the system configuration. |
||
| 269 | * |
||
| 270 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
| 271 | */ |
||
| 272 | public function getConfiguration() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Set's the RegistryProcessor instance to handle the running threads. |
||
| 279 | * |
||
| 280 | * @param \TechDivision\Import\Services\RegistryProcessor $registryProcessor The registry processor instance |
||
| 281 | * |
||
| 282 | * @return void |
||
| 283 | */ |
||
| 284 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 291 | * |
||
| 292 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
| 293 | */ |
||
| 294 | public function getRegistryProcessor() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Set's the import processor instance. |
||
| 301 | * |
||
| 302 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 303 | * |
||
| 304 | * @return void |
||
| 305 | */ |
||
| 306 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Return's the import processor instance. |
||
| 313 | * |
||
| 314 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
| 315 | */ |
||
| 316 | public function getImportProcessor() |
||
| 320 | |||
| 321 | /** |
||
| 322 | * The array with the system loggers. |
||
| 323 | * |
||
| 324 | * @param array $systemLoggers The system logger instances |
||
| 325 | * |
||
| 326 | * @return void |
||
| 327 | */ |
||
| 328 | public function setSystemLoggers(array $systemLoggers) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * Set's the plugin factory instance. |
||
| 335 | * |
||
| 336 | * @param \TechDivision\Import\Plugins\PluginFactoryInterface $pluginFactory The plugin factory instance |
||
| 337 | * |
||
| 338 | * @return void |
||
| 339 | */ |
||
| 340 | public function setPluginFactory(PluginFactoryInterface $pluginFactory) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Return's the plugin factory instance. |
||
| 347 | * |
||
| 348 | * @return \TechDivision\Import\Plugins\PluginFactoryInterface The plugin factory instance |
||
| 349 | */ |
||
| 350 | public function getPluginFactory() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Return's the logger with the passed name, by default the system logger. |
||
| 357 | * |
||
| 358 | * @param string $name The name of the requested system logger |
||
| 359 | * |
||
| 360 | * @return \Psr\Log\LoggerInterface The logger instance |
||
| 361 | * @throws \Exception Is thrown, if the requested logger is NOT available |
||
| 362 | */ |
||
| 363 | public function getSystemLogger($name = LoggerKeys::SYSTEM) |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Query whether or not the system logger with the passed name is available. |
||
| 382 | * |
||
| 383 | * @param string $name The name of the requested system logger |
||
| 384 | * |
||
| 385 | * @return boolean TRUE if the logger with the passed name exists, else FALSE |
||
| 386 | */ |
||
| 387 | public function hasSystemLogger($name = LoggerKeys::SYSTEM) |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Return's the array with the system logger instances. |
||
| 394 | * |
||
| 395 | * @return array The logger instance |
||
| 396 | */ |
||
| 397 | public function getSystemLoggers() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Return's the unique serial for this import process. |
||
| 404 | * |
||
| 405 | * @return string The unique serial |
||
| 406 | */ |
||
| 407 | public function getSerial() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * The shutdown handler to catch fatal errors. |
||
| 414 | * |
||
| 415 | * This method is need to make sure, that an existing PID file will be removed |
||
| 416 | * if a fatal error has been triggered. |
||
| 417 | * |
||
| 418 | * @return void |
||
| 419 | */ |
||
| 420 | public function shutdown() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Persist the UUID of the actual import process to the PID file. |
||
| 442 | * |
||
| 443 | * @return void |
||
| 444 | * @throws \Exception Is thrown, if the PID can not be locked or the PID can not be added |
||
| 445 | * @throws \TechDivision\Import\Exceptions\ImportAlreadyRunningException Is thrown, if a import process is already running |
||
| 446 | */ |
||
| 447 | public function lock() |
||
| 471 | |||
| 472 | /** |
||
| 473 | * Remove's the UUID of the actual import process from the PID file. |
||
| 474 | * |
||
| 475 | * @return void |
||
| 476 | * @throws \Exception Is thrown, if the PID can not be removed |
||
| 477 | */ |
||
| 478 | public function unlock() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Remove's the passed line from the file with the passed name. |
||
| 507 | * |
||
| 508 | * @param string $line The line to be removed |
||
| 509 | * @param resource $fh The file handle of the file the line has to be removed |
||
| 510 | * |
||
| 511 | * @return void |
||
| 512 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
| 513 | */ |
||
| 514 | public function removeLineFromFile($line, $fh) |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Process the given operation. |
||
| 559 | * |
||
| 560 | * @return void |
||
| 561 | * @throws \Exception Is thrown if the operation can't be finished successfully |
||
| 562 | */ |
||
| 563 | public function process() |
||
| 667 | |||
| 668 | /** |
||
| 669 | * Stop processing the operation. |
||
| 670 | * |
||
| 671 | * @param string $reason The reason why the operation has been stopped |
||
| 672 | * |
||
| 673 | * @return void |
||
| 674 | */ |
||
| 675 | public function stop($reason) |
||
| 684 | |||
| 685 | /** |
||
| 686 | * Return's TRUE if the operation has been stopped, else FALSE. |
||
| 687 | * |
||
| 688 | * @return boolean TRUE if the process has been stopped, else FALSE |
||
| 689 | */ |
||
| 690 | public function isStopped() |
||
| 694 | |||
| 695 | /** |
||
| 696 | * Finds an entry of the container by its identifier and returns it. |
||
| 697 | * |
||
| 698 | * @param string $id Identifier of the entry to look for. |
||
| 699 | * |
||
| 700 | * @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
||
| 701 | * @throws ContainerExceptionInterface Error while retrieving the entry. |
||
| 702 | * |
||
| 703 | * @return mixed Entry. |
||
| 704 | */ |
||
| 705 | public function get($id) |
||
| 709 | |||
| 710 | /** |
||
| 711 | * Returns true if the container can return an entry for the given identifier. |
||
| 712 | * Returns false otherwise. |
||
| 713 | * |
||
| 714 | * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
||
| 715 | * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
||
| 716 | * |
||
| 717 | * @param string $id Identifier of the entry to look for. |
||
| 718 | * |
||
| 719 | * @return bool |
||
| 720 | */ |
||
| 721 | public function has($id) |
||
| 725 | |||
| 726 | /** |
||
| 727 | * Lifecycle callback that will be inovked before the |
||
| 728 | * import process has been started. |
||
| 729 | * |
||
| 730 | * @return void |
||
| 731 | */ |
||
| 732 | protected function setUp() |
||
| 780 | |||
| 781 | /** |
||
| 782 | * Lifecycle callback that will be inovked after the |
||
| 783 | * import process has been finished. |
||
| 784 | * |
||
| 785 | * @return void |
||
| 786 | */ |
||
| 787 | protected function tearDown() |
||
| 791 | |||
| 792 | /** |
||
| 793 | * Simple method that writes the passed method the the console and the |
||
| 794 | * system logger, if configured and a log level has been passed. |
||
| 795 | * |
||
| 796 | * @param string $msg The message to log |
||
| 797 | * @param string $logLevel The log level to use |
||
| 798 | * |
||
| 799 | * @return void |
||
| 800 | */ |
||
| 801 | protected function log($msg, $logLevel = null) |
||
| 818 | |||
| 819 | /** |
||
| 820 | * Map's the passed log level to a valid symfony console style. |
||
| 821 | * |
||
| 822 | * @param string $logLevel The log level to map |
||
| 823 | * |
||
| 824 | * @return string The apropriate symfony console style |
||
| 825 | */ |
||
| 826 | protected function mapLogLevelToStyle($logLevel) |
||
| 837 | |||
| 838 | /** |
||
| 839 | * Return's the PID filename to use. |
||
| 840 | * |
||
| 841 | * @return string The PID filename |
||
| 842 | */ |
||
| 843 | protected function getPidFilename() |
||
| 847 | } |
||
| 848 |
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..