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 |
||
21 | class MarathonStoreJobBusinessCase extends AbstractStoreJobBusinessCase implements StoreJobBusinessCaseInterface |
||
22 | { |
||
23 | 12 | View Code Duplication | public function __construct( |
39 | |||
40 | /** |
||
41 | * @return void |
||
42 | */ |
||
43 | 7 | View Code Duplication | public function storeIndexedJobs() |
44 | { |
||
45 | 7 | $_aRemoteMissingApps = $this->oJobComparisonBusinessCase->getRemoteMissingJobs(); |
|
46 | 7 | foreach ($_aRemoteMissingApps as $_sAppId) |
|
47 | { |
||
48 | 5 | $this->addRemoteMissingApp($_sAppId); |
|
49 | } |
||
50 | |||
51 | 7 | $_aLocalMissingApps = $this->oJobComparisonBusinessCase->getLocalMissingJobs(); |
|
52 | 7 | foreach ($_aLocalMissingApps as $_sAppId) |
|
53 | { |
||
54 | 1 | $this->removeLocalMissingAppInRemote($_sAppId); |
|
55 | } |
||
56 | 7 | $_aLocalUpdates = $this->oJobComparisonBusinessCase->getLocalJobUpdates(); |
|
57 | 7 | foreach ($_aLocalUpdates as $_sAppId) |
|
58 | { |
||
59 | 1 | $this->updateAppInRemote($_sAppId); |
|
60 | } |
||
61 | 7 | } |
|
62 | |||
63 | 5 | private function addRemoteMissingApp($sAppId) |
|
64 | { |
||
65 | 5 | if ($this->oJobIndexService->isJobInIndex($sAppId)) |
|
66 | { |
||
67 | /** @var MarathonAppEntity $_oJobEntityLocal */ |
||
68 | 5 | $_oJobEntityLocal = $this->oJobRepositoryLocal->getJob($sAppId); |
|
69 | |||
70 | 5 | if (!$_oJobEntityLocal instanceof MarathonAppEntity) |
|
71 | { |
||
72 | throw new \RuntimeException('Encountered entity that is not MarathonAppEntity'); |
||
73 | } |
||
74 | |||
75 | // check if dependency is satisfied |
||
76 | 5 | if ( $_oJobEntityLocal->isDependencyJob()) |
|
77 | { |
||
78 | try { |
||
79 | 4 | $circular = $this->isDependencyCircular($_oJobEntityLocal, count($_oJobEntityLocal->dependencies)); |
|
80 | 3 | if ($circular) |
|
81 | { |
||
82 | 1 | $this->oLogger->error(sprintf( |
|
83 | 1 | 'The dependency for %s is circular. Please fix them.', $sAppId |
|
84 | )); |
||
85 | 3 | return false; |
|
86 | } |
||
87 | } |
||
88 | 1 | catch(\Exception $e) |
|
89 | { |
||
90 | 1 | $this->oLogger->error(sprintf( |
|
91 | 1 | 'Job %s cannot be added to remote : %s',$sAppId, $e->getMessage() |
|
92 | )); |
||
93 | 1 | return false; |
|
94 | } |
||
95 | |||
96 | |||
97 | 2 | foreach ($_oJobEntityLocal->dependencies as $_sDependencyKey) |
|
98 | { |
||
99 | 2 | $_bAdded = $this->addRemoteMissingApp($_sDependencyKey); |
|
100 | |||
101 | 2 | if (!$_bAdded) |
|
102 | { |
||
103 | 1 | $this->oLogger->error(sprintf( |
|
104 | 1 | 'Job "%s" is dependent on "%s" which is missing. Please add them and try again.', |
|
105 | $sAppId, |
||
106 | $_sDependencyKey |
||
107 | )); |
||
108 | 1 | $this->oJobIndexService->removeJob($_sDependencyKey); |
|
109 | 2 | return false; |
|
110 | } |
||
111 | } |
||
112 | } |
||
113 | |||
114 | 2 | if ($this->oJobRepositoryRemote->addJob($_oJobEntityLocal)) |
|
115 | { |
||
116 | 2 | $this->oJobIndexService->removeJob($_oJobEntityLocal->getKey()); |
|
117 | 2 | $this->oLogger->notice(sprintf( |
|
118 | 2 | 'Job "%s" successfully added to marathon', |
|
119 | 2 | $_oJobEntityLocal->getKey() |
|
120 | )); |
||
121 | |||
122 | 2 | return true; |
|
123 | } |
||
124 | $this->oLogger->error(sprintf( |
||
125 | 'Failed to add job "%s" to marathon', |
||
126 | $_oJobEntityLocal->getKey() |
||
127 | )); |
||
128 | } |
||
129 | 2 | return false; |
|
130 | |||
131 | } |
||
132 | |||
133 | 4 | private function hasDuplicates($arr) |
|
137 | |||
138 | 4 | private function isDependencyCircular(MarathonAppEntity $oEntity, $iImmediateChildren, &$path=[]) |
|
139 | { |
||
199 | |||
200 | |||
201 | 1 | View Code Duplication | private function removeLocalMissingAppInRemote($sAppId) |
223 | |||
224 | 1 | private function updateAppInRemote($sAppId) |
|
253 | } |
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.