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 \Psr\Container\ContainerInterface |
||
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 generic file handler instance. |
||
172 | * |
||
173 | * @var \TechDivision\Import\Handlers\GenericFileHandlerInterface |
||
174 | */ |
||
175 | protected $genericFileHanlder; |
||
176 | |||
177 | /** |
||
178 | * The PID file handler instance. |
||
179 | * |
||
180 | * @var \TechDivision\Import\Handlers\PidFileHandlerInterface |
||
181 | */ |
||
182 | protected $pidFileHanlder; |
||
183 | |||
184 | /** |
||
185 | * The constructor to initialize the instance. |
||
186 | * |
||
187 | * @param \Psr\Container\ContainerInterface $container The DI container instance |
||
188 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
189 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
190 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
191 | * @param \Symfony\Component\Console\Output\OutputInterface $output The output instance |
||
192 | * @param \Doctrine\Common\Collections\Collection $systemLoggers The array with the system logger instances |
||
193 | * @param \League\Event\EmitterInterface $emitter The event emitter instance |
||
194 | * @param \TechDivision\Import\Handlers\GenericFileHandlerInterface $genericFileHandler The generic file handler instance |
||
195 | * @param \TechDivision\Import\Handlers\PidFileHandlerInterface $pidFileHandler The PID file handler instance |
||
196 | * @param \Traversable $modules The modules that provides the business logic |
||
197 | */ |
||
198 | public function __construct( |
||
226 | |||
227 | /** |
||
228 | * Set's the event emitter instance. |
||
229 | * |
||
230 | * @param \League\Event\EmitterInterface $emitter The event emitter instance |
||
231 | * |
||
232 | * @return void |
||
233 | */ |
||
234 | public function setEmitter(EmitterInterface $emitter) |
||
238 | |||
239 | /** |
||
240 | * Return's the event emitter instance. |
||
241 | * |
||
242 | * @return \League\Event\EmitterInterface The event emitter instance |
||
243 | */ |
||
244 | public function getEmitter() |
||
248 | |||
249 | /** |
||
250 | * Set's the container instance. |
||
251 | * |
||
252 | * @param \Psr\Container\ContainerInterface $container The container instance |
||
253 | * |
||
254 | * @return void |
||
255 | */ |
||
256 | public function setContainer(ContainerInterface $container) |
||
260 | |||
261 | /** |
||
262 | * Return's the container instance. |
||
263 | * |
||
264 | * @return \Psr\Container\ContainerInterface The container instance |
||
265 | */ |
||
266 | public function getContainer() |
||
270 | |||
271 | /** |
||
272 | * Set's the output stream to write console information to. |
||
273 | * |
||
274 | * @param \Symfony\Component\Console\Output\OutputInterface $output The output stream |
||
275 | * |
||
276 | * @return void |
||
277 | */ |
||
278 | public function setOutput(OutputInterface $output) |
||
282 | |||
283 | /** |
||
284 | * Return's the output stream to write console information to. |
||
285 | * |
||
286 | * @return \Symfony\Component\Console\Output\OutputInterface The output stream |
||
287 | */ |
||
288 | public function getOutput() |
||
292 | |||
293 | /** |
||
294 | * Set's the system configuration. |
||
295 | * |
||
296 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
297 | * |
||
298 | * @return void |
||
299 | */ |
||
300 | public function setConfiguration(ConfigurationInterface $configuration) |
||
304 | |||
305 | /** |
||
306 | * Return's the system configuration. |
||
307 | * |
||
308 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
309 | */ |
||
310 | public function getConfiguration() |
||
314 | |||
315 | /** |
||
316 | * Set's the RegistryProcessor instance to handle the running threads. |
||
317 | * |
||
318 | * @param \TechDivision\Import\Services\RegistryProcessor $registryProcessor The registry processor instance |
||
319 | * |
||
320 | * @return void |
||
321 | */ |
||
322 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
326 | |||
327 | /** |
||
328 | * Return's the RegistryProcessor instance to handle the running threads. |
||
329 | * |
||
330 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
331 | */ |
||
332 | public function getRegistryProcessor() |
||
336 | |||
337 | /** |
||
338 | * Set's the import processor instance. |
||
339 | * |
||
340 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
341 | * |
||
342 | * @return void |
||
343 | */ |
||
344 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
||
348 | |||
349 | /** |
||
350 | * Return's the import processor instance. |
||
351 | * |
||
352 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
353 | */ |
||
354 | public function getImportProcessor() |
||
358 | |||
359 | /** |
||
360 | * The array with the system loggers. |
||
361 | * |
||
362 | * @param \Doctrine\Common\Collections\Collection $systemLoggers The system logger instances |
||
363 | * |
||
364 | * @return void |
||
365 | */ |
||
366 | public function setSystemLoggers(Collection $systemLoggers) |
||
370 | |||
371 | /** |
||
372 | * Set's the module instances. |
||
373 | * |
||
374 | * @param \Traversable $modules The modules instances |
||
375 | * |
||
376 | * @return void |
||
377 | */ |
||
378 | public function setModules(\Traversable $modules) |
||
382 | |||
383 | /** |
||
384 | * Return's the module instances. |
||
385 | * |
||
386 | * @return \Traversable The module instances |
||
387 | */ |
||
388 | public function getModules() |
||
392 | |||
393 | /** |
||
394 | * Set's the PID file handler instance. |
||
395 | * |
||
396 | * @param \TechDivision\Import\Handlers\PidFileHandlerInterface $pidFileHandler The PID file handler instance |
||
397 | * |
||
398 | * @return void |
||
399 | */ |
||
400 | public function setPidFileHandler(PidFileHandlerInterface $pidFileHandler) : void |
||
404 | |||
405 | /** |
||
406 | * Return's the PID file handler instance. |
||
407 | * |
||
408 | * @return \TechDivision\Import\Handlers\PidFileHandlerInterface The PID file handler instance |
||
409 | */ |
||
410 | public function getPidFileHandler() : PidFileHandlerInterface |
||
414 | |||
415 | /** |
||
416 | * Set's the generic file handler instance. |
||
417 | * |
||
418 | * @param \TechDivision\Import\Handlers\GenericFileHandlerInterface $genericFileHandler The generic file handler instance |
||
419 | * |
||
420 | * @return void |
||
421 | */ |
||
422 | public function setGenericFileHandler(GenericFileHandlerInterface $genericFileHandler) : void |
||
426 | |||
427 | /** |
||
428 | * Return's the generic file handler instance. |
||
429 | * |
||
430 | * @return \TechDivision\Import\Handlers\GenericFileHandlerInterface The generic file handler instance |
||
431 | */ |
||
432 | public function getGenericFileHandler() : GenericFileHandlerInterface |
||
436 | |||
437 | /** |
||
438 | * Return's the logger with the passed name, by default the system logger. |
||
439 | * |
||
440 | * @param string $name The name of the requested system logger |
||
441 | * |
||
442 | * @return \Psr\Log\LoggerInterface The logger instance |
||
443 | * @throws \Exception Is thrown, if the requested logger is NOT available |
||
444 | */ |
||
445 | public function getSystemLogger($name = LoggerKeys::SYSTEM) |
||
461 | |||
462 | /** |
||
463 | * Returns the actual application version. |
||
464 | * |
||
465 | * @return string The application's version |
||
466 | */ |
||
467 | public function getVersion() |
||
471 | |||
472 | /** |
||
473 | * Returns the actual application name. |
||
474 | * |
||
475 | * @return string The application's name |
||
476 | */ |
||
477 | public function getName() |
||
481 | |||
482 | /** |
||
483 | * Query whether or not the system logger with the passed name is available. |
||
484 | * |
||
485 | * @param string $name The name of the requested system logger |
||
486 | * |
||
487 | * @return boolean TRUE if the logger with the passed name exists, else FALSE |
||
488 | */ |
||
489 | public function hasSystemLogger($name = LoggerKeys::SYSTEM) |
||
493 | |||
494 | /** |
||
495 | * Return's the array with the system logger instances. |
||
496 | * |
||
497 | * @return \Doctrine\Common\Collections\Collection The logger instance |
||
498 | */ |
||
499 | public function getSystemLoggers() |
||
503 | |||
504 | /** |
||
505 | * Return's the unique serial for this import process. |
||
506 | * |
||
507 | * @return string The unique serial |
||
508 | */ |
||
509 | public function getSerial() |
||
513 | |||
514 | /** |
||
515 | * The shutdown handler to catch fatal errors. |
||
516 | * |
||
517 | * This method is need to make sure, that an existing PID file will be removed |
||
518 | * if a fatal error has been triggered. |
||
519 | * |
||
520 | * @return void |
||
521 | */ |
||
522 | public function shutdown() |
||
541 | |||
542 | /** |
||
543 | * Persist the UUID of the actual import process to the PID file. |
||
544 | * |
||
545 | * @return void |
||
546 | * @throws \Exception Is thrown, if the PID can not be locked or the PID can not be added |
||
547 | * @throws \TechDivision\Import\Exceptions\ImportAlreadyRunningException Is thrown, if a import process is already running |
||
548 | */ |
||
549 | public function lock() |
||
553 | |||
554 | /** |
||
555 | * Remove's the UUID of the actual import process from the PID file. |
||
556 | * |
||
557 | * @return void |
||
558 | * @throws \Exception Is thrown, if the PID can not be removed |
||
559 | */ |
||
560 | public function unlock() |
||
564 | |||
565 | /** |
||
566 | * Remove's the passed line from the file with the passed name. |
||
567 | * |
||
568 | * @param string $line The line to be removed |
||
569 | * @param resource $fh The file handle of the file the line has to be removed |
||
570 | * |
||
571 | * @return void |
||
572 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
573 | * @deprecated Since version 17.0.0 |
||
574 | * @see \TechDivision\Import\Handlers\GenericFileHandler::removeLineFromFile() |
||
575 | */ |
||
576 | public function removeLineFromFile($line, $fh) |
||
588 | |||
589 | /** |
||
590 | * Process the given operation. |
||
591 | * |
||
592 | * @param string $serial The unique serial of the actual import process |
||
593 | * |
||
594 | * @return null|int null or 0 if everything went fine, or an error code |
||
595 | * @throws \Exception Is thrown if the operation can't be finished successfully |
||
596 | */ |
||
597 | public function process($serial) |
||
734 | |||
735 | /** |
||
736 | * Stop processing the operation. |
||
737 | * |
||
738 | * @param string $reason The reason why the operation has been stopped |
||
739 | * |
||
740 | * @return void |
||
741 | * @throws \TechDivision\Import\Exceptions\ApplicationStoppedException Is thrown if the application has been stopped |
||
742 | */ |
||
743 | public function stop($reason) |
||
752 | |||
753 | /** |
||
754 | * Return's TRUE if the operation has been stopped, else FALSE. |
||
755 | * |
||
756 | * @return boolean TRUE if the process has been stopped, else FALSE |
||
757 | */ |
||
758 | public function isStopped() |
||
762 | |||
763 | /** |
||
764 | * Gets a service. |
||
765 | * |
||
766 | * @param string $id The service identifier |
||
767 | * |
||
768 | * @return object The associated service |
||
769 | * |
||
770 | * @throws \Symfony\Component\DependencyInjection\Exception\ServiceCircularReferenceException When a circular reference is detected |
||
771 | * @throws \Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException When the service is not defined |
||
772 | */ |
||
773 | public function get($id) |
||
777 | |||
778 | /** |
||
779 | * Returns true if the container can return an entry for the given identifier. |
||
780 | * Returns false otherwise. |
||
781 | * |
||
782 | * `has($id)` returning true does not mean that `get($id)` will not throw an exception. |
||
783 | * It does however mean that `get($id)` will not throw a `NotFoundExceptionInterface`. |
||
784 | * |
||
785 | * @param string $id Identifier of the entry to look for. |
||
786 | * |
||
787 | * @return bool |
||
788 | */ |
||
789 | public function has($id) |
||
793 | |||
794 | /** |
||
795 | * Lifecycle callback that will be inovked before the |
||
796 | * import process has been started. |
||
797 | * |
||
798 | * @return void |
||
799 | */ |
||
800 | protected function setUp() |
||
804 | |||
805 | /** |
||
806 | * Lifecycle callback that will be inovked after the |
||
807 | * import process has been finished. |
||
808 | * |
||
809 | * @return void |
||
810 | */ |
||
811 | protected function tearDown() |
||
815 | |||
816 | /** |
||
817 | * Simple method that writes the passed method the the console and the |
||
818 | * system logger, if configured and a log level has been passed. |
||
819 | * |
||
820 | * @param string $msg The message to log |
||
821 | * @param string $logLevel The log level to use |
||
822 | * |
||
823 | * @return void |
||
824 | */ |
||
825 | public function log($msg, $logLevel = null) |
||
842 | |||
843 | /** |
||
844 | * Map's the passed log level to a valid symfony console style. |
||
845 | * |
||
846 | * @param string $logLevel The log level to map |
||
847 | * |
||
848 | * @return string The apropriate symfony console style |
||
849 | */ |
||
850 | protected function mapLogLevelToStyle($logLevel) |
||
861 | |||
862 | /** |
||
863 | * Return's the PID filename to use. |
||
864 | * |
||
865 | * @return string The PID filename |
||
866 | */ |
||
867 | protected function getPidFilename() |
||
871 | } |
||
872 |
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..