Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
47 | class Simple |
||
48 | { |
||
49 | |||
50 | /** |
||
51 | * The default style to write messages to the symfony console. |
||
52 | * |
||
53 | * @var string |
||
54 | */ |
||
55 | const DEFAULT_STYLE = 'info'; |
||
56 | |||
57 | /** |
||
58 | * The TechDivision company name as ANSI art. |
||
59 | * |
||
60 | * @var string |
||
61 | */ |
||
62 | protected $ansiArt = ' _______ _ _____ _ _ _ |
||
63 | |__ __| | | | __ \(_) (_) (_) |
||
64 | | | ___ ___| |__ | | | |___ ___ ___ _ ___ _ __ |
||
65 | | |/ _ \/ __| \'_ \| | | | \ \ / / / __| |/ _ \| \'_ \ |
||
66 | | | __/ (__| | | | |__| | |\ V /| \__ \ | (_) | | | | |
||
67 | |_|\___|\___|_| |_|_____/|_| \_/ |_|___/_|\___/|_| |_| |
||
68 | '; |
||
69 | |||
70 | /** |
||
71 | * The log level => console style mapping. |
||
72 | * |
||
73 | * @var array |
||
74 | */ |
||
75 | protected $logLevelStyleMapping = array( |
||
76 | LogLevel::INFO => 'info', |
||
77 | LogLevel::DEBUG => 'comment', |
||
78 | LogLevel::ERROR => 'error', |
||
79 | LogLevel::ALERT => 'error', |
||
80 | LogLevel::CRITICAL => 'error', |
||
81 | LogLevel::EMERGENCY => 'error', |
||
82 | LogLevel::WARNING => 'error', |
||
83 | LogLevel::NOTICE => 'info' |
||
84 | ); |
||
85 | |||
86 | /** |
||
87 | * The actions unique serial. |
||
88 | * |
||
89 | * @var string |
||
90 | */ |
||
91 | protected $serial; |
||
92 | |||
93 | /** |
||
94 | * The system logger implementation. |
||
95 | * |
||
96 | * @var \Psr\Log\LoggerInterface |
||
97 | */ |
||
98 | protected $systemLogger; |
||
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 system configuration. |
||
116 | * |
||
117 | * @var \TechDivision\Import\ConfigurationInterface |
||
118 | */ |
||
119 | protected $configuration; |
||
120 | |||
121 | /** |
||
122 | * The input stream to read console information from. |
||
123 | * |
||
124 | * @var \Symfony\Component\Console\Input\InputInterface |
||
125 | */ |
||
126 | protected $input; |
||
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 | * Set's the unique serial for this import process. |
||
137 | * |
||
138 | * @param string $serial The unique serial |
||
139 | * |
||
140 | * @return void |
||
141 | */ |
||
142 | public function setSerial($serial) |
||
146 | |||
147 | /** |
||
148 | * Return's the unique serial for this import process. |
||
149 | * |
||
150 | * @return string The unique serial |
||
151 | */ |
||
152 | public function getSerial() |
||
156 | |||
157 | /** |
||
158 | * Set's the system logger. |
||
159 | * |
||
160 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger |
||
161 | * |
||
162 | * @return void |
||
163 | */ |
||
164 | public function setSystemLogger(LoggerInterface $systemLogger) |
||
168 | |||
169 | /** |
||
170 | * Return's the system logger. |
||
171 | * |
||
172 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
173 | */ |
||
174 | public function getSystemLogger() |
||
178 | |||
179 | /** |
||
180 | * Sets's the RegistryProcessor instance to handle the running threads. |
||
181 | * |
||
182 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
183 | * |
||
184 | * @return void |
||
185 | */ |
||
186 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
190 | |||
191 | /** |
||
192 | * Return's the RegistryProcessor instance to handle the running threads. |
||
193 | * |
||
194 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
195 | */ |
||
196 | public function getRegistryProcessor() |
||
200 | |||
201 | /** |
||
202 | * Set's the import processor instance. |
||
203 | * |
||
204 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
205 | * |
||
206 | * @return void |
||
207 | */ |
||
208 | 1 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
|
212 | |||
213 | /** |
||
214 | * Return's the import processor instance. |
||
215 | * |
||
216 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
217 | */ |
||
218 | 1 | public function getImportProcessor() |
|
222 | |||
223 | /** |
||
224 | * Set's the system configuration. |
||
225 | * |
||
226 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
227 | * |
||
228 | * @return void |
||
229 | */ |
||
230 | public function setConfiguration(ConfigurationInterface $configuration) |
||
234 | |||
235 | /** |
||
236 | * Return's the system configuration. |
||
237 | * |
||
238 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
239 | */ |
||
240 | public function getConfiguration() |
||
244 | |||
245 | /** |
||
246 | * Set's the input stream to read console information from. |
||
247 | * |
||
248 | * @param \Symfony\Component\Console\Input\InputInterface $input An IutputInterface instance |
||
249 | * |
||
250 | * @return void |
||
251 | */ |
||
252 | public function setInput(InputInterface $input) |
||
256 | |||
257 | /** |
||
258 | * Return's the input stream to read console information from. |
||
259 | * |
||
260 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
261 | */ |
||
262 | protected function getInput() |
||
266 | |||
267 | /** |
||
268 | * Set's the output stream to write console information to. |
||
269 | * |
||
270 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
271 | * |
||
272 | * @return void |
||
273 | */ |
||
274 | public function setOutput(OutputInterface $output) |
||
278 | |||
279 | /** |
||
280 | * Return's the output stream to write console information to. |
||
281 | * |
||
282 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
283 | */ |
||
284 | protected function getOutput() |
||
288 | |||
289 | /** |
||
290 | * Return's the prefix for the import files. |
||
291 | * |
||
292 | * @return string The prefix |
||
293 | */ |
||
294 | protected function getPrefix() |
||
298 | |||
299 | /** |
||
300 | * Return's the source directory that has to be watched for new files. |
||
301 | * |
||
302 | * @return string The source directory |
||
303 | */ |
||
304 | protected function getSourceDir() |
||
308 | |||
309 | /** |
||
310 | * Parse the temporary upload directory for new files to be imported. |
||
311 | * |
||
312 | * @return void |
||
313 | * @throws \Exception Is thrown if the import can't be finished successfully |
||
314 | */ |
||
315 | public function import() |
||
357 | |||
358 | /** |
||
359 | * This method start's the import process by initializing |
||
360 | * the status and appends it to the registry. |
||
361 | * |
||
362 | * @return void |
||
363 | */ |
||
364 | protected function start() |
||
402 | |||
403 | /** |
||
404 | * Prepares the global data for the import process. |
||
405 | * |
||
406 | * @return void |
||
407 | */ |
||
408 | protected function setUp() |
||
474 | |||
475 | /** |
||
476 | * Process all the subjects defined in the system configuration. |
||
477 | * |
||
478 | * @return void |
||
479 | * @throws \Exception Is thrown, if one of the subjects can't be processed |
||
480 | */ |
||
481 | protected function processSubjects() |
||
510 | |||
511 | /** |
||
512 | * Process the subject with the passed name/identifier. |
||
513 | * |
||
514 | * We create a new, fresh and separate subject for EVERY file here, because this would be |
||
515 | * the starting point to parallelize the import process in a multithreaded/multiprocessed |
||
516 | * environment. |
||
517 | * |
||
518 | * @param \TechDivision\Import\Configuration\Subject $subject The subject configuration |
||
519 | * |
||
520 | * @return void |
||
521 | * @throws \Exception Is thrown, if the subject can't be processed |
||
522 | */ |
||
523 | protected function processSubject($subject) |
||
551 | |||
552 | /** |
||
553 | * Factory method to create new handler instances. |
||
554 | * |
||
555 | * @param \TechDivision\Import\Configuration\Subject $subject The subject configuration |
||
556 | * |
||
557 | * @return object The handler instance |
||
558 | */ |
||
559 | public function subjectFactory($subject) |
||
589 | |||
590 | /** |
||
591 | * Lifecycle callback that will be inovked after the |
||
592 | * import process has been finished. |
||
593 | * |
||
594 | * @return void |
||
595 | * @throws \Exception Is thrown, if the |
||
596 | */ |
||
597 | protected function archive() |
||
658 | |||
659 | /** |
||
660 | * Removes the passed directory recursively. |
||
661 | * |
||
662 | * @param string $src Name of the directory to remove |
||
663 | * |
||
664 | * @return void |
||
665 | */ |
||
666 | protected function removeDir($src) |
||
688 | |||
689 | /** |
||
690 | * Simple method that writes the passed method the the console and the |
||
691 | * system logger, if configured and a log level has been passed. |
||
692 | * |
||
693 | * @param string $msg The message to log |
||
694 | * @param string $logLevel The log level to use |
||
695 | * |
||
696 | * @return void |
||
697 | */ |
||
698 | protected function log($msg, $logLevel = null) |
||
715 | |||
716 | /** |
||
717 | * Map's the passed log level to a valid symfony console style. |
||
718 | * |
||
719 | * @param string The log level to map |
||
720 | * |
||
721 | * @return string The apropriate symfony console style |
||
722 | */ |
||
723 | protected function mapLogLevelToStyle($logLevel) |
||
734 | |||
735 | /** |
||
736 | * Lifecycle callback that will be inovked after the |
||
737 | * import process has been finished. |
||
738 | * |
||
739 | * @return void |
||
740 | * @throws \Exception Is thrown, if the |
||
741 | */ |
||
742 | protected function tearDown() |
||
745 | |||
746 | /** |
||
747 | * This method finishes the import process and cleans the registry. |
||
748 | * |
||
749 | * @return void |
||
750 | */ |
||
751 | protected function finish() |
||
755 | } |
||
756 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.