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 |
||
7 | class StatusCakeSkeleton extends AbstractSkeleton |
||
8 | { |
||
9 | |||
10 | const NAME = "statuscake"; |
||
11 | |||
12 | /** |
||
13 | * @return string |
||
14 | */ |
||
15 | public function getName() |
||
19 | |||
20 | /** |
||
21 | * @param \ArrayObject $project |
||
22 | * |
||
23 | * @return mixed |
||
24 | */ |
||
25 | public function create(\ArrayObject $project) |
||
28 | |||
29 | /** |
||
30 | * @return mixed |
||
31 | */ |
||
32 | public function preMaintenance() |
||
35 | |||
36 | /** |
||
37 | * @return mixed |
||
38 | */ |
||
39 | public function postMaintenance() |
||
42 | |||
43 | /** |
||
44 | * @param \ArrayObject $project |
||
45 | * |
||
46 | * @return mixed |
||
47 | */ |
||
48 | View Code Duplication | public function maintenance(\ArrayObject $project) |
|
61 | |||
62 | private function getCheckName(\ArrayObject $project){ |
||
65 | |||
66 | private function getTestId(\ArrayObject $project) |
||
67 | { |
||
68 | $tests = $this->performAPICall("Tests/", "GET"); |
||
69 | $found = false; |
||
70 | array_walk($tests, function ($item, $key) use (&$found, $project) { |
||
71 | if ($item["WebsiteName"] == $this->getCheckName($project)) { |
||
72 | $found = $item["TestID"]; |
||
73 | } |
||
74 | }); |
||
75 | return $found; |
||
76 | } |
||
77 | |||
78 | private function createTest(\ArrayObject $project) |
||
79 | { |
||
80 | $data = $this->getTestArray($project); |
||
81 | $this->performAPICall("Tests/Update", "PUT", $data); |
||
82 | } |
||
83 | |||
84 | private function updateTest($id, \ArrayObject $project) |
||
85 | { |
||
86 | $data = $this->getTestArray($project, $id); |
||
87 | $this->performAPICall("Tests/Update", "PUT", $data); |
||
88 | } |
||
89 | |||
90 | private function deleteTest($id) |
||
91 | { |
||
92 | $data = $this->getTestArray(null, $id); |
||
93 | $this->performAPICall("Tests/Details/", "DELETE", $data); |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param \ArrayObject $project |
||
98 | * @return array |
||
99 | */ |
||
100 | private function getTestArray(\ArrayObject $project = null, $id = false) |
||
120 | |||
121 | private function performAPICall($url, $method, $data = null) |
||
122 | { |
||
123 | |||
124 | // The data to insert |
||
125 | $API = $this->app["config"]["statuscake"]["apikey"]; |
||
126 | $Username = $this->app["config"]["statuscake"]["username"]; |
||
127 | |||
128 | // Create the CURL String |
||
129 | $ch = curl_init("https://www.statuscake.com/API/" . $url); |
||
130 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
131 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method); |
||
132 | if ($data) { |
||
133 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); |
||
134 | } |
||
135 | curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
||
136 | "API: " . $API, |
||
137 | "Username: " . $Username |
||
138 | )); |
||
139 | |||
140 | $response = curl_exec($ch); |
||
141 | $response = json_decode($response, true); |
||
142 | $this->dialogProvider->logConfig("Sent a " . $method . " call to " . "https://www.statuscake.com/API/" . $url . " and got a response: " . ($method == "GET"?"with " . sizeof($response) . " checks":$response["Message"])); |
||
143 | return $response; |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param \ArrayObject $project |
||
148 | * |
||
149 | * @return mixed |
||
150 | */ |
||
151 | public function preBackup(\ArrayObject $project) |
||
154 | |||
155 | /** |
||
156 | * @param \ArrayObject $project |
||
157 | * |
||
158 | * @return mixed |
||
159 | */ |
||
160 | public function postBackup(\ArrayObject $project) |
||
163 | |||
164 | /** |
||
165 | * @param \ArrayObject $project |
||
166 | * |
||
167 | * @return mixed |
||
168 | */ |
||
169 | public function preRemove(\ArrayObject $project) |
||
172 | |||
173 | /** |
||
174 | * @param \ArrayObject $project |
||
175 | * |
||
176 | * @return mixed |
||
177 | */ |
||
178 | View Code Duplication | public function postRemove(\ArrayObject $project) |
|
179 | { |
||
180 | if (isset($this->app["config"]["statuscake"]["enabled"]) && $this->app["config"]["statuscake"]["enabled"]) { |
||
181 | $id = $this->getTestId($project["name"]); |
||
182 | $this->deleteTest($id); |
||
183 | } else { |
||
184 | $this->dialogProvider->logConfig("Statuscake is disabled"); |
||
185 | } |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * @return string[] |
||
190 | */ |
||
191 | public function dependsOn() |
||
195 | |||
196 | } |
||
197 |
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.