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 |
||
12 | class Fractal |
||
13 | { |
||
14 | /** |
||
15 | * @var \League\Fractal\Manager |
||
16 | */ |
||
17 | protected $manager; |
||
18 | |||
19 | /** |
||
20 | * @var \League\Fractal\Serializer\SerializerAbstract |
||
21 | */ |
||
22 | protected $serializer; |
||
23 | |||
24 | /** |
||
25 | * @var \League\Fractal\TransformerAbstract|Callable |
||
26 | */ |
||
27 | protected $transformer; |
||
28 | |||
29 | /** |
||
30 | * @var \League\Fractal\Pagination\PaginatorInterface |
||
31 | */ |
||
32 | protected $paginator; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $includes = []; |
||
38 | |||
39 | /** |
||
40 | * Array containing modifiers as keys and an array value of params. |
||
41 | * |
||
42 | * @var array |
||
43 | */ |
||
44 | protected $includeParams = []; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | protected $dataType; |
||
50 | |||
51 | /** |
||
52 | * @var mixed |
||
53 | */ |
||
54 | protected $data; |
||
55 | |||
56 | /** |
||
57 | * @var string |
||
58 | */ |
||
59 | protected $resourceName; |
||
60 | |||
61 | /** |
||
62 | * @var array |
||
63 | */ |
||
64 | protected $meta = []; |
||
65 | |||
66 | /** |
||
67 | * @param \League\Fractal\Manager $manager |
||
68 | */ |
||
69 | public function __construct(Manager $manager) |
||
73 | |||
74 | /** |
||
75 | * Set the collection data that must be transformed. |
||
76 | * |
||
77 | * @param mixed $data |
||
78 | * @param \League\Fractal\TransformerAbstract|Callable|null $transformer |
||
79 | * @param string|null $resourceName |
||
80 | * |
||
81 | * @return $this |
||
82 | */ |
||
83 | View Code Duplication | public function collection($data, $transformer = null, $resourceName = null) |
|
93 | |||
94 | /** |
||
95 | * Set the item data that must be transformed. |
||
96 | * |
||
97 | * @param mixed $data |
||
98 | * @param \League\Fractal\TransformerAbstract|Callable|null $transformer |
||
99 | * @param string|null $resourceName |
||
100 | * |
||
101 | * @return $this |
||
102 | */ |
||
103 | View Code Duplication | public function item($data, $transformer = null, $resourceName = null) |
|
113 | |||
114 | /** |
||
115 | * Set the data that must be transformed. |
||
116 | * |
||
117 | * @param string $dataType |
||
118 | * @param mixed $data |
||
119 | * @param \League\Fractal\TransformerAbstract|Callable|null $transformer |
||
120 | * |
||
121 | * @return $this |
||
122 | */ |
||
123 | protected function data($dataType, $data, $transformer = null) |
||
135 | |||
136 | /** |
||
137 | * Set the class or function that will perform the transform. |
||
138 | * |
||
139 | * @param \League\Fractal\TransformerAbstract|Callable $transformer |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function transformWith($transformer) |
||
149 | |||
150 | /** |
||
151 | * Set a Fractal paginator for the data. |
||
152 | * |
||
153 | * @param \League\Fractal\Pagination\PaginatorInterface $paginator |
||
154 | * |
||
155 | * @return $this |
||
156 | */ |
||
157 | public function paginateWith(PaginatorInterface $paginator) |
||
163 | |||
164 | /** |
||
165 | * Specify the includes. |
||
166 | * |
||
167 | * @param array|string $includes Array or csv string of resources to include |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function parseIncludes($includes) |
||
172 | { |
||
173 | if (!$includes) { |
||
174 | return $this; |
||
175 | } |
||
176 | |||
177 | View Code Duplication | if (is_string($includes)) { |
|
178 | $includes = array_map(function ($value) { |
||
179 | return trim($value); |
||
180 | }, explode(',', $includes)); |
||
181 | } |
||
182 | |||
183 | $this->includes = array_merge($this->includes, (array)$includes); |
||
184 | $this->manager->parseIncludes($this->includes); |
||
185 | $this->parseIncludeParams(); |
||
186 | |||
187 | return $this; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @return $this|void |
||
192 | */ |
||
193 | public function parseIncludeParams() |
||
230 | |||
231 | /** |
||
232 | * Get Include Params. |
||
233 | * |
||
234 | * @param string $include |
||
235 | * |
||
236 | * @return \League\Fractal\ParamBag|null |
||
237 | */ |
||
238 | public function getIncludeParams($include) |
||
248 | |||
249 | /** |
||
250 | * Support for magic methods to included data. |
||
251 | * |
||
252 | * @param string $name |
||
253 | * @param array $arguments |
||
254 | * |
||
255 | * @return $this |
||
256 | */ |
||
257 | public function __call($name, array $arguments) |
||
271 | |||
272 | /** |
||
273 | * Set the serializer to be used. |
||
274 | * |
||
275 | * @param \League\Fractal\Serializer\SerializerAbstract $serializer |
||
276 | * |
||
277 | * @return $this |
||
278 | */ |
||
279 | public function serializeWith(SerializerAbstract $serializer) |
||
285 | |||
286 | /** |
||
287 | * Set the meta data. |
||
288 | * @return $this |
||
289 | * @internal param $array |
||
290 | * |
||
291 | */ |
||
292 | public function addMeta() |
||
302 | |||
303 | /** |
||
304 | * Set the resource name, to replace 'data' as the root of the collection or item. |
||
305 | * |
||
306 | * @param string $resourceName |
||
307 | * |
||
308 | * @return $this |
||
309 | */ |
||
310 | public function resourceName($resourceName) |
||
316 | |||
317 | /** |
||
318 | * Perform the transformation to json. |
||
319 | * |
||
320 | * @return string |
||
321 | */ |
||
322 | public function toJson() |
||
326 | |||
327 | /** |
||
328 | * Perform the transformation to array. |
||
329 | * |
||
330 | * @return array |
||
331 | */ |
||
332 | public function toArray() |
||
336 | |||
337 | /** |
||
338 | * Perform the transformation. |
||
339 | * |
||
340 | * @param string $conversionMethod |
||
341 | * |
||
342 | * @return string|array |
||
343 | */ |
||
344 | protected function transform($conversionMethod) |
||
350 | |||
351 | /** |
||
352 | * Create fractal data. |
||
353 | */ |
||
354 | public function createData() |
||
368 | |||
369 | /** |
||
370 | * Get the resource. |
||
371 | */ |
||
372 | public function getResource() |
||
390 | } |
||
391 |
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.