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 system logger implementation. |
||
106 | * |
||
107 | * @var \Psr\Log\LoggerInterface |
||
108 | */ |
||
109 | protected $systemLogger; |
||
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 constructor to initialize the instance. |
||
155 | * |
||
156 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger |
||
157 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
158 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
159 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
160 | * @param \Symfony\Component\Console\Input\InputInterface $input An InputInterface instance |
||
161 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
162 | */ |
||
163 | 1 | public function __construct( |
|
183 | |||
184 | /** |
||
185 | * The shutdown handler to catch fatal errors. |
||
186 | * |
||
187 | * This method is need to make sure, that an existing PID file will be removed |
||
188 | * if a fatal error has been triggered. |
||
189 | * |
||
190 | * @return void |
||
191 | */ |
||
192 | public function shutdown() |
||
193 | { |
||
194 | |||
195 | // check if there was a fatal error caused shutdown |
||
196 | if ($lastError = error_get_last()) { |
||
197 | // initialize error type and message |
||
198 | $type = 0; |
||
199 | $message = ''; |
||
200 | // extract the last error values |
||
201 | extract($lastError); |
||
202 | // query whether we've a fatal/user error |
||
203 | if ($type === E_ERROR || $type === E_USER_ERROR) { |
||
204 | // clean-up the PID file |
||
205 | $this->unlock(); |
||
206 | // log the fatal error message |
||
207 | $this->log($message, LogLevel::ERROR); |
||
208 | } |
||
209 | } |
||
210 | } |
||
211 | |||
212 | /** |
||
213 | * Return's the system logger. |
||
214 | * |
||
215 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
216 | */ |
||
217 | public function getSystemLogger() |
||
221 | |||
222 | /** |
||
223 | * Return's the RegistryProcessor instance to handle the running threads. |
||
224 | * |
||
225 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
226 | */ |
||
227 | public function getRegistryProcessor() |
||
231 | |||
232 | /** |
||
233 | * Return's the import processor instance. |
||
234 | * |
||
235 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
236 | */ |
||
237 | public function getImportProcessor() |
||
241 | |||
242 | /** |
||
243 | * Return's the system configuration. |
||
244 | * |
||
245 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
246 | */ |
||
247 | public function getConfiguration() |
||
251 | |||
252 | /** |
||
253 | * Return's the input stream to read console information from. |
||
254 | * |
||
255 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
256 | */ |
||
257 | public function getInput() |
||
261 | |||
262 | /** |
||
263 | * Return's the output stream to write console information to. |
||
264 | * |
||
265 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
266 | */ |
||
267 | 1 | public function getOutput() |
|
271 | |||
272 | /** |
||
273 | * Return's the unique serial for this import process. |
||
274 | * |
||
275 | * @return string The unique serial |
||
276 | */ |
||
277 | public function getSerial() |
||
281 | |||
282 | /** |
||
283 | * Persist the UUID of the actual import process to the PID file. |
||
284 | * |
||
285 | * @return void |
||
286 | * @throws \Exception Is thrown, if the PID can not be added |
||
287 | */ |
||
288 | public function lock() |
||
310 | |||
311 | /** |
||
312 | * Remove's the UUID of the actual import process from the PID file. |
||
313 | * |
||
314 | * @return void |
||
315 | * @throws \Exception Is thrown, if the PID can not be removed |
||
316 | */ |
||
317 | public function unlock() |
||
318 | { |
||
319 | try { |
||
320 | // remove the PID from the PID file if set |
||
321 | if ($this->pid === $this->getSerial()) { |
||
322 | $this->removeLineFromFile($this->pid, $this->getPidFilename()); |
||
323 | } |
||
324 | |||
325 | } catch (FileNotFoundException $fnfe) { |
||
326 | $this->getSystemLogger()->notice(sprintf('PID file %s doesn\'t exist', $this->getPidFilename())); |
||
327 | } catch (LineNotFoundException $lnfe) { |
||
328 | $this->getSystemLogger()->notice(sprintf('PID %s is can not be found in PID file %s', $this->pid, $this->getPidFilename())); |
||
329 | } catch (\Exception $e) { |
||
330 | throw new \Exception(sprintf('Can\'t remove PID %s from PID file %s', $this->pid, $this->getPidFilename()), null, $e); |
||
331 | } |
||
332 | } |
||
333 | |||
334 | /** |
||
335 | * Remove's the passed line from the file with the passed name. |
||
336 | * |
||
337 | * @param string $line The line to be removed |
||
338 | * @param string $filename The name of the file the line has to be removed |
||
339 | * |
||
340 | * @return void |
||
341 | * @throws \Exception Is thrown, if the file doesn't exists, the line is not found or can not be removed |
||
342 | */ |
||
343 | public function removeLineFromFile($line, $filename) |
||
344 | { |
||
345 | |||
346 | // query whether or not the filename |
||
347 | if (!file_exists($filename)) { |
||
348 | throw new FileNotFoundException(sprintf('File %s doesn\' exists', $filename)); |
||
349 | } |
||
350 | |||
351 | // open the PID file |
||
352 | $fh = fopen($filename, 'r+'); |
||
353 | |||
354 | // initialize the array for the PIDs found in the PID file |
||
355 | $lines = array(); |
||
356 | |||
357 | // initialize the flag if the line has been found |
||
358 | $found = false; |
||
359 | |||
360 | // read the lines with the PIDs from the PID file |
||
361 | while (($buffer = fgets($fh, 4096)) !== false) { |
||
362 | // remove the new line |
||
363 | $buffer = trim($buffer, PHP_EOL); |
||
364 | // if the line is the one to be removed, ignore the line |
||
365 | if ($line === $buffer) { |
||
366 | $found = true; |
||
367 | continue; |
||
368 | } |
||
369 | |||
370 | // add the found PID to the array |
||
371 | $lines[] = $buffer; |
||
372 | } |
||
373 | |||
374 | // query whether or not, we found the line |
||
375 | if (!$found) { |
||
376 | throw new LineNotFoundException(sprintf('Line %s can not be found in file %s', $line, $filename)); |
||
377 | } |
||
378 | |||
379 | // if there are NO more lines, delete the file |
||
380 | if (sizeof($lines) === 0) { |
||
381 | fclose($fh); |
||
382 | unlink($filename); |
||
383 | return; |
||
384 | } |
||
385 | |||
386 | // empty the file and rewind the file pointer |
||
387 | ftruncate($fh, 0); |
||
388 | rewind($fh); |
||
389 | |||
390 | // append the existing lines to the file |
||
391 | foreach ($lines as $ln) { |
||
392 | if (fwrite($fh, $ln . PHP_EOL) === false) { |
||
393 | throw new \Exception(sprintf('Can\'t write %s to file %s', $ln, $filename)); |
||
394 | } |
||
395 | } |
||
396 | |||
397 | // finally close the file |
||
398 | fclose($fh); |
||
399 | } |
||
400 | |||
401 | /** |
||
402 | * Process the given operation. |
||
403 | * |
||
404 | * @return void |
||
405 | * @throws \Exception Is thrown if the operation can't be finished successfully |
||
406 | */ |
||
407 | public function process() |
||
408 | { |
||
409 | |||
410 | try { |
||
411 | // track the start time |
||
412 | $startTime = microtime(true); |
||
413 | |||
414 | // start the transaction |
||
415 | $this->getImportProcessor()->getConnection()->beginTransaction(); |
||
416 | |||
417 | // prepare the global data for the import process |
||
418 | $this->setUp(); |
||
419 | |||
420 | // process the plugins defined in the configuration |
||
421 | foreach ($this->getConfiguration()->getPlugins() as $pluginConfiguration) { |
||
422 | $this->pluginFactory($pluginConfiguration)->process(); |
||
423 | } |
||
424 | |||
425 | // tear down the instance |
||
426 | $this->tearDown(); |
||
427 | |||
428 | // commit the transaction |
||
429 | $this->getImportProcessor()->getConnection()->commit(); |
||
430 | |||
431 | // track the time needed for the import in seconds |
||
432 | $endTime = microtime(true) - $startTime; |
||
433 | |||
434 | // log a message that import has been finished |
||
435 | $this->log( |
||
436 | sprintf( |
||
437 | 'Successfully finished import with serial %s in %f s', |
||
438 | $this->getSerial(), |
||
439 | $endTime |
||
440 | ), |
||
441 | LogLevel::INFO |
||
442 | ); |
||
443 | |||
444 | } catch (\Exception $e) { |
||
445 | // tear down |
||
446 | $this->tearDown(); |
||
447 | |||
448 | // rollback the transaction |
||
449 | $this->getImportProcessor()->getConnection()->rollBack(); |
||
450 | |||
451 | // track the time needed for the import in seconds |
||
452 | $endTime = microtime(true) - $startTime; |
||
453 | |||
454 | // log a message that the file import failed |
||
455 | $this->getSystemLogger()->error($e->__toString()); |
||
456 | |||
457 | // log a message that import has been finished |
||
458 | $this->getSystemLogger()->info( |
||
459 | sprintf( |
||
460 | 'Can\'t finish import with serial %s in %f s', |
||
461 | $this->getSerial(), |
||
462 | $endTime |
||
463 | ) |
||
464 | ); |
||
465 | |||
466 | // re-throw the exception |
||
467 | throw $e; |
||
468 | } |
||
469 | } |
||
470 | |||
471 | /** |
||
472 | * Factory method to create new plugin instances. |
||
473 | * |
||
474 | * @param \TechDivision\Import\Configuration\PluginConfigurationInterface $pluginConfiguration The plugin configuration instance |
||
475 | * |
||
476 | * @return object The plugin instance |
||
477 | */ |
||
478 | protected function pluginFactory(PluginConfigurationInterface $pluginConfiguration) |
||
487 | |||
488 | /** |
||
489 | * Lifecycle callback that will be inovked before the |
||
490 | * import process has been started. |
||
491 | * |
||
492 | * @return void |
||
493 | */ |
||
494 | protected function setUp() |
||
495 | { |
||
496 | |||
497 | // generate the serial for the new job |
||
498 | $this->serial = Uuid::uuid4()->__toString(); |
||
499 | |||
500 | // query whether or not an import is running AND an existing PID has to be ignored |
||
501 | if (file_exists($pidFilename = $this->getPidFilename()) && !$this->getConfiguration()->isIgnorePid()) { |
||
502 | throw new \Exception(sprintf('At least one import process is already running (check PID: %s)', $pidFilename)); |
||
503 | } elseif (file_exists($pidFilename = $this->getPidFilename()) && $this->getConfiguration()->isIgnorePid()) { |
||
504 | $this->log(sprintf('At least one import process is already running (PID: %s)', $pidFilename), LogLevel::WARNING); |
||
505 | } |
||
506 | |||
507 | // write the TechDivision ANSI art icon to the console |
||
508 | $this->log($this->ansiArt); |
||
509 | |||
510 | // log the debug information, if debug mode is enabled |
||
511 | if ($this->getConfiguration()->isDebugMode()) { |
||
512 | // log the system's PHP configuration |
||
513 | $this->log(sprintf('PHP version: %s', phpversion()), LogLevel::DEBUG); |
||
514 | $this->log('-------------------- Loaded Extensions -----------------------', LogLevel::DEBUG); |
||
515 | $this->log(implode(', ', $loadedExtensions = get_loaded_extensions()), LogLevel::DEBUG); |
||
516 | $this->log('--------------------------------------------------------------', LogLevel::DEBUG); |
||
517 | |||
518 | // write a warning for low performance, if XDebug extension is activated |
||
519 | if (in_array('xdebug', $loadedExtensions)) { |
||
520 | $this->log('Low performance exptected, as result of enabled XDebug extension!', LogLevel::WARNING); |
||
521 | } |
||
522 | } |
||
523 | |||
524 | // log a message that import has been started |
||
525 | $this->log( |
||
526 | sprintf( |
||
527 | 'Now start import with serial %s (operation: %s)', |
||
528 | $this->getSerial(), |
||
529 | $this->getConfiguration()->getOperationName() |
||
530 | ), |
||
531 | LogLevel::INFO |
||
532 | ); |
||
533 | |||
534 | // initialize the status |
||
535 | $status = array( |
||
536 | RegistryKeys::STATUS => 1, |
||
537 | RegistryKeys::BUNCHES => 0, |
||
538 | RegistryKeys::SOURCE_DIRECTORY => $this->getConfiguration()->getSourceDir() |
||
539 | ); |
||
540 | |||
541 | // append it to the registry |
||
542 | $this->getRegistryProcessor()->setAttribute($this->getSerial(), $status); |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * Lifecycle callback that will be inovked after the |
||
547 | * import process has been finished. |
||
548 | * |
||
549 | * @return void |
||
550 | */ |
||
551 | protected function tearDown() |
||
555 | |||
556 | /** |
||
557 | * Simple method that writes the passed method the the console and the |
||
558 | * system logger, if configured and a log level has been passed. |
||
559 | * |
||
560 | * @param string $msg The message to log |
||
561 | * @param string $logLevel The log level to use |
||
562 | * |
||
563 | * @return void |
||
564 | */ |
||
565 | protected function log($msg, $logLevel = null) |
||
566 | { |
||
567 | |||
568 | // initialize the formatter helper |
||
569 | $helper = new FormatterHelper(); |
||
570 | |||
571 | // map the log level to the console style |
||
572 | $style = $this->mapLogLevelToStyle($logLevel); |
||
573 | |||
574 | // format the message, according to the passed log level and write it to the console |
||
575 | $this->getOutput()->writeln($logLevel ? $helper->formatBlock($msg, $style) : $msg); |
||
576 | |||
577 | // log the message if a log level has been passed |
||
578 | if ($logLevel && $systemLogger = $this->getSystemLogger()) { |
||
579 | $systemLogger->log($logLevel, $msg); |
||
580 | } |
||
581 | } |
||
582 | |||
583 | /** |
||
584 | * Map's the passed log level to a valid symfony console style. |
||
585 | * |
||
586 | * @param string $logLevel The log level to map |
||
587 | * |
||
588 | * @return string The apropriate symfony console style |
||
589 | */ |
||
590 | protected function mapLogLevelToStyle($logLevel) |
||
601 | |||
602 | /** |
||
603 | * Return's the PID filename to use. |
||
604 | * |
||
605 | * @return string The PID filename |
||
606 | */ |
||
607 | protected function getPidFilename() |
||
611 | } |
||
612 |
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..