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 |
||
18 | class AchievementApi |
||
19 | { |
||
20 | /** |
||
21 | * @var AuthenticationModel |
||
22 | */ |
||
23 | private $authenticationModel; |
||
24 | |||
25 | /** |
||
26 | * @var RequestService |
||
27 | */ |
||
28 | private $requestService; |
||
29 | |||
30 | /** |
||
31 | * AchievementApi constructor. |
||
32 | * |
||
33 | * @param AuthenticationModel $authenticationModel |
||
34 | * @param RequestService $requestService |
||
35 | */ |
||
36 | public function __construct( |
||
43 | |||
44 | /** |
||
45 | * This provides data about an individual achievement |
||
46 | * |
||
47 | * @param int $id |
||
48 | * |
||
49 | * @return AchievementValueObject |
||
50 | */ |
||
51 | public function getAchievement($id) |
||
72 | |||
73 | /** |
||
74 | * @param string $field |
||
75 | * |
||
76 | * @return RequestModel |
||
77 | */ |
||
78 | private function prepareRequestModel($field) |
||
90 | |||
91 | /** |
||
92 | * @param string $field |
||
93 | * |
||
94 | * @return \StdClass |
||
95 | */ |
||
96 | View Code Duplication | private function prepareResponseObject($field) |
|
104 | } |
||
105 |
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.