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 Projects extends TransifexObject |
||
20 | { |
||
21 | /** |
||
22 | * Build the data array to send with create and update requests. |
||
23 | * |
||
24 | * @param array $options Optional additional params to send with the request |
||
25 | * |
||
26 | * @return array |
||
27 | */ |
||
28 | 7 | private function buildProjectRequest(array $options) |
|
64 | |||
65 | /** |
||
66 | * Checks that a license is an accepted value. |
||
67 | * |
||
68 | * @param string $license The license to check |
||
69 | * |
||
70 | * @throws \InvalidArgumentException |
||
71 | */ |
||
72 | 4 | private function checkLicense($license) |
|
87 | |||
88 | /** |
||
89 | * Method to create a project. |
||
90 | * |
||
91 | * @param string $name The name of the project |
||
92 | * @param string $slug The slug for the project |
||
93 | * @param string $description A description of the project |
||
94 | * @param string $sourceLanguage The source language code for the project |
||
95 | * @param array $options Optional additional params to send with the request |
||
96 | * |
||
97 | * @return \Joomla\Http\Response |
||
98 | * |
||
99 | * @throws \InvalidArgumentException |
||
100 | */ |
||
101 | 4 | public function createProject($name, $slug, $description, $sourceLanguage, array $options = []) |
|
133 | |||
134 | /** |
||
135 | * Method to delete a project. |
||
136 | * |
||
137 | * @param string $slug The slug for the resource. |
||
138 | * |
||
139 | * @return \Joomla\Http\Response |
||
140 | */ |
||
141 | 2 | public function deleteProject($slug) |
|
145 | |||
146 | /** |
||
147 | * Method to get information about a project. |
||
148 | * |
||
149 | * @param string $project The project to retrieve details for |
||
150 | * @param bool $details True to retrieve additional project details |
||
151 | * |
||
152 | * @return \Joomla\Http\Response |
||
153 | */ |
||
154 | 2 | View Code Duplication | public function getProject($project, $details = false) |
166 | |||
167 | /** |
||
168 | * Method to get a list of projects the user is part of. |
||
169 | * |
||
170 | * @return \Joomla\Http\Response |
||
171 | */ |
||
172 | 2 | public function getProjects() |
|
176 | |||
177 | /** |
||
178 | * Method to update a project. |
||
179 | * |
||
180 | * @param string $slug The slug for the project |
||
181 | * @param array $options Optional additional params to send with the request |
||
182 | * |
||
183 | * @return \Joomla\Http\Response |
||
184 | * |
||
185 | * @throws \RuntimeException |
||
186 | */ |
||
187 | 4 | public function updateProject($slug, array $options = []) |
|
206 | } |
||
207 |
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.