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 AbstractSubject 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 AbstractSubject, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
50 | abstract class AbstractSubject implements SubjectInterface, FilesystemSubjectInterface, DateConverterSubjectInterface |
||
51 | { |
||
52 | |||
53 | /** |
||
54 | * The trait that provides basic filesystem handling functionality. |
||
55 | * |
||
56 | * @var \TechDivision\Import\Subjects\FilesystemTrait |
||
57 | */ |
||
58 | use FilesystemTrait; |
||
59 | |||
60 | /** |
||
61 | * The trait that provides basic filesystem handling functionality. |
||
62 | * |
||
63 | * @var \TechDivision\Import\SystemLoggerTrait |
||
64 | */ |
||
65 | use SystemLoggerTrait; |
||
66 | |||
67 | /** |
||
68 | * The trait that provides date converting functionality. |
||
69 | * |
||
70 | * @var \TechDivision\Import\DateConverterTrait |
||
71 | */ |
||
72 | use DateConverterTrait; |
||
73 | |||
74 | /** |
||
75 | * The trait that provides header handling functionality. |
||
76 | * |
||
77 | * @var \TechDivision\Import\HeaderTrait |
||
78 | */ |
||
79 | use HeaderTrait; |
||
80 | |||
81 | /** |
||
82 | * The trait that provides row handling functionality. |
||
83 | * |
||
84 | * @var \TechDivision\Import\RowTrait |
||
85 | */ |
||
86 | use RowTrait; |
||
87 | |||
88 | /** |
||
89 | * The name of the file to be imported. |
||
90 | * |
||
91 | * @var string |
||
92 | */ |
||
93 | protected $filename; |
||
94 | |||
95 | /** |
||
96 | * The actual line number. |
||
97 | * |
||
98 | * @var integer |
||
99 | */ |
||
100 | protected $lineNumber = 0; |
||
101 | |||
102 | /** |
||
103 | * The actual operation name. |
||
104 | * |
||
105 | * @var string |
||
106 | */ |
||
107 | protected $operationName ; |
||
108 | |||
109 | /** |
||
110 | * The import adapter instance. |
||
111 | * |
||
112 | * @var \TechDivision\Import\Adapter\ImportAdapterInterface |
||
113 | */ |
||
114 | protected $importAdapter; |
||
115 | |||
116 | /** |
||
117 | * The system configuration. |
||
118 | * |
||
119 | * @var \TechDivision\Import\Configuration\SubjectConfigurationInterface |
||
120 | */ |
||
121 | protected $configuration; |
||
122 | |||
123 | /** |
||
124 | * The RegistryProcessor instance to handle running threads. |
||
125 | * |
||
126 | * @var \TechDivision\Import\Services\RegistryProcessorInterface |
||
127 | */ |
||
128 | protected $registryProcessor; |
||
129 | |||
130 | /** |
||
131 | * The actions unique serial. |
||
132 | * |
||
133 | * @var string |
||
134 | */ |
||
135 | protected $serial; |
||
136 | |||
137 | /** |
||
138 | * Array with the subject's observers. |
||
139 | * |
||
140 | * @var array |
||
141 | */ |
||
142 | protected $observers = array(); |
||
143 | |||
144 | /** |
||
145 | * Array with the subject's callbacks. |
||
146 | * |
||
147 | * @var array |
||
148 | */ |
||
149 | protected $callbacks = array(); |
||
150 | |||
151 | /** |
||
152 | * The subject's callback mappings. |
||
153 | * |
||
154 | * @var array |
||
155 | */ |
||
156 | protected $callbackMappings = array(); |
||
157 | |||
158 | /** |
||
159 | * The available root categories. |
||
160 | * |
||
161 | * @var array |
||
162 | */ |
||
163 | protected $rootCategories = array(); |
||
164 | |||
165 | /** |
||
166 | * The Magento configuration. |
||
167 | * |
||
168 | * @var array |
||
169 | */ |
||
170 | protected $coreConfigData = array(); |
||
171 | |||
172 | /** |
||
173 | * The available stores. |
||
174 | * |
||
175 | * @var array |
||
176 | */ |
||
177 | protected $stores = array(); |
||
178 | |||
179 | /** |
||
180 | * The available websites. |
||
181 | * |
||
182 | * @var array |
||
183 | */ |
||
184 | protected $storeWebsites = array(); |
||
185 | |||
186 | /** |
||
187 | * The default store. |
||
188 | * |
||
189 | * @var array |
||
190 | */ |
||
191 | protected $defaultStore; |
||
192 | |||
193 | /** |
||
194 | * The store view code the create the product/attributes for. |
||
195 | * |
||
196 | * @var string |
||
197 | */ |
||
198 | protected $storeViewCode; |
||
199 | |||
200 | /** |
||
201 | * The UID generator for the core config data. |
||
202 | * |
||
203 | * @var \TechDivision\Import\Utils\Generators\GeneratorInterface |
||
204 | */ |
||
205 | protected $coreConfigDataUidGenerator; |
||
206 | |||
207 | /** |
||
208 | * UNIX timestamp with the date the last row counter has been logged. |
||
209 | * |
||
210 | * @var integer |
||
211 | */ |
||
212 | protected $lastLog = 0; |
||
213 | |||
214 | /** |
||
215 | * The number of the last line that has been logged with the row counter |
||
216 | * @var integer |
||
217 | */ |
||
218 | protected $lastLineNumber = 0; |
||
219 | |||
220 | /** |
||
221 | * The event emitter instance. |
||
222 | * |
||
223 | * @var \League\Event\EmitterInterface |
||
224 | */ |
||
225 | protected $emitter; |
||
226 | |||
227 | /** |
||
228 | * Initialize the subject instance. |
||
229 | * |
||
230 | * @param \TechDivision\Import\Services\RegistryProcessorInterface $registryProcessor The registry processor instance |
||
231 | * @param \TechDivision\Import\Utils\Generators\GeneratorInterface $coreConfigDataUidGenerator The UID generator for the core config data |
||
232 | * @param \Doctrine\Common\Collections\Collection $systemLoggers The array with the system loggers instances |
||
233 | * @param \League\Event\EmitterInterface $emitter The event emitter instance |
||
234 | */ |
||
235 | 82 | public function __construct( |
|
246 | |||
247 | /** |
||
248 | * Return's the event emitter instance. |
||
249 | * |
||
250 | * @return \League\Event\EmitterInterface The event emitter instance |
||
251 | */ |
||
252 | 9 | public function getEmitter() |
|
256 | |||
257 | /** |
||
258 | * Set's the name of the file to import |
||
259 | * |
||
260 | * @param string $filename The filename |
||
261 | * |
||
262 | * @return void |
||
263 | */ |
||
264 | 13 | public function setFilename($filename) |
|
268 | |||
269 | /** |
||
270 | * Return's the name of the file to import. |
||
271 | * |
||
272 | * @return string The filename |
||
273 | */ |
||
274 | 12 | public function getFilename() |
|
278 | |||
279 | /** |
||
280 | * Set's the actual operation name. |
||
281 | * |
||
282 | * @param string $operationName The actual operation name |
||
283 | * |
||
284 | * @return void |
||
285 | */ |
||
286 | 1 | public function setOperationName($operationName) |
|
290 | |||
291 | /** |
||
292 | * Return's the actual operation name. |
||
293 | * |
||
294 | * @return string |
||
295 | */ |
||
296 | 1 | public function getOperationName() |
|
300 | |||
301 | /** |
||
302 | * Set's the actual line number. |
||
303 | * |
||
304 | * @param integer $lineNumber The line number |
||
305 | * |
||
306 | * @return void |
||
307 | */ |
||
308 | 1 | public function setLineNumber($lineNumber) |
|
312 | |||
313 | /** |
||
314 | * Return's the actual line number. |
||
315 | * |
||
316 | * @return integer The line number |
||
317 | */ |
||
318 | 12 | public function getLineNumber() |
|
322 | |||
323 | /** |
||
324 | * Return's the default callback mappings. |
||
325 | * |
||
326 | * @return array The default callback mappings |
||
327 | */ |
||
328 | 1 | public function getDefaultCallbackMappings() |
|
332 | |||
333 | /** |
||
334 | * Tries to format the passed value to a valid date with format 'Y-m-d H:i:s'. |
||
335 | * If the passed value is NOT a valid date, NULL will be returned. |
||
336 | * |
||
337 | * @param string $value The value to format |
||
338 | * |
||
339 | * @return string|null The formatted date or NULL if the date is not valid |
||
340 | */ |
||
341 | public function formatDate($value) |
||
345 | |||
346 | /** |
||
347 | * Extracts the elements of the passed value by exploding them |
||
348 | * with the also passed delimiter. |
||
349 | * |
||
350 | * @param string $value The value to extract |
||
351 | * @param string|null $delimiter The delimiter used to extrace the elements |
||
352 | * |
||
353 | * @return array The exploded values |
||
354 | */ |
||
355 | public function explode($value, $delimiter = null) |
||
359 | |||
360 | /** |
||
361 | * Queries whether or not debug mode is enabled or not, default is TRUE. |
||
362 | * |
||
363 | * @return boolean TRUE if debug mode is enabled, else FALSE |
||
364 | */ |
||
365 | 1 | public function isDebugMode() |
|
369 | |||
370 | /** |
||
371 | * Set's the subject configuration. |
||
372 | * |
||
373 | * @param \TechDivision\Import\Configuration\SubjectConfigurationInterface $configuration The subject configuration |
||
374 | * |
||
375 | * @return void |
||
376 | */ |
||
377 | 82 | public function setConfiguration(SubjectConfigurationInterface $configuration) |
|
381 | |||
382 | /** |
||
383 | * Return's the subject configuration. |
||
384 | * |
||
385 | * @return \TechDivision\Import\Configuration\SubjectConfigurationInterface The subject configuration |
||
386 | */ |
||
387 | 82 | public function getConfiguration() |
|
391 | |||
392 | /** |
||
393 | * Set's the import adapter instance. |
||
394 | * |
||
395 | * @param \TechDivision\Import\Adapter\ImportAdapterInterface $importAdapter The import adapter instance |
||
396 | * |
||
397 | * @return void |
||
398 | */ |
||
399 | 1 | public function setImportAdapter(ImportAdapterInterface $importAdapter) |
|
403 | |||
404 | /** |
||
405 | * Return's the import adapter instance. |
||
406 | * |
||
407 | * @return \TechDivision\Import\Adapter\ImportAdapterInterface The import adapter instance |
||
408 | */ |
||
409 | 1 | public function getImportAdapter() |
|
413 | |||
414 | /** |
||
415 | * Return's the RegistryProcessor instance to handle the running threads. |
||
416 | * |
||
417 | * @return \TechDivision\Import\Services\RegistryProcessorInterface The registry processor instance |
||
418 | */ |
||
419 | 82 | public function getRegistryProcessor() |
|
423 | |||
424 | /** |
||
425 | * Set's the unique serial for this import process. |
||
426 | * |
||
427 | * @param string $serial The unique serial |
||
428 | * |
||
429 | * @return void |
||
430 | */ |
||
431 | 9 | public function setSerial($serial) |
|
435 | |||
436 | /** |
||
437 | * Return's the unique serial for this import process. |
||
438 | * |
||
439 | * @return string The unique serial |
||
440 | */ |
||
441 | 2 | public function getSerial() |
|
445 | |||
446 | /** |
||
447 | * Return's the source date format to use. |
||
448 | * |
||
449 | * @return string The source date format |
||
450 | */ |
||
451 | public function getSourceDateFormat() |
||
455 | |||
456 | /** |
||
457 | * Return's the multiple field delimiter character to use, default value is comma (,). |
||
458 | * |
||
459 | * @return string The multiple field delimiter character |
||
460 | */ |
||
461 | 1 | public function getMultipleFieldDelimiter() |
|
465 | |||
466 | /** |
||
467 | * Return's the multiple value delimiter character to use, default value is comma (|). |
||
468 | * |
||
469 | * @return string The multiple value delimiter character |
||
470 | */ |
||
471 | 1 | public function getMultipleValueDelimiter() |
|
475 | |||
476 | /** |
||
477 | * Intializes the previously loaded global data for exactly one bunch. |
||
478 | * |
||
479 | * @param string $serial The serial of the actual import |
||
480 | * |
||
481 | * @return void |
||
482 | */ |
||
483 | 82 | public function setUp($serial) |
|
524 | |||
525 | /** |
||
526 | * Clean up the global data after importing the variants. |
||
527 | * |
||
528 | * @param string $serial The serial of the actual import |
||
529 | * |
||
530 | * @return void |
||
531 | */ |
||
532 | 1 | public function tearDown($serial) |
|
552 | |||
553 | /** |
||
554 | * Return's the target directory for the artefact export. |
||
555 | * |
||
556 | * @return string The target directory for the artefact export |
||
557 | */ |
||
558 | 1 | public function getTargetDir() |
|
562 | |||
563 | /** |
||
564 | * Return's the next source directory, which will be the target directory |
||
565 | * of this subject, in most cases. |
||
566 | * |
||
567 | * @param string $serial The serial of the actual import |
||
568 | * |
||
569 | * @return string The new source directory |
||
570 | */ |
||
571 | 5 | public function getNewSourceDir($serial) |
|
575 | |||
576 | /** |
||
577 | * Register the passed observer with the specific type. |
||
578 | * |
||
579 | * @param \TechDivision\Import\Observers\ObserverInterface $observer The observer to register |
||
580 | * @param string $type The type to register the observer with |
||
581 | * |
||
582 | * @return void |
||
583 | */ |
||
584 | 6 | public function registerObserver(ObserverInterface $observer, $type) |
|
596 | |||
597 | /** |
||
598 | * Register the passed callback with the specific type. |
||
599 | * |
||
600 | * @param \TechDivision\Import\Callbacks\CallbackInterface $callback The subject to register the callbacks for |
||
601 | * @param string $type The type to register the callback with |
||
602 | * |
||
603 | * @return void |
||
604 | */ |
||
605 | 2 | public function registerCallback(CallbackInterface $callback, $type) |
|
617 | |||
618 | /** |
||
619 | * Return's the array with callbacks for the passed type. |
||
620 | * |
||
621 | * @param string $type The type of the callbacks to return |
||
622 | * |
||
623 | * @return array The callbacks |
||
624 | */ |
||
625 | 1 | public function getCallbacksByType($type) |
|
639 | |||
640 | /** |
||
641 | * Return's the array with the available observers. |
||
642 | * |
||
643 | * @return array The observers |
||
644 | */ |
||
645 | 6 | public function getObservers() |
|
649 | |||
650 | /** |
||
651 | * Return's the array with the available callbacks. |
||
652 | * |
||
653 | * @return array The callbacks |
||
654 | */ |
||
655 | 1 | public function getCallbacks() |
|
659 | |||
660 | /** |
||
661 | * Return's the callback mappings for this subject. |
||
662 | * |
||
663 | * @return array The array with the subject's callback mappings |
||
664 | */ |
||
665 | 2 | public function getCallbackMappings() |
|
669 | |||
670 | /** |
||
671 | * Imports the content of the file with the passed filename. |
||
672 | * |
||
673 | * |
||
674 | * @param string $serial The serial of the actual import |
||
675 | * @param string $filename The filename to process |
||
676 | * |
||
677 | * @return void |
||
678 | * @throws \Exception Is thrown, if the import can't be processed |
||
679 | */ |
||
680 | 2 | public function import($serial, $filename) |
|
756 | |||
757 | /** |
||
758 | * Imports the passed row into the database. If the import failed, the exception |
||
759 | * will be catched and logged, but the import process will be continued. |
||
760 | * |
||
761 | * @param array $row The row with the data to be imported |
||
762 | * |
||
763 | * @return void |
||
764 | */ |
||
765 | 7 | public function importRow(array $row) |
|
848 | |||
849 | /** |
||
850 | * Queries whether or not that the subject needs an OK file to be processed. |
||
851 | * |
||
852 | * @return boolean TRUE if the subject needs an OK file, else FALSE |
||
853 | */ |
||
854 | 1 | public function isOkFileNeeded() |
|
858 | |||
859 | /** |
||
860 | * Return's the default store. |
||
861 | * |
||
862 | * @return array The default store |
||
863 | */ |
||
864 | public function getDefaultStore() |
||
868 | |||
869 | /** |
||
870 | * Return's the default store view code. |
||
871 | * |
||
872 | * @return array The default store view code |
||
873 | */ |
||
874 | 5 | public function getDefaultStoreViewCode() |
|
878 | |||
879 | /** |
||
880 | * Set's the store view code the create the product/attributes for. |
||
881 | * |
||
882 | * @param string $storeViewCode The store view code |
||
883 | * |
||
884 | * @return void |
||
885 | */ |
||
886 | 4 | public function setStoreViewCode($storeViewCode) |
|
890 | |||
891 | /** |
||
892 | * Return's the store view code the create the product/attributes for. |
||
893 | * |
||
894 | * @param string|null $default The default value to return, if the store view code has not been set |
||
895 | * |
||
896 | * @return string The store view code |
||
897 | */ |
||
898 | 8 | public function getStoreViewCode($default = null) |
|
915 | |||
916 | /** |
||
917 | * Prepare's the store view code in the subject. If the store_view_code row doesn't contain |
||
918 | * any value, the default code of the default store view will be set. |
||
919 | * |
||
920 | * @return void |
||
921 | */ |
||
922 | 2 | public function prepareStoreViewCode() |
|
933 | |||
934 | /** |
||
935 | * Return's the store ID of the store with the passed store view code |
||
936 | * |
||
937 | * @param string $storeViewCode The store view code to return the store ID for |
||
938 | * |
||
939 | * @return integer The ID of the store with the passed ID |
||
940 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
941 | */ |
||
942 | 4 | public function getStoreId($storeViewCode) |
|
960 | |||
961 | /** |
||
962 | * Return's the store ID of the actual row, or of the default store |
||
963 | * if no store view code is set in the CSV file. |
||
964 | * |
||
965 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
966 | * |
||
967 | * @return integer The ID of the actual store |
||
968 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
969 | */ |
||
970 | 2 | public function getRowStoreId($default = null) |
|
981 | |||
982 | /** |
||
983 | * Return's the root category for the actual view store. |
||
984 | * |
||
985 | * @return array The store's root category |
||
986 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
987 | */ |
||
988 | 2 | public function getRootCategory() |
|
1002 | |||
1003 | /** |
||
1004 | * Return's the Magento configuration value. |
||
1005 | * |
||
1006 | * @param string $path The Magento path of the requested configuration value |
||
1007 | * @param mixed $default The default value that has to be returned, if the requested configuration value is not set |
||
1008 | * @param string $scope The scope the configuration value has been set |
||
1009 | * @param integer $scopeId The scope ID the configuration value has been set |
||
1010 | * |
||
1011 | * @return mixed The configuration value |
||
1012 | * @throws \Exception Is thrown, if nor a value can be found or a default value has been passed |
||
1013 | */ |
||
1014 | 5 | public function getCoreConfigData($path, $default = null, $scope = ScopeKeys::SCOPE_DEFAULT, $scopeId = 0) |
|
1090 | |||
1091 | /** |
||
1092 | * Resolve the original column name for the passed one. |
||
1093 | * |
||
1094 | * @param string $columnName The column name that has to be resolved |
||
1095 | * |
||
1096 | * @return string|null The original column name |
||
1097 | */ |
||
1098 | 2 | public function resolveOriginalColumnName($columnName) |
|
1120 | |||
1121 | /** |
||
1122 | * Return's the original data if available, or an empty array. |
||
1123 | * |
||
1124 | * @return array The original data |
||
1125 | */ |
||
1126 | 2 | public function getOriginalData() |
|
1141 | |||
1142 | /** |
||
1143 | * Query's whether or not the actual column contains original data like |
||
1144 | * filename, line number and column names. |
||
1145 | * |
||
1146 | * @return boolean TRUE if the actual column contains origin data, else FALSE |
||
1147 | */ |
||
1148 | 2 | public function hasOriginalData() |
|
1152 | |||
1153 | /** |
||
1154 | * Wraps the passed exeception into a new one by trying to resolve the original filname, |
||
1155 | * line number and column names and use it for a detailed exception message. |
||
1156 | * |
||
1157 | * @param array $columnNames The column names that should be resolved and wrapped |
||
1158 | * @param \Exception $parent The exception we want to wrap |
||
1159 | * @param string $className The class name of the exception type we want to wrap the parent one |
||
1160 | * |
||
1161 | * @return \Exception the wrapped exception |
||
1162 | */ |
||
1163 | 1 | public function wrapException( |
|
1207 | |||
1208 | /** |
||
1209 | * Strip's the exception suffix containing filename and line number from the |
||
1210 | * passed message. |
||
1211 | * |
||
1212 | * @param string $message The message to strip the exception suffix from |
||
1213 | * |
||
1214 | * @return mixed The message without the exception suffix |
||
1215 | */ |
||
1216 | 1 | public function stripExceptionSuffix($message) |
|
1220 | |||
1221 | /** |
||
1222 | * Append's the exception suffix containing filename and line number to the |
||
1223 | * passed message. If no message has been passed, only the suffix will be |
||
1224 | * returned |
||
1225 | * |
||
1226 | * @param string|null $message The message to append the exception suffix to |
||
1227 | * @param string|null $filename The filename used to create the suffix |
||
1228 | * @param string|null $lineNumber The line number used to create the suffx |
||
1229 | * |
||
1230 | * @return string The message with the appended exception suffix |
||
1231 | */ |
||
1232 | 11 | public function appendExceptionSuffix($message = null, $filename = null, $lineNumber = null) |
|
1253 | |||
1254 | /** |
||
1255 | * Raises the value for the counter with the passed key by one. |
||
1256 | * |
||
1257 | * @param mixed $counterName The name of the counter to raise |
||
1258 | * |
||
1259 | * @return integer The counter's new value |
||
1260 | */ |
||
1261 | 1 | public function raiseCounter($counterName) |
|
1270 | |||
1271 | /** |
||
1272 | * Merge the passed array into the status of the actual import. |
||
1273 | * |
||
1274 | * @param array $status The status information to be merged |
||
1275 | * |
||
1276 | * @return void |
||
1277 | */ |
||
1278 | 1 | public function mergeAttributesRecursive(array $status) |
|
1287 | } |
||
1288 |
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..