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 |
||
26 | class SuccessResponseFactory extends ResponseFactory |
||
27 | { |
||
28 | /** |
||
29 | * Generate a successful JSON response. |
||
30 | * |
||
31 | * @param mixed $data |
||
32 | * @param int $statusCode |
||
33 | * @param array $meta |
||
34 | * @return JsonResponse |
||
35 | */ |
||
36 | public function make( $data = null, $statusCode = 200, $meta = [ ] ):JsonResponse |
||
46 | |||
47 | /** |
||
48 | * Transforms the data. |
||
49 | * |
||
50 | * @param mixed $data |
||
51 | * @param Transformer|null $transformer |
||
52 | * @return ResourceInterface |
||
53 | */ |
||
54 | protected function transform( $data = null, Transformer $transformer = null ):ResourceInterface |
||
75 | |||
76 | /** |
||
77 | * Transform a transformable Eloquent model. |
||
78 | * |
||
79 | * @param Transformable $model |
||
80 | * @param Transformer|null $transformer |
||
81 | * @return FractalItem |
||
82 | */ |
||
83 | View Code Duplication | protected function transformModel( Transformable $model, Transformer $transformer = null ):FractalItem |
|
95 | |||
96 | /** |
||
97 | * Transform a collection of Eloquent models. |
||
98 | * |
||
99 | * @param Collection $collection |
||
100 | * @param Transformer|null $transformer |
||
101 | * @return FractalCollection |
||
102 | */ |
||
103 | View Code Duplication | protected function transformCollection( Collection $collection, Transformer $transformer = null ):FractalCollection |
|
116 | |||
117 | /** |
||
118 | * Transform an Eloquent builder. |
||
119 | * |
||
120 | * @param Builder $collection |
||
121 | * @param Transformer|null $transformer |
||
122 | * @return FractalCollection |
||
123 | */ |
||
124 | protected function transformBuilder( Builder $query, Transformer $transformer = null ):FractalCollection |
||
128 | |||
129 | /** |
||
130 | * Transform paginated data using Laravel's paginator. |
||
131 | * |
||
132 | * @param Paginator $paginator |
||
133 | * @param Transformer|null $transformer |
||
134 | * @return FractalCollection |
||
135 | */ |
||
136 | protected function transformPaginator( Paginator $paginator, Transformer $transformer = null ):FractalCollection |
||
143 | |||
144 | /** |
||
145 | * Transform the data using the given transformer. |
||
146 | * |
||
147 | * @param Transformable|Collection $data |
||
148 | * @param Transformer|null $transformer |
||
149 | * @param string $resourceKey |
||
150 | * @return ResourceInterface |
||
151 | */ |
||
152 | protected function transformData( $data, Transformer $transformer, string $resourceKey ):ResourceInterface |
||
160 | |||
161 | /** |
||
162 | * Serializes the data. |
||
163 | * |
||
164 | * @param ResourceInterface $resource |
||
165 | * @return array |
||
166 | */ |
||
167 | protected function serialize( ResourceInterface $resource ):array |
||
182 | |||
183 | /** |
||
184 | * Here we prepend a status code to the response data, if status code is enabled in |
||
185 | * the configuration file. |
||
186 | * |
||
187 | * @param int $statusCode |
||
188 | * @param array $data |
||
189 | * @return array |
||
190 | */ |
||
191 | protected function includeStatusCode( int $statusCode, array $data ):array |
||
201 | |||
202 | /** |
||
203 | * Resolves model class path from a collection of models. |
||
204 | * |
||
205 | * @param Collection $collection |
||
206 | * @return Transformable |
||
207 | * @throws InvalidArgumentException |
||
208 | */ |
||
209 | protected function resolveModel( Collection $collection ):Transformable |
||
225 | } |
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.