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 |
||
5 | View Code Duplication | class Location extends AbstractModel |
|
|
|||
6 | { |
||
7 | /** |
||
8 | * Get all location residents without running API call. Useful if you want to perform additional filtering. |
||
9 | * @param bool $removeDuplicates |
||
10 | * @return \Zmaglica\RickAndMortyApiWrapper\Api\AbstractApi |
||
11 | */ |
||
12 | public function residents($removeDuplicates = false) |
||
18 | |||
19 | /** |
||
20 | * Get all location residents |
||
21 | * @param bool $removeDuplicates |
||
22 | * @return mixed |
||
23 | */ |
||
24 | public function getResidents($removeDuplicates = false) |
||
28 | |||
29 | /** |
||
30 | * This helper function will extract all URL ids by key. Key can be location, residence or episode |
||
31 | * @param $key |
||
32 | * @param $removeDuplicates |
||
33 | * @return array |
||
34 | */ |
||
35 | private function extractIdsFromResponseBasedOnKey($key, $removeDuplicates) |
||
61 | } |
||
62 |
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.