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 |
||
27 | class Responder implements ResponderContract |
||
28 | { |
||
29 | /** |
||
30 | * Generate a successful JSON response. |
||
31 | * |
||
32 | * @param mixed $data |
||
33 | * @param int $statusCode |
||
34 | * @param array|null $meta |
||
35 | * @return JsonResponse |
||
36 | */ |
||
37 | public function success( $data = null, $statusCode = 200, $meta = null ):JsonResponse |
||
56 | |||
57 | /** |
||
58 | * Generate an unsuccessful JSON response. |
||
59 | * |
||
60 | * @param string $errorCode |
||
61 | * @param int $statusCode |
||
62 | * @param mixed $message |
||
63 | * @return JsonResponse |
||
64 | */ |
||
65 | public function error( string $errorCode, int $statusCode = 500, $message = null ):JsonResponse |
||
78 | |||
79 | /** |
||
80 | * Transforms the data. |
||
81 | * |
||
82 | * @param mixed $data |
||
83 | * @param Transformer|null $transformer |
||
84 | * @return ResourceInterface |
||
85 | */ |
||
86 | public function transform( $data = null, Transformer $transformer = null ):ResourceInterface |
||
102 | |||
103 | /** |
||
104 | * Serializes the data. |
||
105 | * |
||
106 | * @param ResourceInterface $resource |
||
107 | * @return array |
||
108 | */ |
||
109 | public function serialize( ResourceInterface $resource ):array |
||
124 | |||
125 | /** |
||
126 | * Transform a transformable Eloquent model. |
||
127 | * |
||
128 | * @param Transformable $model |
||
129 | * @param Transformer|null $transformer |
||
130 | * @return ResourceInterface |
||
131 | */ |
||
132 | View Code Duplication | protected function transformModel( Transformable $model, Transformer $transformer = null ):ResourceInterface |
|
144 | |||
145 | /** |
||
146 | * Transform a collection of Eloquent models. |
||
147 | * |
||
148 | * @param Collection $collection |
||
149 | * @param Transformer|null $transformer |
||
150 | * @return ResourceInterface |
||
151 | */ |
||
152 | View Code Duplication | protected function transformCollection( Collection $collection, Transformer $transformer = null ):ResourceInterface |
|
165 | |||
166 | /** |
||
167 | * Transform paginated data using Laravel's paginator. |
||
168 | * |
||
169 | * @param LengthAwarePaginator $paginator |
||
170 | * @param Transformer|null $transformer |
||
171 | * @return ResourceInterface |
||
172 | */ |
||
173 | protected function transformPaginator( LengthAwarePaginator $paginator, Transformer $transformer = null ):ResourceInterface |
||
180 | |||
181 | /** |
||
182 | * Transform the data using the given transformer. |
||
183 | * |
||
184 | * @param Transformable|Collection $data |
||
185 | * @param Transformer|null $transformer |
||
186 | * @param string $resourceKey |
||
187 | * @return ResourceInterface |
||
188 | */ |
||
189 | protected function transformData( $data, Transformer $transformer, string $resourceKey ):ResourceInterface |
||
197 | |||
198 | /** |
||
199 | * Resolves model class path from a collection of models. |
||
200 | * |
||
201 | * @param Collection $collection |
||
202 | * @return Transformable |
||
203 | * @throws InvalidArgumentException |
||
204 | */ |
||
205 | protected function resolveModel( Collection $collection ):Transformable |
||
221 | |||
222 | /** |
||
223 | * Here we prepend a status code to the response data, if status code is enabled in |
||
224 | * the configuration file. |
||
225 | * |
||
226 | * @param int $statusCode |
||
227 | * @param array $data |
||
228 | * @return array |
||
229 | */ |
||
230 | protected function includeStatusCode( int $statusCode, array $data ):array |
||
240 | |||
241 | /** |
||
242 | * Get the skeleton for an error response. |
||
243 | * |
||
244 | * @param string $errorCode |
||
245 | * @param int $statusCode |
||
246 | * @return array |
||
247 | */ |
||
248 | protected function getErrorResponse( string $errorCode, int $statusCode ):array |
||
259 | |||
260 | /** |
||
261 | * Get any error messages for the response. If no message can be found it will |
||
262 | * try to resolve a set message from the translator. |
||
263 | * |
||
264 | * @param string $errorCode |
||
265 | * @param mixed $message |
||
266 | * @return array |
||
267 | */ |
||
268 | protected function getErrorMessages( string $errorCode, $message ):array |
||
283 | } |
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.