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 | 70 | 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 | 70 | 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 | 70 | 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 | 70 | 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 | 4 | 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 | 70 | public function setUp($serial) |
|
521 | |||
522 | /** |
||
523 | * Clean up the global data after importing the variants. |
||
524 | * |
||
525 | * @param string $serial The serial of the actual import |
||
526 | * |
||
527 | * @return void |
||
528 | */ |
||
529 | 1 | public function tearDown($serial) |
|
549 | |||
550 | /** |
||
551 | * Return's the target directory for the artefact export. |
||
552 | * |
||
553 | * @return string The target directory for the artefact export |
||
554 | */ |
||
555 | 1 | public function getTargetDir() |
|
559 | |||
560 | /** |
||
561 | * Return's the next source directory, which will be the target directory |
||
562 | * of this subject, in most cases. |
||
563 | * |
||
564 | * @param string $serial The serial of the actual import |
||
565 | * |
||
566 | * @return string The new source directory |
||
567 | */ |
||
568 | 5 | public function getNewSourceDir($serial) |
|
572 | |||
573 | /** |
||
574 | * Register the passed observer with the specific type. |
||
575 | * |
||
576 | * @param \TechDivision\Import\Observers\ObserverInterface $observer The observer to register |
||
577 | * @param string $type The type to register the observer with |
||
578 | * |
||
579 | * @return void |
||
580 | */ |
||
581 | 6 | public function registerObserver(ObserverInterface $observer, $type) |
|
593 | |||
594 | /** |
||
595 | * Register the passed callback with the specific type. |
||
596 | * |
||
597 | * @param \TechDivision\Import\Callbacks\CallbackInterface $callback The subject to register the callbacks for |
||
598 | * @param string $type The type to register the callback with |
||
599 | * |
||
600 | * @return void |
||
601 | */ |
||
602 | 2 | public function registerCallback(CallbackInterface $callback, $type) |
|
614 | |||
615 | /** |
||
616 | * Return's the array with callbacks for the passed type. |
||
617 | * |
||
618 | * @param string $type The type of the callbacks to return |
||
619 | * |
||
620 | * @return array The callbacks |
||
621 | */ |
||
622 | 1 | public function getCallbacksByType($type) |
|
636 | |||
637 | /** |
||
638 | * Return's the array with the available observers. |
||
639 | * |
||
640 | * @return array The observers |
||
641 | */ |
||
642 | 6 | public function getObservers() |
|
646 | |||
647 | /** |
||
648 | * Return's the array with the available callbacks. |
||
649 | * |
||
650 | * @return array The callbacks |
||
651 | */ |
||
652 | 1 | public function getCallbacks() |
|
656 | |||
657 | /** |
||
658 | * Return's the callback mappings for this subject. |
||
659 | * |
||
660 | * @return array The array with the subject's callback mappings |
||
661 | */ |
||
662 | 2 | public function getCallbackMappings() |
|
666 | |||
667 | /** |
||
668 | * Imports the content of the file with the passed filename. |
||
669 | * |
||
670 | * |
||
671 | * @param string $serial The serial of the actual import |
||
672 | * @param string $filename The filename to process |
||
673 | * |
||
674 | * @return void |
||
675 | * @throws \Exception Is thrown, if the import can't be processed |
||
676 | */ |
||
677 | 2 | public function import($serial, $filename) |
|
753 | |||
754 | /** |
||
755 | * Imports the passed row into the database. If the import failed, the exception |
||
756 | * will be catched and logged, but the import process will be continued. |
||
757 | * |
||
758 | * @param array $row The row with the data to be imported |
||
759 | * |
||
760 | * @return void |
||
761 | */ |
||
762 | 7 | public function importRow(array $row) |
|
842 | |||
843 | /** |
||
844 | * Queries whether or not that the subject needs an OK file to be processed. |
||
845 | * |
||
846 | * @return boolean TRUE if the subject needs an OK file, else FALSE |
||
847 | */ |
||
848 | 1 | public function isOkFileNeeded() |
|
852 | |||
853 | /** |
||
854 | * Return's the default store. |
||
855 | * |
||
856 | * @return array The default store |
||
857 | */ |
||
858 | public function getDefaultStore() |
||
862 | |||
863 | /** |
||
864 | * Return's the default store view code. |
||
865 | * |
||
866 | * @return array The default store view code |
||
867 | */ |
||
868 | 5 | public function getDefaultStoreViewCode() |
|
872 | |||
873 | /** |
||
874 | * Set's the store view code the create the product/attributes for. |
||
875 | * |
||
876 | * @param string $storeViewCode The store view code |
||
877 | * |
||
878 | * @return void |
||
879 | */ |
||
880 | 4 | public function setStoreViewCode($storeViewCode) |
|
884 | |||
885 | /** |
||
886 | * Return's the store view code the create the product/attributes for. |
||
887 | * |
||
888 | * @param string|null $default The default value to return, if the store view code has not been set |
||
889 | * |
||
890 | * @return string The store view code |
||
891 | */ |
||
892 | 8 | public function getStoreViewCode($default = null) |
|
909 | |||
910 | /** |
||
911 | * Prepare's the store view code in the subject. If the store_view_code row doesn't contain |
||
912 | * any value, the default code of the default store view will be set. |
||
913 | * |
||
914 | * @return void |
||
915 | */ |
||
916 | 2 | public function prepareStoreViewCode() |
|
927 | |||
928 | /** |
||
929 | * Return's the store ID of the store with the passed store view code |
||
930 | * |
||
931 | * @param string $storeViewCode The store view code to return the store ID for |
||
932 | * |
||
933 | * @return integer The ID of the store with the passed ID |
||
934 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
935 | */ |
||
936 | 4 | public function getStoreId($storeViewCode) |
|
954 | |||
955 | /** |
||
956 | * Return's the store ID of the actual row, or of the default store |
||
957 | * if no store view code is set in the CSV file. |
||
958 | * |
||
959 | * @param string|null $default The default store view code to use, if no store view code is set in the CSV file |
||
960 | * |
||
961 | * @return integer The ID of the actual store |
||
962 | * @throws \Exception Is thrown, if the store with the actual code is not available |
||
963 | */ |
||
964 | 2 | public function getRowStoreId($default = null) |
|
975 | |||
976 | /** |
||
977 | * Return's the root category for the actual view store. |
||
978 | * |
||
979 | * @return array The store's root category |
||
980 | * @throws \Exception Is thrown if the root category for the passed store code is NOT available |
||
981 | */ |
||
982 | 2 | public function getRootCategory() |
|
996 | |||
997 | /** |
||
998 | * Return's the Magento configuration value. |
||
999 | * |
||
1000 | * @param string $path The Magento path of the requested configuration value |
||
1001 | * @param mixed $default The default value that has to be returned, if the requested configuration value is not set |
||
1002 | * @param string $scope The scope the configuration value has been set |
||
1003 | * @param integer $scopeId The scope ID the configuration value has been set |
||
1004 | * |
||
1005 | * @return mixed The configuration value |
||
1006 | * @throws \Exception Is thrown, if nor a value can be found or a default value has been passed |
||
1007 | */ |
||
1008 | 5 | public function getCoreConfigData($path, $default = null, $scope = ScopeKeys::SCOPE_DEFAULT, $scopeId = 0) |
|
1084 | |||
1085 | /** |
||
1086 | * Resolve the original column name for the passed one. |
||
1087 | * |
||
1088 | * @param string $columnName The column name that has to be resolved |
||
1089 | * |
||
1090 | * @return string|null The original column name |
||
1091 | */ |
||
1092 | 2 | public function resolveOriginalColumnName($columnName) |
|
1114 | |||
1115 | /** |
||
1116 | * Return's the original data if available, or an empty array. |
||
1117 | * |
||
1118 | * @return array The original data |
||
1119 | */ |
||
1120 | 2 | public function getOriginalData() |
|
1135 | |||
1136 | /** |
||
1137 | * Query's whether or not the actual column contains original data like |
||
1138 | * filename, line number and column names. |
||
1139 | * |
||
1140 | * @return boolean TRUE if the actual column contains origin data, else FALSE |
||
1141 | */ |
||
1142 | 2 | public function hasOriginalData() |
|
1146 | |||
1147 | /** |
||
1148 | * Wraps the passed exeception into a new one by trying to resolve the original filname, |
||
1149 | * line number and column names and use it for a detailed exception message. |
||
1150 | * |
||
1151 | * @param array $columnNames The column names that should be resolved and wrapped |
||
1152 | * @param \Exception $parent The exception we want to wrap |
||
1153 | * @param string $className The class name of the exception type we want to wrap the parent one |
||
1154 | * |
||
1155 | * @return \Exception the wrapped exception |
||
1156 | */ |
||
1157 | 1 | public function wrapException( |
|
1201 | |||
1202 | /** |
||
1203 | * Strip's the exception suffix containing filename and line number from the |
||
1204 | * passed message. |
||
1205 | * |
||
1206 | * @param string $message The message to strip the exception suffix from |
||
1207 | * |
||
1208 | * @return mixed The message without the exception suffix |
||
1209 | */ |
||
1210 | 1 | public function stripExceptionSuffix($message) |
|
1214 | |||
1215 | /** |
||
1216 | * Append's the exception suffix containing filename and line number to the |
||
1217 | * passed message. If no message has been passed, only the suffix will be |
||
1218 | * returned |
||
1219 | * |
||
1220 | * @param string|null $message The message to append the exception suffix to |
||
1221 | * @param string|null $filename The filename used to create the suffix |
||
1222 | * @param string|null $lineNumber The line number used to create the suffx |
||
1223 | * |
||
1224 | * @return string The message with the appended exception suffix |
||
1225 | */ |
||
1226 | 11 | public function appendExceptionSuffix($message = null, $filename = null, $lineNumber = null) |
|
1247 | |||
1248 | /** |
||
1249 | * Raises the value for the counter with the passed key by one. |
||
1250 | * |
||
1251 | * @param mixed $counterName The name of the counter to raise |
||
1252 | * |
||
1253 | * @return integer The counter's new value |
||
1254 | */ |
||
1255 | 1 | public function raiseCounter($counterName) |
|
1264 | |||
1265 | /** |
||
1266 | * Merge the passed array into the status of the actual import. |
||
1267 | * |
||
1268 | * @param array $status The status information to be merged |
||
1269 | * |
||
1270 | * @return void |
||
1271 | */ |
||
1272 | 1 | public function mergeAttributesRecursive(array $status) |
|
1281 | } |
||
1282 |
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..