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 |
||
13 | class Fractal implements JsonSerializable |
||
14 | { |
||
15 | /** |
||
16 | * @var \League\Fractal\Manager |
||
17 | */ |
||
18 | protected $manager; |
||
19 | |||
20 | /** |
||
21 | * @var \League\Fractal\Serializer\SerializerAbstract |
||
22 | */ |
||
23 | protected $serializer; |
||
24 | |||
25 | /** |
||
26 | * @var \League\Fractal\TransformerAbstract|callable |
||
27 | */ |
||
28 | protected $transformer; |
||
29 | |||
30 | /** |
||
31 | * @var \League\Fractal\Pagination\PaginatorInterface |
||
32 | */ |
||
33 | protected $paginator; |
||
34 | |||
35 | /** |
||
36 | * @var \League\Fractal\Pagination\CursorInterface |
||
37 | */ |
||
38 | protected $cursor; |
||
39 | |||
40 | /** |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $includes = []; |
||
44 | |||
45 | |||
46 | protected $excludes = []; |
||
47 | |||
48 | /** |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $dataType; |
||
52 | |||
53 | /** |
||
54 | * @var mixed |
||
55 | */ |
||
56 | protected $data; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $resourceName; |
||
62 | |||
63 | /** |
||
64 | * @var array |
||
65 | */ |
||
66 | protected $meta = []; |
||
67 | |||
68 | /** |
||
69 | * @param \League\Fractal\Manager $manager |
||
70 | */ |
||
71 | public function __construct(Manager $manager) |
||
75 | |||
76 | /** |
||
77 | * Set the collection data that must be transformed. |
||
78 | * |
||
79 | * @param mixed $data |
||
80 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
81 | * @param string|null $resourceName |
||
82 | * |
||
83 | * @return $this |
||
84 | */ |
||
85 | public function collection($data, $transformer = null, $resourceName = null) |
||
91 | |||
92 | /** |
||
93 | * Set the item data that must be transformed. |
||
94 | * |
||
95 | * @param mixed $data |
||
96 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
97 | * @param string|null $resourceName |
||
98 | * |
||
99 | * @return $this |
||
100 | */ |
||
101 | public function item($data, $transformer = null, $resourceName = null) |
||
107 | |||
108 | /** |
||
109 | * Set the data that must be transformed. |
||
110 | * |
||
111 | * @param string $dataType |
||
112 | * @param mixed $data |
||
113 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
114 | * |
||
115 | * @return $this |
||
116 | */ |
||
117 | protected function data($dataType, $data, $transformer = null) |
||
129 | |||
130 | /** |
||
131 | * Set the class or function that will perform the transform. |
||
132 | * |
||
133 | * @param \League\Fractal\TransformerAbstract|callable $transformer |
||
134 | * |
||
135 | * @return $this |
||
136 | */ |
||
137 | public function transformWith($transformer) |
||
143 | |||
144 | /** |
||
145 | * Set a Fractal paginator for the data. |
||
146 | * |
||
147 | * @param \League\Fractal\Pagination\PaginatorInterface $paginator |
||
148 | * |
||
149 | * @return $this |
||
150 | */ |
||
151 | public function paginateWith(PaginatorInterface $paginator) |
||
157 | |||
158 | /** |
||
159 | * Set a Fractal cursor for the data. |
||
160 | * |
||
161 | * @param \League\Fractal\Pagination\CursorInterface $cursor |
||
162 | * |
||
163 | * @return $this |
||
164 | */ |
||
165 | public function withCursor(CursorInterface $cursor) |
||
171 | |||
172 | /** |
||
173 | * Specify the includes. |
||
174 | * |
||
175 | * @param array|string $includes Array or csv string of resources to include |
||
176 | * |
||
177 | * @return $this |
||
178 | */ |
||
179 | public function parseIncludes($includes) |
||
187 | |||
188 | public function parseExcludes($excludes) |
||
196 | |||
197 | protected function normalizeExcludesOrIncludes($includesOrExcludes) |
||
207 | |||
208 | /** |
||
209 | * Support for magic methods to included data. |
||
210 | * |
||
211 | * @param string $name |
||
212 | * @param array $arguments |
||
213 | * |
||
214 | * @return $this |
||
215 | */ |
||
216 | public function __call($name, array $arguments) |
||
231 | |||
232 | /** |
||
233 | * Set the serializer to be used. |
||
234 | * |
||
235 | * @param \League\Fractal\Serializer\SerializerAbstract $serializer |
||
236 | * |
||
237 | * @return $this |
||
238 | */ |
||
239 | public function serializeWith(SerializerAbstract $serializer) |
||
245 | |||
246 | /** |
||
247 | * Set the meta data. |
||
248 | * |
||
249 | * @param $array,... |
||
250 | * |
||
251 | * @return $this |
||
252 | */ |
||
253 | public function addMeta() |
||
263 | |||
264 | /** |
||
265 | * Set the resource name, to replace 'data' as the root of the collection or item. |
||
266 | * |
||
267 | * @param string $resourceName |
||
268 | * |
||
269 | * @return $this |
||
270 | */ |
||
271 | public function resourceName($resourceName) |
||
277 | |||
278 | /** |
||
279 | * Perform the transformation to json. |
||
280 | * |
||
281 | * @return string |
||
282 | */ |
||
283 | public function toJson() |
||
287 | |||
288 | /** |
||
289 | * Perform the transformation to array. |
||
290 | * |
||
291 | * @return array |
||
292 | */ |
||
293 | public function toArray() |
||
297 | |||
298 | /** |
||
299 | * Perform the transformation. |
||
300 | * |
||
301 | * @param string $conversionMethod |
||
302 | * |
||
303 | * @return string|array |
||
304 | * |
||
305 | * @throws \Spatie\Fractal\Exceptions\InvalidTransformation |
||
306 | * @throws \Spatie\Fractal\Exceptions\NoTransformerSpecified |
||
307 | */ |
||
308 | protected function transform($conversionMethod) |
||
314 | |||
315 | /** |
||
316 | * Create fractal data. |
||
317 | * |
||
318 | * @return \League\Fractal\Scope |
||
319 | * |
||
320 | * @throws \Spatie\Fractal\Exceptions\InvalidTransformation |
||
321 | * @throws \Spatie\Fractal\Exceptions\NoTransformerSpecified |
||
322 | */ |
||
323 | public function createData() |
||
343 | |||
344 | /** |
||
345 | * Get the resource. |
||
346 | * |
||
347 | * @return \League\Fractal\Resource\ResourceInterface |
||
348 | * |
||
349 | * @throws \Spatie\Fractal\Exceptions\InvalidTransformation |
||
350 | */ |
||
351 | public function getResource() |
||
373 | |||
374 | /** |
||
375 | * Convert the object into something JSON serializable. |
||
376 | * |
||
377 | * @return array |
||
378 | */ |
||
379 | public function jsonSerialize() |
||
383 | } |
||
384 |
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.