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 log level => console style mapping. |
||
| 65 | * |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $logLevelStyleMapping = array( |
||
| 69 | LogLevel::INFO => 'info', |
||
| 70 | LogLevel::DEBUG => 'comment', |
||
| 71 | LogLevel::ERROR => 'error', |
||
| 72 | LogLevel::ALERT => 'error', |
||
| 73 | LogLevel::CRITICAL => 'error', |
||
| 74 | LogLevel::EMERGENCY => 'error', |
||
| 75 | LogLevel::WARNING => 'error', |
||
| 76 | LogLevel::NOTICE => 'info' |
||
| 77 | ); |
||
| 78 | |||
| 79 | /** |
||
| 80 | * The PID for the running processes. |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | protected $pid; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * The actions unique serial. |
||
| 88 | * |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | protected $serial; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * The array with the system logger instances. |
||
| 95 | * |
||
| 96 | * @var \Doctrine\Common\Collections\Collection |
||
| 97 | */ |
||
| 98 | protected $systemLoggers; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * The RegistryProcessor instance to handle running threads. |
||
| 102 | * |
||
| 103 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 104 | */ |
||
| 105 | protected $registryProcessor; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * The processor to read/write the necessary import data. |
||
| 109 | * |
||
| 110 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
| 111 | */ |
||
| 112 | protected $importProcessor; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The DI container builder instance. |
||
| 116 | * |
||
| 117 | * @var \Symfony\Component\DependencyInjection\TaggedContainerInterface |
||
| 118 | */ |
||
| 119 | protected $container; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * The system configuration. |
||
| 123 | * |
||
| 124 | * @var \TechDivision\Import\ConfigurationInterface |
||
| 125 | */ |
||
| 126 | protected $configuration; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * The output stream to write console information to. |
||
| 130 | * |
||
| 131 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
| 132 | */ |
||
| 133 | protected $output; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * The plugins to be processed. |
||
| 137 | * |
||
| 138 | * @var array |
||
| 139 | */ |
||
| 140 | protected $plugins = array(); |
||
| 141 | |||
| 142 | /** |
||
| 143 | * The flag that stop's processing the operation. |
||
| 144 | * |
||
| 145 | * @var boolean |
||
| 146 | */ |
||
| 147 | protected $stopped = false; |
||
| 148 | |||
| 149 | /** |
||
| 150 | * The filehandle for the PID file. |
||
| 151 | * |
||
| 152 | * @var resource |
||
| 153 | */ |
||
| 154 | protected $fh; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * The array with the module instances. |
||
| 158 | * |
||
| 159 | * @var \TechDivision\Import\Modules\ModuleInterface[] |
||
| 160 | */ |
||
| 161 | protected $modules; |
||
| 162 | |||
| 163 | /** |
||
| 164 | * The event emitter instance. |
||
| 165 | * |
||
| 166 | * @var \League\Event\EmitterInterface |
||
| 167 | */ |
||
| 168 | protected $emitter; |
||
| 169 | |||
| 170 | /** |
||
| 171 | * The constructor to initialize the instance. |
||
| 172 | * |
||
| 173 | * @param \Symfony\Component\DependencyInjection\TaggedContainerInterface $container The DI container instance |
||
| 174 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 175 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 176 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 177 | * @param \Symfony\Component\Console\Output\OutputInterface $output The output instance |
||
| 178 | * @param \Doctrine\Common\Collections\Collection $systemLoggers The array with the system logger instances |
||
| 179 | * @param \League\Event\EmitterInterface $emitter The event emitter instance |
||
| 180 | * @param \Traversable $modules The modules that provides the business logic |
||
| 181 | */ |
||
| 182 | public function __construct( |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Set's the event emitter instance. |
||
| 209 | * |
||
| 210 | * @param \League\Event\EmitterInterface $emitter The event emitter instance |
||
| 211 | * |
||
| 212 | * @return void |
||
| 213 | */ |
||
| 214 | public function setEmitter(EmitterInterface $emitter) |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Return's the event emitter instance. |
||
| 221 | * |
||
| 222 | * @return \League\Event\EmitterInterface The event emitter instance |
||
| 223 | */ |
||
| 224 | public function getEmitter() |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Set's the container instance. |
||
| 231 | * |
||
| 232 | * @param \Symfony\Component\DependencyInjection\TaggedContainerInterface $container The container instance |
||
| 233 | * |
||
| 234 | * @return void |
||
| 235 | */ |
||
| 236 | public function setContainer(TaggedContainerInterface $container) |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Return's the container instance. |
||
| 243 | * |
||
| 244 | * @return \Symfony\Component\DependencyInjection\TaggedContainerInterface The container instance |
||
| 245 | */ |
||
| 246 | public function getContainer() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Set's the output stream to write console information to. |
||
| 253 | * |
||
| 254 | * @param \Symfony\Component\Console\Output\OutputInterface $output The output stream |
||
| 255 | * |
||
| 256 | * @return void |
||
| 257 | */ |
||
| 258 | public function setOutput(OutputInterface $output) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Return's the output stream to write console information to. |
||
| 265 | * |
||
| 266 | * @return \Symfony\Component\Console\Output\OutputInterface The output stream |
||
| 267 | */ |
||
| 268 | public function getOutput() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Set's the system configuration. |
||
| 275 | * |
||
| 276 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
| 277 | * |
||
| 278 | * @return void |
||
| 279 | */ |
||
| 280 | public function setConfiguration(ConfigurationInterface $configuration) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Return's the system configuration. |
||
| 287 | * |
||
| 288 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
| 289 | */ |
||
| 290 | public function getConfiguration() |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Set's the RegistryProcessor instance to handle the running threads. |
||
| 297 | * |
||
| 298 | * @param \TechDivision\Import\Services\RegistryProcessor $registryProcessor The registry processor instance |
||
| 299 | * |
||
| 300 | * @return void |
||
| 301 | */ |
||
| 302 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 309 | * |
||
| 310 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
| 311 | */ |
||
| 312 | public function getRegistryProcessor() |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Set's the import processor instance. |
||
| 319 | * |
||
| 320 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 321 | * |
||
| 322 | * @return void |
||
| 323 | */ |
||
| 324 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Return's the import processor instance. |
||
| 331 | * |
||
| 332 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
| 333 | */ |
||
| 334 | public function getImportProcessor() |
||
| 338 | |||
| 339 | /** |
||
| 340 | * The array with the system loggers. |
||
| 341 | * |
||
| 342 | * @param \Doctrine\Common\Collections\Collection $systemLoggers The system logger instances |
||
| 343 | * |
||
| 344 | * @return void |
||
| 345 | */ |
||
| 346 | public function setSystemLoggers(Collection $systemLoggers) |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Set's the module instances. |
||
| 353 | * |
||
| 354 | * @param \Traversable $modules The modules instances |
||
| 355 | * |
||
| 356 | * @return void |
||
| 357 | */ |
||
| 358 | public function setModules(\Traversable $modules) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Return's the module instances. |
||
| 365 | * |
||
| 366 | * @return \Traversable The module instances |
||
| 367 | */ |
||
| 368 | public function getModules() |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Return's the logger with the passed name, by default the system logger. |
||
| 375 | * |
||
| 376 | * @param string $name The name of the requested system logger |
||
| 377 | * |
||
| 378 | * @return \Psr\Log\LoggerInterface The logger instance |
||
| 379 | * @throws \Exception Is thrown, if the requested logger is NOT available |
||
| 380 | */ |
||
| 381 | public function getSystemLogger($name = LoggerKeys::SYSTEM) |
||
| 397 | |||
| 398 | /** |
||
| 399 | * Returns the actual application version. |
||
| 400 | * |
||
| 401 | * @return string The application's version |
||
| 402 | */ |
||
| 403 | public function getVersion() |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Returns the actual application name. |
||
| 410 | * |
||
| 411 | * @return string The application's name |
||
| 412 | */ |
||
| 413 | public function getName() |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Query whether or not the system logger with the passed name is available. |
||
| 420 | * |
||
| 421 | * @param string $name The name of the requested system logger |
||
| 422 | * |
||
| 423 | * @return boolean TRUE if the logger with the passed name exists, else FALSE |
||
| 424 | */ |
||
| 425 | public function hasSystemLogger($name = LoggerKeys::SYSTEM) |
||
| 429 | |||
| 430 | /** |
||
| 431 | * Return's the array with the system logger instances. |
||
| 432 | * |
||
| 433 | * @return \Doctrine\Common\Collections\Collection The logger instance |
||
| 434 | */ |
||
| 435 | public function getSystemLoggers() |
||
| 439 | |||
| 440 | /** |
||
| 441 | * Return's the unique serial for this import process. |
||
| 442 | * |
||
| 443 | * @return string The unique serial |
||
| 444 | */ |
||
| 445 | public function getSerial() |
||
| 449 | |||
| 450 | /** |
||
| 451 | * The shutdown handler to catch fatal errors. |
||
| 452 | * |
||
| 453 | * This method is need to make sure, that an existing PID file will be removed |
||
| 454 | * if a fatal error has been triggered. |
||
| 455 | * |
||
| 456 | * @return void |
||
| 457 | */ |
||
| 458 | public function shutdown() |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Persist the UUID of the actual import process to the PID file. |
||
| 480 | * |
||
| 481 | * @return void |
||
| 482 | * @throws \Exception Is thrown, if the PID can not be locked or the PID can not be added |
||
| 483 | * @throws \TechDivision\Import\Exceptions\ImportAlreadyRunningException Is thrown, if a import process is already running |
||
| 484 | */ |
||
| 485 | public function lock() |
||
| 509 | |||
| 510 | /** |
||
| 511 | * Remove's the UUID of the actual import process from the PID file. |
||
| 512 | * |
||
| 513 | * @return void |
||
| 514 | * @throws \Exception Is thrown, if the PID can not be removed |
||
| 515 | */ |
||
| 516 | public function unlock() |
||
| 541 | |||
| 542 | /** |
||
| 543 | * Remove's the passed line from the file with the passed name. |
||
| 544 | * |
||
| 545 | * @param string $line The line to be removed |
||
| 546 | * @param resource $fh The file handle of the file the line has to be removed |
||
| 547 | * |
||
| 548 | * @return void |
||
| 549 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
| 550 | */ |
||
| 551 | public function removeLineFromFile($line, $fh) |
||
| 593 | |||
| 594 | /** |
||
| 595 | * Process the given operation. |
||
| 596 | * |
||
| 597 | * @param string $serial The unique serial of the actual import process |
||
| 598 | * |
||
| 599 | * @return null|int null or 0 if everything went fine, or an error code |
||
| 600 | * @throws \Exception Is thrown if the operation can't be finished successfully |
||
| 601 | */ |
||
| 602 | public function process($serial) |
||
| 770 | |||
| 771 | /** |
||
| 772 | * Stop processing the operation. |
||
| 773 | * |
||
| 774 | * @param string $reason The reason why the operation has been stopped |
||
| 775 | * |
||
| 776 | * @return void |
||
| 777 | * @throws \TechDivision\Import\Exceptions\ApplicationStoppedException Is thrown if the application has been stopped |
||
| 778 | */ |
||
| 779 | public function stop($reason) |
||
| 788 | |||
| 789 | /** |
||
| 790 | * Return's TRUE if the operation has been stopped, else FALSE. |
||
| 791 | * |
||
| 792 | * @return boolean TRUE if the process has been stopped, else FALSE |
||
| 793 | */ |
||
| 794 | public function isStopped() |
||
| 798 | |||
| 799 | /** |
||
| 800 | * Gets a service. |
||
| 801 | * |
||
| 802 | * @param string $id The service identifier |
||
| 803 | * |
||
| 804 | * @return object The associated service |
||
| 805 | * |
||
| 806 | * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException When a circular reference is detected |
||
| 807 | * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException When the service is not defined |
||
| 808 | */ |
||
| 809 | public function get($id) |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Returns true if the container can return an entry for the given identifier. |
||
| 816 | * Returns false otherwise. |
||
| 817 | * |
||
| 818 | * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
||
| 819 | * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
||
| 820 | * |
||
| 821 | * @param string $id Identifier of the entry to look for. |
||
| 822 | * |
||
| 823 | * @return bool |
||
| 824 | */ |
||
| 825 | public function has($id) |
||
| 829 | |||
| 830 | /** |
||
| 831 | * Lifecycle callback that will be inovked before the |
||
| 832 | * import process has been started. |
||
| 833 | * |
||
| 834 | * @return void |
||
| 835 | */ |
||
| 836 | protected function setUp() |
||
| 840 | |||
| 841 | /** |
||
| 842 | * Lifecycle callback that will be inovked after the |
||
| 843 | * import process has been finished. |
||
| 844 | * |
||
| 845 | * @return void |
||
| 846 | */ |
||
| 847 | protected function tearDown() |
||
| 851 | |||
| 852 | /** |
||
| 853 | * Simple method that writes the passed method the the console and the |
||
| 854 | * system logger, if configured and a log level has been passed. |
||
| 855 | * |
||
| 856 | * @param string $msg The message to log |
||
| 857 | * @param string $logLevel The log level to use |
||
| 858 | * |
||
| 859 | * @return void |
||
| 860 | */ |
||
| 861 | public function log($msg, $logLevel = null) |
||
| 878 | |||
| 879 | /** |
||
| 880 | * Map's the passed log level to a valid symfony console style. |
||
| 881 | * |
||
| 882 | * @param string $logLevel The log level to map |
||
| 883 | * |
||
| 884 | * @return string The apropriate symfony console style |
||
| 885 | */ |
||
| 886 | protected function mapLogLevelToStyle($logLevel) |
||
| 897 | |||
| 898 | /** |
||
| 899 | * Return's the PID filename to use. |
||
| 900 | * |
||
| 901 | * @return string The PID filename |
||
| 902 | */ |
||
| 903 | protected function getPidFilename() |
||
| 907 | } |
||
| 908 |
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..