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 |
||
52 | class Simple |
||
53 | { |
||
54 | |||
55 | /** |
||
56 | * The default style to write messages to the symfony console. |
||
57 | * |
||
58 | * @var string |
||
59 | */ |
||
60 | const DEFAULT_STYLE = 'info'; |
||
61 | |||
62 | /** |
||
63 | * The PID filename to use. |
||
64 | * |
||
65 | * @var string |
||
66 | */ |
||
67 | const PID_FILENAME = 'importer.pid'; |
||
68 | |||
69 | /** |
||
70 | * The TechDivision company name as ANSI art. |
||
71 | * |
||
72 | * @var string |
||
73 | */ |
||
74 | protected $ansiArt = ' _______ _ _____ _ _ _ |
||
75 | |__ __| | | | __ \(_) (_) (_) |
||
76 | | | ___ ___| |__ | | | |___ ___ ___ _ ___ _ __ |
||
77 | | |/ _ \/ __| \'_ \| | | | \ \ / / / __| |/ _ \| \'_ \ |
||
78 | | | __/ (__| | | | |__| | |\ V /| \__ \ | (_) | | | | |
||
79 | |_|\___|\___|_| |_|_____/|_| \_/ |_|___/_|\___/|_| |_| |
||
80 | '; |
||
81 | |||
82 | /** |
||
83 | * The log level => console style mapping. |
||
84 | * |
||
85 | * @var array |
||
86 | */ |
||
87 | protected $logLevelStyleMapping = array( |
||
88 | LogLevel::INFO => 'info', |
||
89 | LogLevel::DEBUG => 'comment', |
||
90 | LogLevel::ERROR => 'error', |
||
91 | LogLevel::ALERT => 'error', |
||
92 | LogLevel::CRITICAL => 'error', |
||
93 | LogLevel::EMERGENCY => 'error', |
||
94 | LogLevel::WARNING => 'error', |
||
95 | LogLevel::NOTICE => 'info' |
||
96 | ); |
||
97 | |||
98 | /** |
||
99 | * The actions unique serial. |
||
100 | * |
||
101 | * @var string |
||
102 | */ |
||
103 | protected $serial; |
||
104 | |||
105 | /** |
||
106 | * The system logger implementation. |
||
107 | * |
||
108 | * @var \Psr\Log\LoggerInterface |
||
109 | */ |
||
110 | protected $systemLogger; |
||
111 | |||
112 | /** |
||
113 | * The RegistryProcessor instance to handle running threads. |
||
114 | * |
||
115 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
116 | */ |
||
117 | protected $registryProcessor; |
||
118 | |||
119 | /** |
||
120 | * The processor to read/write the necessary import data. |
||
121 | * |
||
122 | * @var \TechDivision\Import\Services\ImportProcessorInterface |
||
123 | */ |
||
124 | protected $importProcessor; |
||
125 | |||
126 | /** |
||
127 | * The system configuration. |
||
128 | * |
||
129 | * @var \TechDivision\Import\ConfigurationInterface |
||
130 | */ |
||
131 | protected $configuration; |
||
132 | |||
133 | /** |
||
134 | * The input stream to read console information from. |
||
135 | * |
||
136 | * @var \Symfony\Component\Console\Input\InputInterface |
||
137 | */ |
||
138 | protected $input; |
||
139 | |||
140 | /** |
||
141 | * The output stream to write console information to. |
||
142 | * |
||
143 | * @var \Symfony\Component\Console\Output\OutputInterface |
||
144 | */ |
||
145 | protected $output; |
||
146 | |||
147 | /** |
||
148 | * The matches for the last processed CSV filename. |
||
149 | * |
||
150 | * @var array |
||
151 | */ |
||
152 | protected $matches = array(); |
||
153 | |||
154 | /** |
||
155 | * The number of imported bunches. |
||
156 | * |
||
157 | * @var integer |
||
158 | */ |
||
159 | protected $bunches = 0; |
||
160 | |||
161 | /** |
||
162 | * The PID for the running processes. |
||
163 | * |
||
164 | * @var array |
||
165 | */ |
||
166 | protected $pid = null; |
||
167 | |||
168 | /** |
||
169 | * The CSV files that has to be imported. |
||
170 | * |
||
171 | * @var array |
||
172 | */ |
||
173 | protected $files = array(); |
||
174 | |||
175 | /** |
||
176 | * Set's the unique serial for this import process. |
||
177 | * |
||
178 | * @param string $serial The unique serial |
||
179 | * |
||
180 | * @return void |
||
181 | */ |
||
182 | public function setSerial($serial) |
||
186 | |||
187 | /** |
||
188 | * Return's the unique serial for this import process. |
||
189 | * |
||
190 | * @return string The unique serial |
||
191 | */ |
||
192 | public function getSerial() |
||
196 | |||
197 | /** |
||
198 | * Set's the system logger. |
||
199 | * |
||
200 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger |
||
201 | * |
||
202 | * @return void |
||
203 | */ |
||
204 | public function setSystemLogger(LoggerInterface $systemLogger) |
||
208 | |||
209 | /** |
||
210 | * Return's the system logger. |
||
211 | * |
||
212 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
213 | */ |
||
214 | public function getSystemLogger() |
||
218 | |||
219 | /** |
||
220 | * Sets's the RegistryProcessor instance to handle the running threads. |
||
221 | * |
||
222 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
223 | * |
||
224 | * @return void |
||
225 | */ |
||
226 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
230 | |||
231 | /** |
||
232 | * Return's the RegistryProcessor instance to handle the running threads. |
||
233 | * |
||
234 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
235 | */ |
||
236 | public function getRegistryProcessor() |
||
240 | |||
241 | /** |
||
242 | * Set's the import processor instance. |
||
243 | * |
||
244 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
245 | * |
||
246 | * @return void |
||
247 | */ |
||
248 | 1 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
|
252 | |||
253 | /** |
||
254 | * Return's the import processor instance. |
||
255 | * |
||
256 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
257 | */ |
||
258 | 1 | public function getImportProcessor() |
|
262 | |||
263 | /** |
||
264 | * Set's the system configuration. |
||
265 | * |
||
266 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
267 | * |
||
268 | * @return void |
||
269 | */ |
||
270 | public function setConfiguration(ConfigurationInterface $configuration) |
||
274 | |||
275 | /** |
||
276 | * Return's the system configuration. |
||
277 | * |
||
278 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
279 | */ |
||
280 | public function getConfiguration() |
||
284 | |||
285 | /** |
||
286 | * Set's the input stream to read console information from. |
||
287 | * |
||
288 | * @param \Symfony\Component\Console\Input\InputInterface $input An IutputInterface instance |
||
289 | * |
||
290 | * @return void |
||
291 | */ |
||
292 | public function setInput(InputInterface $input) |
||
296 | |||
297 | /** |
||
298 | * Return's the input stream to read console information from. |
||
299 | * |
||
300 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
301 | */ |
||
302 | protected function getInput() |
||
306 | |||
307 | /** |
||
308 | * Set's the output stream to write console information to. |
||
309 | * |
||
310 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
311 | * |
||
312 | * @return void |
||
313 | */ |
||
314 | public function setOutput(OutputInterface $output) |
||
318 | |||
319 | /** |
||
320 | * Return's the output stream to write console information to. |
||
321 | * |
||
322 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
323 | */ |
||
324 | protected function getOutput() |
||
328 | |||
329 | /** |
||
330 | * Return's the source directory that has to be watched for new files. |
||
331 | * |
||
332 | * @return string The source directory |
||
333 | */ |
||
334 | protected function getSourceDir() |
||
338 | |||
339 | /** |
||
340 | * Parse the temporary upload directory for new files to be imported. |
||
341 | * |
||
342 | * @return void |
||
343 | * @throws \Exception Is thrown if the import can't be finished successfully |
||
344 | */ |
||
345 | public function import() |
||
400 | |||
401 | /** |
||
402 | * Clears the PID file from a previous import process, if it has not |
||
403 | * been cleaned up properly. |
||
404 | * |
||
405 | * @return void |
||
406 | */ |
||
407 | public function clearPid() |
||
441 | |||
442 | /** |
||
443 | * This method start's the import process by initializing |
||
444 | * the status and appends it to the registry. |
||
445 | * |
||
446 | * @return void |
||
447 | * @throws \Exception Is thrown, an import process is already running |
||
448 | */ |
||
449 | protected function start() |
||
501 | |||
502 | /** |
||
503 | * Prepares the global data for the import process. |
||
504 | * |
||
505 | * @return void |
||
506 | */ |
||
507 | protected function setUp() |
||
579 | |||
580 | /** |
||
581 | * Process all the subjects defined in the system configuration. |
||
582 | * |
||
583 | * @return void |
||
584 | * @throws \Exception Is thrown, if one of the subjects can't be processed |
||
585 | */ |
||
586 | protected function processSubjects() |
||
615 | |||
616 | /** |
||
617 | * Process the subject with the passed name/identifier. |
||
618 | * |
||
619 | * We create a new, fresh and separate subject for EVERY file here, because this would be |
||
620 | * the starting point to parallelize the import process in a multithreaded/multiprocessed |
||
621 | * environment. |
||
622 | * |
||
623 | * @param \TechDivision\Import\Configuration\SubjectInterface $subject The subject configuration |
||
624 | * |
||
625 | * @return void |
||
626 | * @throws \Exception Is thrown, if the subject can't be processed |
||
627 | */ |
||
628 | protected function processSubject(\TechDivision\Import\Configuration\SubjectInterface $subject) |
||
721 | |||
722 | /** |
||
723 | * Queries whether or not, the passed filename is part of a bunch or not. |
||
724 | * |
||
725 | * @param string $prefix The prefix to query for |
||
726 | * @param string $filename The filename to query for |
||
727 | * |
||
728 | * @return boolean TRUE if the filename is part, else FALSE |
||
729 | */ |
||
730 | 2 | public function isPartOfBunch($prefix, $filename) |
|
770 | |||
771 | /** |
||
772 | * Return's an array with the names of the expected OK files for the actual subject. |
||
773 | * |
||
774 | * @return array The array with the expected OK filenames |
||
775 | */ |
||
776 | protected function getOkFilenames() |
||
803 | |||
804 | /** |
||
805 | * Query whether or not, the passed CSV filename is in the OK file. If the filename was found, |
||
806 | * it'll be returned and the method return TRUE. |
||
807 | * |
||
808 | * If the filename is NOT in the OK file, the method return's FALSE and the CSV should NOT be |
||
809 | * imported/moved. |
||
810 | * |
||
811 | * @param string $filename The CSV filename to query for |
||
812 | * |
||
813 | * @return void |
||
814 | * @throws \Exception Is thrown, if the passed filename is NOT in the OK file or it can NOT be removed from it |
||
815 | */ |
||
816 | protected function removeFromOkFile($filename) |
||
859 | |||
860 | /** |
||
861 | * Factory method to create new handler instances. |
||
862 | * |
||
863 | * @param \TechDivision\Import\Configuration\Subject $subject The subject configuration |
||
864 | * |
||
865 | * @return object The handler instance |
||
866 | */ |
||
867 | public function subjectFactory($subject) |
||
893 | |||
894 | /** |
||
895 | * Lifecycle callback that will be inovked after the |
||
896 | * import process has been finished. |
||
897 | * |
||
898 | * @return void |
||
899 | * @throws \Exception Is thrown, if the |
||
900 | */ |
||
901 | protected function archive() |
||
959 | |||
960 | /** |
||
961 | * Removes the passed directory recursively. |
||
962 | * |
||
963 | * @param string $src Name of the directory to remove |
||
964 | * |
||
965 | * @return void |
||
966 | * @throws \Exception Is thrown, if the directory can not be removed |
||
967 | */ |
||
968 | protected function removeDir($src) |
||
994 | |||
995 | /** |
||
996 | * Simple method that writes the passed method the the console and the |
||
997 | * system logger, if configured and a log level has been passed. |
||
998 | * |
||
999 | * @param string $msg The message to log |
||
1000 | * @param string $logLevel The log level to use |
||
1001 | * |
||
1002 | * @return void |
||
1003 | */ |
||
1004 | protected function log($msg, $logLevel = null) |
||
1021 | |||
1022 | /** |
||
1023 | * Map's the passed log level to a valid symfony console style. |
||
1024 | * |
||
1025 | * @param string $logLevel The log level to map |
||
1026 | * |
||
1027 | * @return string The apropriate symfony console style |
||
1028 | */ |
||
1029 | protected function mapLogLevelToStyle($logLevel) |
||
1040 | |||
1041 | /** |
||
1042 | * Return's the PID filename to use. |
||
1043 | * |
||
1044 | * @return string The PID filename |
||
1045 | */ |
||
1046 | protected function getPidFilename() |
||
1050 | |||
1051 | /** |
||
1052 | * Persist the passed PID to PID filename. |
||
1053 | * |
||
1054 | * @param string $pid The PID of the actual import process to added |
||
1055 | * |
||
1056 | * @return void |
||
1057 | * @throws \Exception Is thrown, if the PID can not be added |
||
1058 | */ |
||
1059 | protected function addPid($pid) |
||
1073 | |||
1074 | /** |
||
1075 | * Remove's the actual PID from the PID file. |
||
1076 | * |
||
1077 | * @param string $pid The PID of the actual import process to be removed |
||
1078 | * |
||
1079 | * @return void |
||
1080 | * @throws \Exception Is thrown, if the PID can not be removed |
||
1081 | */ |
||
1082 | protected function removePid($pid) |
||
1093 | |||
1094 | /** |
||
1095 | * Lifecycle callback that will be inovked after the |
||
1096 | * import process has been finished. |
||
1097 | * |
||
1098 | * @return void |
||
1099 | */ |
||
1100 | protected function tearDown() |
||
1103 | |||
1104 | /** |
||
1105 | * This method finishes the import process and cleans the registry. |
||
1106 | * |
||
1107 | * @return void |
||
1108 | */ |
||
1109 | protected function finish() |
||
1114 | |||
1115 | |||
1116 | /** |
||
1117 | * Remove's the passed line from the file with the passed name. |
||
1118 | * |
||
1119 | * @param string $line The line to be removed |
||
1120 | * @param string $filename The name of the file the line has to be removed |
||
1121 | * |
||
1122 | * @return void |
||
1123 | * @throws \Exception Is thrown, if the line is not found or can not be removed |
||
1124 | */ |
||
1125 | protected function removeLineFromFile($line, $filename) |
||
1177 | } |
||
1178 |