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 |
||
20 | class Album implements IMusicServiceEndpoint |
||
21 | { |
||
22 | const DATA_SOURCE = 'spotify'; |
||
23 | |||
24 | /** |
||
25 | * @var SpotifyService |
||
26 | */ |
||
27 | protected $parent; |
||
28 | |||
29 | 39 | public function __construct(SpotifyService $service) |
|
33 | |||
34 | /** |
||
35 | * @param $apiService |
||
36 | * |
||
37 | * @return mixed |
||
38 | */ |
||
39 | public function setParent($apiService) |
||
45 | |||
46 | /** |
||
47 | * Transform single item to model |
||
48 | * |
||
49 | * @param $raw |
||
50 | * |
||
51 | * @return BaseModel |
||
52 | */ |
||
53 | 2 | public function transformSingle($raw) |
|
66 | |||
67 | /** |
||
68 | * @param $raw |
||
69 | * |
||
70 | * @return ArrayCollection |
||
71 | * @throws \Exception |
||
72 | */ |
||
73 | 2 | View Code Duplication | public function transformCollection($raw) |
86 | |||
87 | /** |
||
88 | * Transform to models |
||
89 | * |
||
90 | * @param $raw |
||
91 | * |
||
92 | * @return ArrayCollection |
||
93 | */ |
||
94 | 2 | public function transform($raw) |
|
98 | |||
99 | /** |
||
100 | * @return mixed |
||
101 | */ |
||
102 | 2 | public function getParent() |
|
106 | |||
107 | /** |
||
108 | * @param $arguments |
||
109 | * |
||
110 | * @return void |
||
111 | * |
||
112 | * @throws MethodNotImplementedException |
||
113 | */ |
||
114 | public function get($arguments) |
||
119 | |||
120 | /** |
||
121 | * @param $arguments |
||
122 | * |
||
123 | * @return void |
||
124 | * |
||
125 | * @throws MethodNotImplementedException |
||
126 | */ |
||
127 | public function getComplete($arguments) |
||
132 | |||
133 | /** |
||
134 | * @param $id |
||
135 | * |
||
136 | * @return array|object |
||
137 | */ |
||
138 | public function getById($id) |
||
142 | |||
143 | /** |
||
144 | * @param $name |
||
145 | * |
||
146 | * @return mixed |
||
147 | */ |
||
148 | 2 | public function getByName($name) |
|
152 | |||
153 | /** |
||
154 | * @param $guid |
||
155 | * |
||
156 | * @return array|object |
||
157 | */ |
||
158 | public function getByGuid($guid) |
||
162 | } |
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.