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 |
||
51 | class Simple implements ApplicationInterface |
||
52 | { |
||
53 | |||
54 | /** |
||
55 | * The default style to write messages to the symfony console. |
||
56 | * |
||
57 | * @var string |
||
58 | */ |
||
59 | const DEFAULT_STYLE = 'info'; |
||
60 | |||
61 | /** |
||
62 | * The TechDivision company name as ANSI art. |
||
63 | * |
||
64 | * @var string |
||
65 | */ |
||
66 | protected $ansiArt = ' _______ _ _____ _ _ _ |
||
67 | |__ __| | | | __ \(_) (_) (_) |
||
68 | | | ___ ___| |__ | | | |___ ___ ___ _ ___ _ __ |
||
69 | | |/ _ \/ __| \'_ \| | | | \ \ / / / __| |/ _ \| \'_ \ |
||
70 | | | __/ (__| | | | |__| | |\ V /| \__ \ | (_) | | | | |
||
71 | |_|\___|\___|_| |_|_____/|_| \_/ |_|___/_|\___/|_| |_| |
||
72 | '; |
||
73 | |||
74 | /** |
||
75 | * The log level => console style mapping. |
||
76 | * |
||
77 | * @var array |
||
78 | */ |
||
79 | protected $logLevelStyleMapping = array( |
||
80 | LogLevel::INFO => 'info', |
||
81 | LogLevel::DEBUG => 'comment', |
||
82 | LogLevel::ERROR => 'error', |
||
83 | LogLevel::ALERT => 'error', |
||
84 | LogLevel::CRITICAL => 'error', |
||
85 | LogLevel::EMERGENCY => 'error', |
||
86 | LogLevel::WARNING => 'error', |
||
87 | LogLevel::NOTICE => 'info' |
||
88 | ); |
||
89 | |||
90 | /** |
||
91 | * The PID for the running processes. |
||
92 | * |
||
93 | * @var array |
||
94 | */ |
||
95 | protected $pid; |
||
96 | |||
97 | /** |
||
98 | * The actions unique serial. |
||
99 | * |
||
100 | * @var string |
||
101 | */ |
||
102 | protected $serial; |
||
103 | |||
104 | /** |
||
105 | * The array with the system logger instances. |
||
106 | * |
||
107 | * @var array |
||
108 | */ |
||
109 | protected $systemLoggers; |
||
110 | |||
111 | /** |
||
112 | * The RegistryProcessor instance to handle running threads. |
||
113 | * |
||
114 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
115 | */ |
||
116 | protected $registryProcessor; |
||
117 | |||
118 | /** |
||
119 | * The processor to read/write the necessary import data. |
||
120 | * |
||
121 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
122 | */ |
||
123 | protected $importProcessor; |
||
124 | |||
125 | /** |
||
126 | * The system configuration. |
||
127 | * |
||
128 | * @var \TechDivision\Import\ConfigurationInterface |
||
129 | */ |
||
130 | protected $configuration; |
||
131 | |||
132 | /** |
||
133 | * The input stream to read console information from. |
||
134 | * |
||
135 | * @var \Symfony\Component\Console\Input\InputInterface |
||
136 | */ |
||
137 | protected $input; |
||
138 | |||
139 | /** |
||
140 | * The output stream to write console information to. |
||
141 | * |
||
142 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
143 | */ |
||
144 | protected $output; |
||
145 | |||
146 | /** |
||
147 | * The plugins to be processed. |
||
148 | * |
||
149 | * @var array |
||
150 | */ |
||
151 | protected $plugins = array(); |
||
152 | |||
153 | /** |
||
154 | * The flag that stop's processing the operation. |
||
155 | * |
||
156 | * @var boolean |
||
157 | */ |
||
158 | protected $stopped = false; |
||
159 | |||
160 | /** |
||
161 | * The constructor to initialize the instance. |
||
162 | * |
||
163 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
164 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
165 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
166 | * @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
||
167 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
168 | * @param array $systemLoggers The array with the system logger instances |
||
169 | */ |
||
170 | 1 | public function __construct( |
|
190 | |||
191 | /** |
||
192 | * The shutdown handler to catch fatal errors. |
||
193 | * |
||
194 | * This method is need to make sure, that an existing PID file will be removed |
||
195 | * if a fatal error has been triggered. |
||
196 | * |
||
197 | * @return void |
||
198 | */ |
||
199 | public function shutdown() |
||
218 | |||
219 | /** |
||
220 | * Return's the logger with the passed name, by default the system logger. |
||
221 | * |
||
222 | * @param string $name The name of the requested system logger |
||
223 | * |
||
224 | * @return \Psr\Log\LoggerInterface The logger instance |
||
225 | * @throws \Exception Is thrown, if the requested logger is NOT available |
||
226 | */ |
||
227 | public function getSystemLogger($name = LoggerKeys::SYSTEM) |
||
238 | |||
239 | /** |
||
240 | * Return's the array with the system logger instances. |
||
241 | * |
||
242 | * @return array The logger instance |
||
243 | */ |
||
244 | public function getSystemLoggers() |
||
248 | |||
249 | /** |
||
250 | * Return's the RegistryProcessor instance to handle the running threads. |
||
251 | * |
||
252 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
253 | */ |
||
254 | public function getRegistryProcessor() |
||
258 | |||
259 | /** |
||
260 | * Return's the import processor instance. |
||
261 | * |
||
262 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
263 | */ |
||
264 | public function getImportProcessor() |
||
268 | |||
269 | /** |
||
270 | * Return's the system configuration. |
||
271 | * |
||
272 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
273 | */ |
||
274 | public function getConfiguration() |
||
278 | |||
279 | /** |
||
280 | * Return's the input stream to read console information from. |
||
281 | * |
||
282 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
283 | */ |
||
284 | public function getInput() |
||
288 | |||
289 | /** |
||
290 | * Return's the output stream to write console information to. |
||
291 | * |
||
292 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
293 | */ |
||
294 | 1 | public function getOutput() |
|
298 | |||
299 | /** |
||
300 | * Return's the unique serial for this import process. |
||
301 | * |
||
302 | * @return string The unique serial |
||
303 | */ |
||
304 | public function getSerial() |
||
308 | |||
309 | /** |
||
310 | * Persist the UUID of the actual import process to the PID file. |
||
311 | * |
||
312 | * @return void |
||
313 | * @throws \Exception Is thrown, if the PID can not be added |
||
314 | */ |
||
315 | public function lock() |
||
337 | |||
338 | /** |
||
339 | * Remove's the UUID of the actual import process from the PID file. |
||
340 | * |
||
341 | * @return void |
||
342 | * @throws \Exception Is thrown, if the PID can not be removed |
||
343 | */ |
||
344 | public function unlock() |
||
360 | |||
361 | /** |
||
362 | * Remove's the passed line from the file with the passed name. |
||
363 | * |
||
364 | * @param string $line The line to be removed |
||
365 | * @param string $filename The name of the file the line has to be removed |
||
366 | * |
||
367 | * @return void |
||
368 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
369 | */ |
||
370 | public function removeLineFromFile($line, $filename) |
||
427 | |||
428 | /** |
||
429 | * Process the given operation. |
||
430 | * |
||
431 | * @return void |
||
432 | * @throws \Exception Is thrown if the operation can't be finished successfully |
||
433 | */ |
||
434 | public function process() |
||
508 | |||
509 | /** |
||
510 | * Stop processing the operation. |
||
511 | * |
||
512 | * @param string $reason The reason why the operation has been stopped |
||
513 | * |
||
514 | * @return void |
||
515 | */ |
||
516 | public function stop($reason) |
||
525 | |||
526 | /** |
||
527 | * Return's TRUE if the operation has been stopped, else FALSE. |
||
528 | * |
||
529 | * @return boolean TRUE if the process has been stopped, else FALSE |
||
530 | */ |
||
531 | public function isStopped() |
||
535 | |||
536 | /** |
||
537 | * Factory method to create new plugin instances. |
||
538 | * |
||
539 | * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance |
||
540 | * |
||
541 | * @return object The plugin instance |
||
542 | */ |
||
543 | protected function pluginFactory(PluginConfigurationInterface $pluginConfiguration) |
||
552 | |||
553 | /** |
||
554 | * Lifecycle callback that will be inovked before the |
||
555 | * import process has been started. |
||
556 | * |
||
557 | * @return void |
||
558 | */ |
||
559 | protected function setUp() |
||
609 | |||
610 | /** |
||
611 | * Lifecycle callback that will be inovked after the |
||
612 | * import process has been finished. |
||
613 | * |
||
614 | * @return void |
||
615 | */ |
||
616 | protected function tearDown() |
||
620 | |||
621 | /** |
||
622 | * Simple method that writes the passed method the the console and the |
||
623 | * system logger, if configured and a log level has been passed. |
||
624 | * |
||
625 | * @param string $msg The message to log |
||
626 | * @param string $logLevel The log level to use |
||
627 | * |
||
628 | * @return void |
||
629 | */ |
||
630 | protected function log($msg, $logLevel = null) |
||
647 | |||
648 | /** |
||
649 | * Map's the passed log level to a valid symfony console style. |
||
650 | * |
||
651 | * @param string $logLevel The log level to map |
||
652 | * |
||
653 | * @return string The apropriate symfony console style |
||
654 | */ |
||
655 | protected function mapLogLevelToStyle($logLevel) |
||
666 | |||
667 | /** |
||
668 | * Return's the PID filename to use. |
||
669 | * |
||
670 | * @return string The PID filename |
||
671 | */ |
||
672 | protected function getPidFilename() |
||
676 | } |
||
677 |
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..