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 AbstractMagentoConnector 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 AbstractMagentoConnector, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
22 | abstract class AbstractMagentoConnector extends AbstractConnector implements MagentoConnectorInterface |
||
23 | { |
||
24 | const LAST_SYNC_KEY = 'lastSyncItemDate'; |
||
25 | |||
26 | /** @var MagentoTransportInterface */ |
||
27 | protected $transport; |
||
28 | |||
29 | /** @var array */ |
||
30 | protected $bundleConfiguration; |
||
31 | |||
32 | /** @var ManagerRegistry */ |
||
33 | protected $managerRegistry; |
||
34 | |||
35 | /** @var string */ |
||
36 | protected $className; |
||
37 | |||
38 | /** |
||
39 | * @param ContextRegistry $contextRegistry |
||
40 | * @param LoggerStrategy $logger |
||
41 | * @param ConnectorContextMediator $contextMediator |
||
42 | * @param array $bundleConfiguration |
||
43 | */ |
||
44 | public function __construct( |
||
45 | ContextRegistry $contextRegistry, |
||
46 | LoggerStrategy $logger, |
||
47 | ConnectorContextMediator $contextMediator, |
||
48 | array $bundleConfiguration |
||
49 | ) { |
||
50 | parent::__construct($contextRegistry, $logger, $contextMediator); |
||
51 | $this->bundleConfiguration = $bundleConfiguration; |
||
52 | } |
||
53 | |||
54 | /** |
||
55 | * @param ManagerRegistry $managerRegistry |
||
56 | */ |
||
57 | public function setManagerRegistry(ManagerRegistry $managerRegistry) |
||
61 | |||
62 | /** |
||
63 | * @param string $className |
||
64 | */ |
||
65 | public function setClassName($className) |
||
69 | |||
70 | /** |
||
71 | * {@inheritdoc} |
||
72 | */ |
||
73 | public function getImportEntityFQCN() |
||
81 | |||
82 | /** |
||
83 | * {@inheritdoc} |
||
84 | */ |
||
85 | public function read() |
||
86 | { |
||
87 | $item = parent::read(); |
||
88 | |||
89 | if (null !== $item) { |
||
90 | $this->addStatusData( |
||
91 | self::LAST_SYNC_KEY, |
||
92 | $this->getMaxUpdatedDate($this->getUpdatedDate($item), $this->getStatusData(self::LAST_SYNC_KEY)) |
||
93 | ); |
||
94 | } |
||
95 | $iterator = $this->getSourceIterator(); |
||
96 | if (!$iterator->valid() && $iterator instanceof UpdatedLoaderInterface) { |
||
97 | // cover case, when no one item was synced |
||
98 | // then just take point from what it was started |
||
99 | $dateFromReadStarted = $iterator->getStartDate() ? $iterator->getStartDate()->format('Y-m-d H:i:s') : null; |
||
100 | $this->addStatusData( |
||
101 | self::LAST_SYNC_KEY, |
||
102 | $this->getMaxUpdatedDate($this->getStatusData(self::LAST_SYNC_KEY), $dateFromReadStarted) |
||
103 | ); |
||
104 | } |
||
105 | |||
106 | return $item; |
||
107 | } |
||
108 | |||
109 | /** |
||
110 | * @param ContextInterface $context |
||
111 | */ |
||
112 | protected function initializeTransport(ContextInterface $context) |
||
125 | |||
126 | /** |
||
127 | * {@inheritdoc} |
||
128 | */ |
||
129 | protected function initializeFromContext(ContextInterface $context) |
||
167 | |||
168 | /** |
||
169 | * @param Status $status |
||
170 | * |
||
171 | * @return \DateTime |
||
172 | */ |
||
173 | protected function getStartDate(Status $status = null) |
||
203 | |||
204 | /** |
||
205 | * Get last completed status for connector of integration instance. |
||
206 | * |
||
207 | * @param Integration $integration |
||
208 | * @param string $connector |
||
209 | * @return Status|null |
||
210 | */ |
||
211 | protected function getLastCompletedIntegrationStatus(Integration $integration, $connector) |
||
220 | |||
221 | /** |
||
222 | * {@inheritdoc} |
||
223 | */ |
||
224 | protected function validateConfiguration() |
||
232 | |||
233 | /** |
||
234 | * @param string|null $currDateToCompare |
||
235 | * @param string|null $prevDateToCompare |
||
236 | * |
||
237 | * @return null|string |
||
238 | */ |
||
239 | protected function getMaxUpdatedDate($currDateToCompare, $prevDateToCompare) |
||
253 | |||
254 | /** |
||
255 | * Compares maximum updated date with current date and returns the smallest. |
||
256 | * |
||
257 | * @param string $updatedDate |
||
258 | * |
||
259 | * @return string |
||
260 | */ |
||
261 | protected function getMinUpdatedDate($updatedDate) |
||
270 | |||
271 | /** |
||
272 | * @param array $item |
||
273 | * |
||
274 | * @return string|null |
||
275 | */ |
||
276 | protected function getUpdatedDate(array $item) |
||
289 | |||
290 | /** |
||
291 | * @param ContextInterface $context |
||
292 | * @param \Iterator $iterator |
||
293 | */ |
||
294 | protected function setPredefinedFilters(ContextInterface $context, \Iterator $iterator) |
||
318 | } |
||
319 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.