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( |
|
171 | RegistryProcessorInterface $registryProcessor, |
||
172 | ImportProcessorInterface $importProcessor, |
||
173 | ConfigurationInterface $configuration, |
||
174 | InputInterface $input, |
||
175 | OutputInterface $output, |
||
176 | array $systemLoggers |
||
177 | ) { |
||
178 | |||
179 | // register the shutdown function |
||
180 | 1 | register_shutdown_function(array($this, 'shutdown')); |
|
181 | |||
182 | // initialize the values |
||
183 | 1 | $this->registryProcessor = $registryProcessor; |
|
184 | 1 | $this->importProcessor = $importProcessor; |
|
185 | 1 | $this->configuration = $configuration; |
|
186 | 1 | $this->input = $input; |
|
187 | 1 | $this->output = $output; |
|
188 | 1 | $this->systemLoggers = $systemLoggers; |
|
189 | 1 | } |
|
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() |
||
200 | { |
||
201 | |||
202 | // check if there was a fatal error caused shutdown |
||
203 | if ($lastError = error_get_last()) { |
||
204 | // initialize error type and message |
||
205 | $type = 0; |
||
206 | $message = ''; |
||
207 | // extract the last error values |
||
208 | extract($lastError); |
||
209 | // query whether we've a fatal/user error |
||
210 | if ($type === E_ERROR || $type === E_USER_ERROR) { |
||
211 | // clean-up the PID file |
||
212 | $this->unlock(); |
||
213 | // log the fatal error message |
||
214 | $this->log($message, LogLevel::ERROR); |
||
215 | } |
||
216 | } |
||
217 | } |
||
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) |
||
228 | { |
||
229 | |||
230 | // query whether or not, the requested logger is available |
||
231 | if (isset($this->systemLoggers[$name])) { |
||
232 | return $this->systemLoggers[$name]; |
||
233 | } |
||
234 | |||
235 | // throw an exception if the requested logger is NOT available |
||
236 | throw new \Exception(sprintf('The requested logger \'%s\' is not available', $name)); |
||
237 | } |
||
238 | |||
239 | /** |
||
240 | * Query whether or not the system logger with the passed name is available. |
||
241 | * |
||
242 | * @param string $name The name of the requested system logger |
||
243 | * |
||
244 | * @return boolean TRUE if the logger with the passed name exists, else FALSE |
||
245 | */ |
||
246 | public function hasSystemLogger($name = LoggerKeys::SYSTEM) |
||
247 | { |
||
248 | return isset($this->systemLoggers[$name]); |
||
249 | } |
||
250 | |||
251 | /** |
||
252 | * Return's the array with the system logger instances. |
||
253 | * |
||
254 | * @return array The logger instance |
||
255 | */ |
||
256 | public function getSystemLoggers() |
||
257 | { |
||
258 | return $this->systemLoggers; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * Return's the RegistryProcessor instance to handle the running threads. |
||
263 | * |
||
264 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
265 | */ |
||
266 | public function getRegistryProcessor() |
||
270 | |||
271 | /** |
||
272 | * Return's the import processor instance. |
||
273 | * |
||
274 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
275 | */ |
||
276 | public function getImportProcessor() |
||
280 | |||
281 | /** |
||
282 | * Return's the system configuration. |
||
283 | * |
||
284 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
285 | */ |
||
286 | public function getConfiguration() |
||
290 | |||
291 | /** |
||
292 | * Return's the input stream to read console information from. |
||
293 | * |
||
294 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
295 | */ |
||
296 | public function getInput() |
||
300 | |||
301 | /** |
||
302 | * Return's the output stream to write console information to. |
||
303 | * |
||
304 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
305 | */ |
||
306 | 1 | public function getOutput() |
|
310 | |||
311 | /** |
||
312 | * Return's the unique serial for this import process. |
||
313 | * |
||
314 | * @return string The unique serial |
||
315 | */ |
||
316 | public function getSerial() |
||
320 | |||
321 | /** |
||
322 | * Persist the UUID of the actual import process to the PID file. |
||
323 | * |
||
324 | * @return void |
||
325 | * @throws \Exception Is thrown, if the PID can not be added |
||
326 | */ |
||
327 | public function lock() |
||
349 | |||
350 | /** |
||
351 | * Remove's the UUID of the actual import process from the PID file. |
||
352 | * |
||
353 | * @return void |
||
354 | * @throws \Exception Is thrown, if the PID can not be removed |
||
355 | */ |
||
356 | public function unlock() |
||
372 | |||
373 | /** |
||
374 | * Remove's the passed line from the file with the passed name. |
||
375 | * |
||
376 | * @param string $line The line to be removed |
||
377 | * @param string $filename The name of the file the line has to be removed |
||
378 | * |
||
379 | * @return void |
||
380 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
381 | */ |
||
382 | public function removeLineFromFile($line, $filename) |
||
439 | |||
440 | /** |
||
441 | * Process the given operation. |
||
442 | * |
||
443 | * @return void |
||
444 | * @throws \Exception Is thrown if the operation can't be finished successfully |
||
445 | */ |
||
446 | public function process() |
||
520 | |||
521 | /** |
||
522 | * Stop processing the operation. |
||
523 | * |
||
524 | * @param string $reason The reason why the operation has been stopped |
||
525 | * |
||
526 | * @return void |
||
527 | */ |
||
528 | public function stop($reason) |
||
537 | |||
538 | /** |
||
539 | * Return's TRUE if the operation has been stopped, else FALSE. |
||
540 | * |
||
541 | * @return boolean TRUE if the process has been stopped, else FALSE |
||
542 | */ |
||
543 | public function isStopped() |
||
547 | |||
548 | /** |
||
549 | * Factory method to create new plugin instances. |
||
550 | * |
||
551 | * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance |
||
552 | * |
||
553 | * @return object The plugin instance |
||
554 | */ |
||
555 | protected function pluginFactory(PluginConfigurationInterface $pluginConfiguration) |
||
564 | |||
565 | /** |
||
566 | * Lifecycle callback that will be inovked before the |
||
567 | * import process has been started. |
||
568 | * |
||
569 | * @return void |
||
570 | */ |
||
571 | protected function setUp() |
||
622 | |||
623 | /** |
||
624 | * Lifecycle callback that will be inovked after the |
||
625 | * import process has been finished. |
||
626 | * |
||
627 | * @return void |
||
628 | */ |
||
629 | protected function tearDown() |
||
633 | |||
634 | /** |
||
635 | * Simple method that writes the passed method the the console and the |
||
636 | * system logger, if configured and a log level has been passed. |
||
637 | * |
||
638 | * @param string $msg The message to log |
||
639 | * @param string $logLevel The log level to use |
||
640 | * |
||
641 | * @return void |
||
642 | */ |
||
643 | protected function log($msg, $logLevel = null) |
||
660 | |||
661 | /** |
||
662 | * Map's the passed log level to a valid symfony console style. |
||
663 | * |
||
664 | * @param string $logLevel The log level to map |
||
665 | * |
||
666 | * @return string The apropriate symfony console style |
||
667 | */ |
||
668 | protected function mapLogLevelToStyle($logLevel) |
||
679 | |||
680 | /** |
||
681 | * Return's the PID filename to use. |
||
682 | * |
||
683 | * @return string The PID filename |
||
684 | */ |
||
685 | protected function getPidFilename() |
||
689 | } |
||
690 |
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..