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 log level => console style mapping. |
||
| 66 | * |
||
| 67 | * @var array |
||
| 68 | */ |
||
| 69 | protected $logLevelStyleMapping = array( |
||
| 70 | LogLevel::INFO => 'info', |
||
| 71 | LogLevel::DEBUG => 'comment', |
||
| 72 | LogLevel::ERROR => 'error', |
||
| 73 | LogLevel::ALERT => 'error', |
||
| 74 | LogLevel::CRITICAL => 'error', |
||
| 75 | LogLevel::EMERGENCY => 'error', |
||
| 76 | LogLevel::WARNING => 'error', |
||
| 77 | LogLevel::NOTICE => 'info' |
||
| 78 | ); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * The PID for the running processes. |
||
| 82 | * |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $pid; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * The actions unique serial. |
||
| 89 | * |
||
| 90 | * @var string |
||
| 91 | */ |
||
| 92 | protected $serial; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * The array with the system logger instances. |
||
| 96 | * |
||
| 97 | * @var \Doctrine\Common\Collections\Collection |
||
| 98 | */ |
||
| 99 | protected $systemLoggers; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * The RegistryProcessor instance to handle running threads. |
||
| 103 | * |
||
| 104 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
| 105 | */ |
||
| 106 | protected $registryProcessor; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * The processor to read/write the necessary import data. |
||
| 110 | * |
||
| 111 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
| 112 | */ |
||
| 113 | protected $importProcessor; |
||
| 114 | |||
| 115 | /** |
||
| 116 | * The DI container builder instance. |
||
| 117 | * |
||
| 118 | * @var \Psr\Container\ContainerInterface |
||
| 119 | */ |
||
| 120 | protected $container; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * The system configuration. |
||
| 124 | * |
||
| 125 | * @var \TechDivision\Import\Configuration\ConfigurationInterface |
||
| 126 | */ |
||
| 127 | protected $configuration; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * The output stream to write console information to. |
||
| 131 | * |
||
| 132 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
| 133 | */ |
||
| 134 | protected $output; |
||
| 135 | |||
| 136 | /** |
||
| 137 | * The plugins to be processed. |
||
| 138 | * |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | protected $plugins = array(); |
||
| 142 | |||
| 143 | /** |
||
| 144 | * The flag that stop's processing the operation. |
||
| 145 | * |
||
| 146 | * @var boolean |
||
| 147 | */ |
||
| 148 | protected $stopped = false; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * The filehandle for the PID file. |
||
| 152 | * |
||
| 153 | * @var resource |
||
| 154 | */ |
||
| 155 | protected $fh; |
||
| 156 | |||
| 157 | /** |
||
| 158 | * The array with the module instances. |
||
| 159 | * |
||
| 160 | * @var \TechDivision\Import\Modules\ModuleInterface[] |
||
| 161 | */ |
||
| 162 | protected $modules; |
||
| 163 | |||
| 164 | /** |
||
| 165 | * The event emitter instance. |
||
| 166 | * |
||
| 167 | * @var \League\Event\EmitterInterface |
||
| 168 | */ |
||
| 169 | protected $emitter; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * The generic file handler instance. |
||
| 173 | * |
||
| 174 | * @var \TechDivision\Import\Handlers\GenericFileHandlerInterface |
||
| 175 | */ |
||
| 176 | protected $genericFileHanlder; |
||
| 177 | |||
| 178 | /** |
||
| 179 | * The PID file handler instance. |
||
| 180 | * |
||
| 181 | * @var \TechDivision\Import\Handlers\PidFileHandlerInterface |
||
| 182 | */ |
||
| 183 | protected $pidFileHanlder; |
||
| 184 | |||
| 185 | /** |
||
| 186 | * The constructor to initialize the instance. |
||
| 187 | * |
||
| 188 | * @param \Psr\Container\ContainerInterface $container The DI container instance |
||
| 189 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
| 190 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 191 | * @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The system configuration |
||
| 192 | * @param \Symfony\Component\Console\Output\OutputInterface $output The output instance |
||
| 193 | * @param \Doctrine\Common\Collections\Collection $systemLoggers The array with the system logger instances |
||
| 194 | * @param \League\Event\EmitterInterface $emitter The event emitter instance |
||
| 195 | * @param \TechDivision\Import\Handlers\GenericFileHandlerInterface $genericFileHandler The generic file handler instance |
||
| 196 | * @param \TechDivision\Import\Handlers\PidFileHandlerInterface $pidFileHandler The PID file handler instance |
||
| 197 | * @param \Traversable $modules The modules that provides the business logic |
||
| 198 | */ |
||
| 199 | public function __construct( |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Set's the event emitter instance. |
||
| 230 | * |
||
| 231 | * @param \League\Event\EmitterInterface $emitter The event emitter instance |
||
| 232 | * |
||
| 233 | * @return void |
||
| 234 | */ |
||
| 235 | public function setEmitter(EmitterInterface $emitter) |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Return's the event emitter instance. |
||
| 242 | * |
||
| 243 | * @return \League\Event\EmitterInterface The event emitter instance |
||
| 244 | */ |
||
| 245 | public function getEmitter() |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Set's the container instance. |
||
| 252 | * |
||
| 253 | * @param \Psr\Container\ContainerInterface $container The container instance |
||
| 254 | * |
||
| 255 | * @return void |
||
| 256 | */ |
||
| 257 | public function setContainer(ContainerInterface $container) |
||
| 261 | |||
| 262 | /** |
||
| 263 | * Return's the container instance. |
||
| 264 | * |
||
| 265 | * @return \Psr\Container\ContainerInterface The container instance |
||
| 266 | */ |
||
| 267 | public function getContainer() |
||
| 271 | |||
| 272 | /** |
||
| 273 | * Set's the output stream to write console information to. |
||
| 274 | * |
||
| 275 | * @param \Symfony\Component\Console\Output\OutputInterface $output The output stream |
||
| 276 | * |
||
| 277 | * @return void |
||
| 278 | */ |
||
| 279 | public function setOutput(OutputInterface $output) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Return's the output stream to write console information to. |
||
| 286 | * |
||
| 287 | * @return \Symfony\Component\Console\Output\OutputInterface The output stream |
||
| 288 | */ |
||
| 289 | public function getOutput() |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Set's the system configuration. |
||
| 296 | * |
||
| 297 | * @param \TechDivision\Import\Configuration\ConfigurationInterface $configuration The system configuration |
||
| 298 | * |
||
| 299 | * @return void |
||
| 300 | */ |
||
| 301 | public function setConfiguration(ConfigurationInterface $configuration) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Return's the system configuration. |
||
| 308 | * |
||
| 309 | * @return \TechDivision\Import\Configuration\ConfigurationInterface The system configuration |
||
| 310 | */ |
||
| 311 | public function getConfiguration() |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Set's the RegistryProcessor instance to handle the running threads. |
||
| 318 | * |
||
| 319 | * @param \TechDivision\Import\Services\RegistryProcessor $registryProcessor The registry processor instance |
||
| 320 | * |
||
| 321 | * @return void |
||
| 322 | */ |
||
| 323 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Return's the RegistryProcessor instance to handle the running threads. |
||
| 330 | * |
||
| 331 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
| 332 | */ |
||
| 333 | public function getRegistryProcessor() |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Set's the import processor instance. |
||
| 340 | * |
||
| 341 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
| 342 | * |
||
| 343 | * @return void |
||
| 344 | */ |
||
| 345 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Return's the import processor instance. |
||
| 352 | * |
||
| 353 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
| 354 | */ |
||
| 355 | public function getImportProcessor() |
||
| 359 | |||
| 360 | /** |
||
| 361 | * The array with the system loggers. |
||
| 362 | * |
||
| 363 | * @param \Doctrine\Common\Collections\Collection $systemLoggers The system logger instances |
||
| 364 | * |
||
| 365 | * @return void |
||
| 366 | */ |
||
| 367 | public function setSystemLoggers(Collection $systemLoggers) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Set's the module instances. |
||
| 374 | * |
||
| 375 | * @param \Traversable $modules The modules instances |
||
| 376 | * |
||
| 377 | * @return void |
||
| 378 | */ |
||
| 379 | public function setModules(\Traversable $modules) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * Return's the module instances. |
||
| 386 | * |
||
| 387 | * @return \Traversable The module instances |
||
| 388 | */ |
||
| 389 | public function getModules() |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Set's the PID file handler instance. |
||
| 396 | * |
||
| 397 | * @param \TechDivision\Import\Handlers\PidFileHandlerInterface $pidFileHandler The PID file handler instance |
||
| 398 | * |
||
| 399 | * @return void |
||
| 400 | */ |
||
| 401 | public function setPidFileHandler(PidFileHandlerInterface $pidFileHandler) : void |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Return's the PID file handler instance. |
||
| 408 | * |
||
| 409 | * @return \TechDivision\Import\Handlers\PidFileHandlerInterface The PID file handler instance |
||
| 410 | */ |
||
| 411 | public function getPidFileHandler() : PidFileHandlerInterface |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Set's the generic file handler instance. |
||
| 418 | * |
||
| 419 | * @param \TechDivision\Import\Handlers\GenericFileHandlerInterface $genericFileHandler The generic file handler instance |
||
| 420 | * |
||
| 421 | * @return void |
||
| 422 | */ |
||
| 423 | public function setGenericFileHandler(GenericFileHandlerInterface $genericFileHandler) : void |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Return's the generic file handler instance. |
||
| 430 | * |
||
| 431 | * @return \TechDivision\Import\Handlers\GenericFileHandlerInterface The generic file handler instance |
||
| 432 | */ |
||
| 433 | public function getGenericFileHandler() : GenericFileHandlerInterface |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Return's the logger with the passed name, by default the system logger. |
||
| 440 | * |
||
| 441 | * @param string $name The name of the requested system logger |
||
| 442 | * |
||
| 443 | * @return \Psr\Log\LoggerInterface The logger instance |
||
| 444 | * @throws \Exception Is thrown, if the requested logger is NOT available |
||
| 445 | */ |
||
| 446 | public function getSystemLogger($name = LoggerKeys::SYSTEM) |
||
| 462 | |||
| 463 | /** |
||
| 464 | * Returns the actual application version. |
||
| 465 | * |
||
| 466 | * @return string The application's version |
||
| 467 | */ |
||
| 468 | public function getVersion() |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Returns the actual application name. |
||
| 475 | * |
||
| 476 | * @return string The application's name |
||
| 477 | */ |
||
| 478 | public function getName() |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Query whether or not the system logger with the passed name is available. |
||
| 485 | * |
||
| 486 | * @param string $name The name of the requested system logger |
||
| 487 | * |
||
| 488 | * @return boolean TRUE if the logger with the passed name exists, else FALSE |
||
| 489 | */ |
||
| 490 | public function hasSystemLogger($name = LoggerKeys::SYSTEM) |
||
| 494 | |||
| 495 | /** |
||
| 496 | * Return's the array with the system logger instances. |
||
| 497 | * |
||
| 498 | * @return \Doctrine\Common\Collections\Collection The logger instance |
||
| 499 | */ |
||
| 500 | public function getSystemLoggers() |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Return's the unique serial for this import process. |
||
| 507 | * |
||
| 508 | * @return string The unique serial |
||
| 509 | */ |
||
| 510 | public function getSerial() |
||
| 514 | |||
| 515 | /** |
||
| 516 | * The shutdown handler to catch fatal errors. |
||
| 517 | * |
||
| 518 | * This method is need to make sure, that an existing PID file will be removed |
||
| 519 | * if a fatal error has been triggered. |
||
| 520 | * |
||
| 521 | * @return void |
||
| 522 | */ |
||
| 523 | public function shutdown() |
||
| 542 | |||
| 543 | /** |
||
| 544 | * Persist the UUID of the actual import process to the PID file. |
||
| 545 | * |
||
| 546 | * @return void |
||
| 547 | * @throws \Exception Is thrown, if the PID can not be locked or the PID can not be added |
||
| 548 | * @throws \TechDivision\Import\Exceptions\ImportAlreadyRunningException Is thrown, if a import process is already running |
||
| 549 | */ |
||
| 550 | public function lock() |
||
| 554 | |||
| 555 | /** |
||
| 556 | * Remove's the UUID of the actual import process from the PID file. |
||
| 557 | * |
||
| 558 | * @return void |
||
| 559 | * @throws \Exception Is thrown, if the PID can not be removed |
||
| 560 | */ |
||
| 561 | public function unlock() |
||
| 565 | |||
| 566 | /** |
||
| 567 | * Remove's the passed line from the file with the passed name. |
||
| 568 | * |
||
| 569 | * @param string $line The line to be removed |
||
| 570 | * @param resource $fh The file handle of the file the line has to be removed |
||
| 571 | * |
||
| 572 | * @return void |
||
| 573 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
| 574 | * @deprecated Since version 17.0.0 |
||
| 575 | * @see \TechDivision\Import\Handlers\GenericFileHandler::removeLineFromFile() |
||
| 576 | */ |
||
| 577 | public function removeLineFromFile($line, $fh) |
||
| 589 | |||
| 590 | /** |
||
| 591 | * Process the given operation. |
||
| 592 | * |
||
| 593 | * @param string $serial The unique serial of the actual import process |
||
| 594 | * |
||
| 595 | * @return null|int null or 0 if everything went fine, or an error code |
||
| 596 | * @throws \Exception Is thrown if the operation can't be finished successfully |
||
| 597 | */ |
||
| 598 | public function process($serial) |
||
| 744 | |||
| 745 | /** |
||
| 746 | * Stop processing the operation. |
||
| 747 | * |
||
| 748 | * @param string $reason The reason why the operation has been stopped |
||
| 749 | * |
||
| 750 | * @return void |
||
| 751 | * @throws \TechDivision\Import\Exceptions\ApplicationStoppedException Is thrown if the application has been stopped |
||
| 752 | */ |
||
| 753 | public function stop($reason) |
||
| 762 | |||
| 763 | /** |
||
| 764 | * Finish processing the operation. The application will be stopped without an error output. |
||
| 765 | * |
||
| 766 | * @param string $reason The reason why the operation has been finish |
||
| 767 | * |
||
| 768 | * @return void |
||
| 769 | * @throws \TechDivision\Import\Exceptions\ApplicationFinishedException Is thrown if the application has been finish |
||
| 770 | */ |
||
| 771 | public function finish($reason = '') |
||
| 776 | |||
| 777 | /** |
||
| 778 | * Return's TRUE if the operation has been stopped, else FALSE. |
||
| 779 | * |
||
| 780 | * @return boolean TRUE if the process has been stopped, else FALSE |
||
| 781 | */ |
||
| 782 | public function isStopped() |
||
| 786 | |||
| 787 | /** |
||
| 788 | * Gets a service. |
||
| 789 | * |
||
| 790 | * @param string $id The service identifier |
||
| 791 | * |
||
| 792 | * @return object The associated service |
||
| 793 | * |
||
| 794 | * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException When a circular reference is detected |
||
| 795 | * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException When the service is not defined |
||
| 796 | */ |
||
| 797 | public function get($id) |
||
| 801 | |||
| 802 | /** |
||
| 803 | * Returns true if the container can return an entry for the given identifier. |
||
| 804 | * Returns false otherwise. |
||
| 805 | * |
||
| 806 | * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
||
| 807 | * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
||
| 808 | * |
||
| 809 | * @param string $id Identifier of the entry to look for. |
||
| 810 | * |
||
| 811 | * @return bool |
||
| 812 | */ |
||
| 813 | public function has($id) |
||
| 817 | |||
| 818 | /** |
||
| 819 | * Lifecycle callback that will be inovked before the |
||
| 820 | * import process has been started. |
||
| 821 | * |
||
| 822 | * @return void |
||
| 823 | */ |
||
| 824 | protected function setUp() |
||
| 828 | |||
| 829 | /** |
||
| 830 | * Lifecycle callback that will be inovked after the |
||
| 831 | * import process has been finished. |
||
| 832 | * |
||
| 833 | * @return void |
||
| 834 | */ |
||
| 835 | protected function tearDown() |
||
| 839 | |||
| 840 | /** |
||
| 841 | * Simple method that writes the passed method the the console and the |
||
| 842 | * system logger, if configured and a log level has been passed. |
||
| 843 | * |
||
| 844 | * @param string $msg The message to log |
||
| 845 | * @param string $logLevel The log level to use |
||
| 846 | * |
||
| 847 | * @return void |
||
| 848 | */ |
||
| 849 | public function log($msg, $logLevel = null) |
||
| 866 | |||
| 867 | /** |
||
| 868 | * Map's the passed log level to a valid symfony console style. |
||
| 869 | * |
||
| 870 | * @param string $logLevel The log level to map |
||
| 871 | * |
||
| 872 | * @return string The apropriate symfony console style |
||
| 873 | */ |
||
| 874 | protected function mapLogLevelToStyle($logLevel) |
||
| 885 | |||
| 886 | /** |
||
| 887 | * Return's the PID filename to use. |
||
| 888 | * |
||
| 889 | * @return string The PID filename |
||
| 890 | */ |
||
| 891 | protected function getPidFilename() |
||
| 895 | } |
||
| 896 |
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..