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 | * Set's the unique serial for this import process. |
||
170 | * |
||
171 | * @param string $serial The unique serial |
||
172 | * |
||
173 | * @return void |
||
174 | */ |
||
175 | public function setSerial($serial) |
||
179 | |||
180 | /** |
||
181 | * Return's the unique serial for this import process. |
||
182 | * |
||
183 | * @return string The unique serial |
||
184 | */ |
||
185 | public function getSerial() |
||
189 | |||
190 | /** |
||
191 | * Set's the system logger. |
||
192 | * |
||
193 | * @param \Psr\Log\LoggerInterface $systemLogger The system logger |
||
194 | * |
||
195 | * @return void |
||
196 | */ |
||
197 | public function setSystemLogger(LoggerInterface $systemLogger) |
||
201 | |||
202 | /** |
||
203 | * Return's the system logger. |
||
204 | * |
||
205 | * @return \Psr\Log\LoggerInterface The system logger instance |
||
206 | */ |
||
207 | public function getSystemLogger() |
||
211 | |||
212 | /** |
||
213 | * Sets's the RegistryProcessor instance to handle the running threads. |
||
214 | * |
||
215 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
216 | * |
||
217 | * @return void |
||
218 | */ |
||
219 | public function setRegistryProcessor(RegistryProcessorInterface $registryProcessor) |
||
223 | |||
224 | /** |
||
225 | * Return's the RegistryProcessor instance to handle the running threads. |
||
226 | * |
||
227 | * @return \TechDivision\Import\Services\RegistryProcessor The registry processor instance |
||
228 | */ |
||
229 | public function getRegistryProcessor() |
||
233 | |||
234 | /** |
||
235 | * Set's the import processor instance. |
||
236 | * |
||
237 | * @param \TechDivision\Import\Services\ImportProcessorInterface $importProcessor The import processor instance |
||
238 | * |
||
239 | * @return void |
||
240 | */ |
||
241 | 1 | public function setImportProcessor(ImportProcessorInterface $importProcessor) |
|
245 | |||
246 | /** |
||
247 | * Return's the import processor instance. |
||
248 | * |
||
249 | * @return \TechDivision\Import\Services\ImportProcessorInterface The import processor instance |
||
250 | */ |
||
251 | 1 | public function getImportProcessor() |
|
255 | |||
256 | /** |
||
257 | * Set's the system configuration. |
||
258 | * |
||
259 | * @param \TechDivision\Import\ConfigurationInterface $configuration The system configuration |
||
260 | * |
||
261 | * @return void |
||
262 | */ |
||
263 | public function setConfiguration(ConfigurationInterface $configuration) |
||
267 | |||
268 | /** |
||
269 | * Return's the system configuration. |
||
270 | * |
||
271 | * @return \TechDivision\Import\ConfigurationInterface The system configuration |
||
272 | */ |
||
273 | public function getConfiguration() |
||
277 | |||
278 | /** |
||
279 | * Set's the input stream to read console information from. |
||
280 | * |
||
281 | * @param \Symfony\Component\Console\Input\InputInterface $input An IutputInterface instance |
||
282 | * |
||
283 | * @return void |
||
284 | */ |
||
285 | public function setInput(InputInterface $input) |
||
289 | |||
290 | /** |
||
291 | * Return's the input stream to read console information from. |
||
292 | * |
||
293 | * @return \Symfony\Component\Console\Input\InputInterface An IutputInterface instance |
||
294 | */ |
||
295 | protected function getInput() |
||
299 | |||
300 | /** |
||
301 | * Set's the output stream to write console information to. |
||
302 | * |
||
303 | * @param \Symfony\Component\Console\Output\OutputInterface $output An OutputInterface instance |
||
304 | * |
||
305 | * @return void |
||
306 | */ |
||
307 | public function setOutput(OutputInterface $output) |
||
311 | |||
312 | /** |
||
313 | * Return's the output stream to write console information to. |
||
314 | * |
||
315 | * @return \Symfony\Component\Console\Output\OutputInterface An OutputInterface instance |
||
316 | */ |
||
317 | protected function getOutput() |
||
321 | |||
322 | /** |
||
323 | * Return's the source directory that has to be watched for new files. |
||
324 | * |
||
325 | * @return string The source directory |
||
326 | */ |
||
327 | protected function getSourceDir() |
||
331 | |||
332 | /** |
||
333 | * Parse the temporary upload directory for new files to be imported. |
||
334 | * |
||
335 | * @return void |
||
336 | * @throws \Exception Is thrown if the import can't be finished successfully |
||
337 | */ |
||
338 | public function import() |
||
380 | |||
381 | /** |
||
382 | * This method start's the import process by initializing |
||
383 | * the status and appends it to the registry. |
||
384 | * |
||
385 | * @return void |
||
386 | * @throws \Exception Is thrown, an import process is already running |
||
387 | */ |
||
388 | protected function start() |
||
443 | |||
444 | /** |
||
445 | * Prepares the global data for the import process. |
||
446 | * |
||
447 | * @return void |
||
448 | */ |
||
449 | protected function setUp() |
||
515 | |||
516 | /** |
||
517 | * Process all the subjects defined in the system configuration. |
||
518 | * |
||
519 | * @return void |
||
520 | * @throws \Exception Is thrown, if one of the subjects can't be processed |
||
521 | */ |
||
522 | protected function processSubjects() |
||
551 | |||
552 | /** |
||
553 | * Process the subject with the passed name/identifier. |
||
554 | * |
||
555 | * We create a new, fresh and separate subject for EVERY file here, because this would be |
||
556 | * the starting point to parallelize the import process in a multithreaded/multiprocessed |
||
557 | * environment. |
||
558 | * |
||
559 | * @param \TechDivision\Import\Configuration\SubjectInterface $subject The subject configuration |
||
560 | * |
||
561 | * @return void |
||
562 | * @throws \Exception Is thrown, if the subject can't be processed |
||
563 | */ |
||
564 | protected function processSubject(\TechDivision\Import\Configuration\SubjectInterface $subject) |
||
623 | |||
624 | /** |
||
625 | * Queries whether or not, the passed filename is part of a bunch or not. |
||
626 | * |
||
627 | * @param string $prefix The prefix to query for |
||
628 | * @param string $filename The filename to query for |
||
629 | * |
||
630 | * @return boolean TRUE if the filename is part, else FALSE |
||
631 | */ |
||
632 | 2 | public function isPartOfBunch($prefix, $filename) |
|
672 | |||
673 | /** |
||
674 | * Return's an array with the names of the expected OK files for the actual subject. |
||
675 | * |
||
676 | * @return array The array with the expected OK filenames |
||
677 | */ |
||
678 | protected function getOkFilenames() |
||
705 | |||
706 | /** |
||
707 | * Query whether or not, the passed CSV filename is in the OK file. If the filename was found, |
||
708 | * it'll be returned and the method return TRUE. |
||
709 | * |
||
710 | * If the filename is NOT in the OK file, the method return's FALSE and the CSV should NOT be |
||
711 | * imported/moved. |
||
712 | * |
||
713 | * @param string $filename The CSV filename to query for |
||
714 | * |
||
715 | * @return void |
||
716 | * @throws \Exception Is thrown, if the passed filename is NOT in the OK file or it can NOT be removed from it |
||
717 | */ |
||
718 | protected function removeFromOkFile($filename) |
||
761 | |||
762 | /** |
||
763 | * Factory method to create new handler instances. |
||
764 | * |
||
765 | * @param \TechDivision\Import\Configuration\Subject $subject The subject configuration |
||
766 | * |
||
767 | * @return object The handler instance |
||
768 | */ |
||
769 | public function subjectFactory($subject) |
||
795 | |||
796 | /** |
||
797 | * Lifecycle callback that will be inovked after the |
||
798 | * import process has been finished. |
||
799 | * |
||
800 | * @return void |
||
801 | * @throws \Exception Is thrown, if the |
||
802 | */ |
||
803 | protected function archive() |
||
861 | |||
862 | /** |
||
863 | * Removes the passed directory recursively. |
||
864 | * |
||
865 | * @param string $src Name of the directory to remove |
||
866 | * |
||
867 | * @return void |
||
868 | * @throws \Exception Is thrown, if the directory can not be removed |
||
869 | */ |
||
870 | protected function removeDir($src) |
||
896 | |||
897 | /** |
||
898 | * Simple method that writes the passed method the the console and the |
||
899 | * system logger, if configured and a log level has been passed. |
||
900 | * |
||
901 | * @param string $msg The message to log |
||
902 | * @param string $logLevel The log level to use |
||
903 | * |
||
904 | * @return void |
||
905 | */ |
||
906 | protected function log($msg, $logLevel = null) |
||
923 | |||
924 | /** |
||
925 | * Map's the passed log level to a valid symfony console style. |
||
926 | * |
||
927 | * @param string $logLevel The log level to map |
||
928 | * |
||
929 | * @return string The apropriate symfony console style |
||
930 | */ |
||
931 | protected function mapLogLevelToStyle($logLevel) |
||
942 | |||
943 | /** |
||
944 | * Return's the PID filename to use. |
||
945 | * |
||
946 | * @return string The PID filename |
||
947 | */ |
||
948 | protected function getPidFilename() |
||
952 | |||
953 | /** |
||
954 | * Persist the passed PID to PID filename. |
||
955 | * |
||
956 | * @param string $pid The PID of the actual import process to added |
||
957 | * |
||
958 | * @return void |
||
959 | * @throws \Exception Is thrown, if the PID can not be added |
||
960 | */ |
||
961 | protected function addPid($pid) |
||
975 | |||
976 | /** |
||
977 | * Remove's the actual PID from the PID file. |
||
978 | * |
||
979 | * @param string $pid The PID of the actual import process to be removed |
||
980 | * |
||
981 | * @return void |
||
982 | * @throws \Exception Is thrown, if the PID can not be removed |
||
983 | */ |
||
984 | protected function removePid($pid) |
||
995 | |||
996 | /** |
||
997 | * Lifecycle callback that will be inovked after the |
||
998 | * import process has been finished. |
||
999 | * |
||
1000 | * @return void |
||
1001 | */ |
||
1002 | protected function tearDown() |
||
1007 | |||
1008 | /** |
||
1009 | * This method finishes the import process and cleans the registry. |
||
1010 | * |
||
1011 | * @return void |
||
1012 | */ |
||
1013 | protected function finish() |
||
1018 | |||
1019 | |||
1020 | /** |
||
1021 | * Remove's the passed line from the file with the passed name. |
||
1022 | * |
||
1023 | * @param string $line The line to be removed |
||
1024 | * @param string $filename The name of the file the line has to be removed |
||
1025 | * |
||
1026 | * @return void |
||
1027 | * @throws \Exception Is thrown, if the line is not found or can not be removed |
||
1028 | */ |
||
1029 | protected function removeLineFromFile($line, $filename) |
||
1081 | } |
||
1082 |