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:
1 | <?php |
||
19 | class MarathonStoreJobBusinessCase extends AbstractStoreJobBusinessCase implements StoreJobBusinessCaseInterface |
||
20 | { |
||
21 | /** |
||
22 | * MarathonStoreJobBusinessCase constructor. |
||
23 | * @param JobIndexServiceInterface $jobIndexService |
||
24 | * @param JobRepositoryInterface $jobRepositoryRemote |
||
25 | * @param JobRepositoryInterface $jobRepositoryLocal |
||
26 | * @param JobComparisonInterface $jobComparisonBusinessCase |
||
27 | * @param LoggerInterface $logger |
||
28 | */ |
||
29 | 12 | View Code Duplication | public function __construct( |
42 | |||
43 | /** |
||
44 | * @return void |
||
45 | */ |
||
46 | 7 | View Code Duplication | public function storeIndexedJobs() |
62 | |||
63 | /** |
||
64 | * @param string $appId |
||
65 | * @return bool |
||
66 | */ |
||
67 | 5 | private function addRemoteMissingApp($appId) |
|
68 | { |
||
69 | 5 | if ($this->jobIndexService->isJobInIndex($appId)) { |
|
70 | /** @var MarathonAppEntity $jobEntityLocal */ |
||
71 | 5 | $jobEntityLocal = $this->jobRepositoryLocal->getJob($appId); |
|
72 | |||
73 | 5 | if (!$jobEntityLocal instanceof MarathonAppEntity) { |
|
74 | throw new \RuntimeException('Encountered entity that is not MarathonAppEntity'); |
||
75 | } |
||
76 | |||
77 | // check if dependency is satisfied |
||
78 | 5 | if ($jobEntityLocal->isDependencyJob()) { |
|
79 | try { |
||
80 | 4 | $circular = $this->isDependencyCircular($jobEntityLocal, count($jobEntityLocal->dependencies)); |
|
81 | 3 | if ($circular) { |
|
82 | 1 | $this->logger->error(sprintf( |
|
83 | 1 | 'The dependency for %s is circular. Please fix them.', |
|
84 | 1 | $appId |
|
85 | )); |
||
86 | 3 | return false; |
|
87 | } |
||
88 | 1 | } catch (\Exception $exception) { |
|
89 | 1 | $this->logger->error(sprintf( |
|
90 | 1 | 'Job %s cannot be added to remote : %s', |
|
91 | 1 | $appId, |
|
92 | 1 | $exception->getMessage() |
|
93 | )); |
||
94 | 1 | return false; |
|
95 | } |
||
96 | |||
97 | |||
98 | 2 | foreach ($jobEntityLocal->dependencies as $dependencyKey) { |
|
99 | 2 | $wasAdded = $this->addRemoteMissingApp($dependencyKey); |
|
100 | |||
101 | 2 | if (!$wasAdded) { |
|
102 | 1 | $this->logger->error(sprintf( |
|
103 | 1 | 'Job "%s" is dependent on "%s" which is missing. Please add them and try again.', |
|
104 | 1 | $appId, |
|
105 | 1 | $dependencyKey |
|
106 | )); |
||
107 | 1 | $this->jobIndexService->removeJob($dependencyKey); |
|
108 | 2 | return false; |
|
109 | } |
||
110 | } |
||
111 | } |
||
112 | |||
113 | 2 | if ($this->jobRepositoryRemote->addJob($jobEntityLocal)) { |
|
114 | 2 | $this->jobIndexService->removeJob($jobEntityLocal->getKey()); |
|
115 | 2 | $this->logger->notice(sprintf( |
|
116 | 2 | 'Job "%s" successfully added to marathon', |
|
117 | 2 | $jobEntityLocal->getKey() |
|
118 | )); |
||
119 | |||
120 | 2 | return true; |
|
121 | } |
||
122 | $this->logger->error(sprintf( |
||
123 | 'Failed to add job "%s" to marathon', |
||
124 | $jobEntityLocal->getKey() |
||
125 | )); |
||
126 | } |
||
127 | 2 | return false; |
|
128 | } |
||
129 | |||
130 | /** |
||
131 | * @param array $array |
||
132 | * @return bool |
||
133 | */ |
||
134 | 4 | private function hasDuplicates($array) |
|
138 | |||
139 | /** |
||
140 | * @param MarathonAppEntity $entity |
||
141 | * @param int $immediateChildren |
||
142 | * @param array $path |
||
143 | * @return bool |
||
144 | * @throws \Exception |
||
145 | */ |
||
146 | 4 | private function isDependencyCircular(MarathonAppEntity $entity, $immediateChildren, &$path = []) |
|
202 | |||
203 | /** |
||
204 | * @param string $appId |
||
205 | * @return bool |
||
206 | */ |
||
207 | 1 | View Code Duplication | private function removeLocalMissingAppInRemote($appId) |
226 | |||
227 | /** |
||
228 | * @param string $appId |
||
229 | * @return bool |
||
230 | */ |
||
231 | 1 | private function updateAppInRemote($appId) |
|
256 | } |
||
257 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.