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 |
||
16 | class Parameters |
||
17 | { |
||
18 | /** |
||
19 | * @var array |
||
20 | */ |
||
21 | protected $input; |
||
22 | |||
23 | /** |
||
24 | * @param array $input |
||
25 | */ |
||
26 | 42 | public function __construct(array $input) |
|
30 | |||
31 | /** |
||
32 | * Get the includes. |
||
33 | * |
||
34 | * @param array $available |
||
35 | * |
||
36 | * @throws \Tobscure\JsonApi\Exception\InvalidParameterException |
||
37 | * |
||
38 | * @return array |
||
39 | */ |
||
40 | 9 | public function getInclude(array $available = []) |
|
61 | |||
62 | /** |
||
63 | * Get number of offset. |
||
64 | * |
||
65 | * @param int|null $perPage |
||
66 | * |
||
67 | * @throws \Tobscure\JsonApi\Exception\InvalidParameterException |
||
68 | * |
||
69 | * @return int |
||
70 | */ |
||
71 | 9 | public function getOffset($perPage = null) |
|
85 | |||
86 | /** |
||
87 | * Calculate the offset based on the page[number] parameter. |
||
88 | * |
||
89 | * @param int $perPage |
||
90 | * |
||
91 | * @throws \Tobscure\JsonApi\Exception\InvalidParameterException |
||
92 | * |
||
93 | * @return int |
||
94 | */ |
||
95 | 3 | protected function getOffsetFromNumber($perPage) |
|
105 | |||
106 | /** |
||
107 | * Get the limit. |
||
108 | * |
||
109 | * @param int|null $max |
||
110 | * |
||
111 | * @return int|null |
||
112 | */ |
||
113 | 6 | public function getLimit($max = null) |
|
123 | |||
124 | /** |
||
125 | * Get the sort. |
||
126 | * |
||
127 | * @param array $available |
||
128 | * |
||
129 | * @throws \Tobscure\JsonApi\Exception\InvalidParameterException |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | 12 | public function getSort(array $available = []) |
|
165 | |||
166 | /** |
||
167 | * Get the fields requested for inclusion. |
||
168 | * |
||
169 | * @return array |
||
170 | */ |
||
171 | 6 | public function getFields() |
|
183 | |||
184 | /** |
||
185 | * Get a filter item. |
||
186 | * |
||
187 | * @return mixed |
||
188 | */ |
||
189 | public function getFilter() |
||
193 | |||
194 | /** |
||
195 | * Get an input item. |
||
196 | * |
||
197 | * @param string $key |
||
198 | * @param null $default |
||
199 | * |
||
200 | * @return mixed |
||
201 | */ |
||
202 | 42 | protected function getInput($key, $default = null) |
|
206 | |||
207 | /** |
||
208 | * Get the page. |
||
209 | * |
||
210 | * @param string $key |
||
211 | * |
||
212 | * @return string |
||
213 | */ |
||
214 | 15 | protected function getPage($key) |
|
220 | } |
||
221 |
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.