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 | /** @var \League\Fractal\Manager */ |
||
16 | protected $manager; |
||
17 | |||
18 | /** @var \League\Fractal\Serializer\SerializerAbstract */ |
||
19 | protected $serializer; |
||
20 | |||
21 | /** @var \League\Fractal\TransformerAbstract|callable */ |
||
22 | protected $transformer; |
||
23 | |||
24 | /** @var \League\Fractal\Pagination\PaginatorInterface */ |
||
25 | protected $paginator; |
||
26 | |||
27 | /** @var \League\Fractal\Pagination\CursorInterface */ |
||
28 | protected $cursor; |
||
29 | |||
30 | /** @var array */ |
||
31 | protected $includes = []; |
||
32 | |||
33 | /** @var array */ |
||
34 | protected $excludes = []; |
||
35 | |||
36 | /** @var string */ |
||
37 | protected $dataType; |
||
38 | |||
39 | /** @var mixed */ |
||
40 | protected $data; |
||
41 | |||
42 | /** @var string */ |
||
43 | protected $resourceName; |
||
44 | |||
45 | /** @var array */ |
||
46 | protected $meta = []; |
||
47 | |||
48 | /** |
||
49 | * @param null|mixed $data |
||
|
|||
50 | * @param null|callable|\League\Fractal\TransformerAbstract $transformer |
||
51 | * @param null|\League\Fractal\Serializer\SerializerAbstract $serializer |
||
52 | * |
||
53 | * @return \Spatie\Fractalistic\Fractal |
||
54 | */ |
||
55 | public static function create() |
||
61 | |||
62 | /** @param \League\Fractal\Manager $manager */ |
||
63 | public function __construct(Manager $manager) |
||
67 | |||
68 | /** |
||
69 | * Set the collection data that must be transformed. |
||
70 | * |
||
71 | * @param mixed $data |
||
72 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
73 | * @param string|null $resourceName |
||
74 | * |
||
75 | * @return $this |
||
76 | */ |
||
77 | public function collection($data, $transformer = null, $resourceName = null) |
||
83 | |||
84 | /** |
||
85 | * Set the item data that must be transformed. |
||
86 | * |
||
87 | * @param mixed $data |
||
88 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
89 | * @param string|null $resourceName |
||
90 | * |
||
91 | * @return $this |
||
92 | */ |
||
93 | public function item($data, $transformer = null, $resourceName = null) |
||
99 | |||
100 | /** |
||
101 | * Set the data that must be transformed. |
||
102 | * |
||
103 | * @param string $dataType |
||
104 | * @param mixed $data |
||
105 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
106 | * |
||
107 | * @return $this |
||
108 | */ |
||
109 | public function data($dataType, $data, $transformer = null) |
||
121 | |||
122 | /** |
||
123 | * Set the class or function that will perform the transform. |
||
124 | * |
||
125 | * @param \League\Fractal\TransformerAbstract|callable $transformer |
||
126 | * |
||
127 | * @return $this |
||
128 | */ |
||
129 | public function transformWith($transformer) |
||
135 | |||
136 | /** |
||
137 | * Set the serializer to be used. |
||
138 | * |
||
139 | * @param \League\Fractal\Serializer\SerializerAbstract $serializer |
||
140 | * |
||
141 | * @return $this |
||
142 | */ |
||
143 | public function serializeWith(SerializerAbstract $serializer) |
||
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 | * Set a Fractal cursor for the data. |
||
166 | * |
||
167 | * @param \League\Fractal\Pagination\CursorInterface $cursor |
||
168 | * |
||
169 | * @return $this |
||
170 | */ |
||
171 | public function withCursor(CursorInterface $cursor) |
||
177 | |||
178 | /** |
||
179 | * Specify the includes. |
||
180 | * |
||
181 | * @param array|string $includes Array or string of resources to include. |
||
182 | * |
||
183 | * @return $this |
||
184 | */ |
||
185 | public function parseIncludes($includes) |
||
193 | |||
194 | /** |
||
195 | * Specify the excludes. |
||
196 | * |
||
197 | * @param array|string $excludes Array or string of resources to exclude. |
||
198 | * @return $this |
||
199 | */ |
||
200 | public function parseExcludes($excludes) |
||
208 | |||
209 | /** |
||
210 | * Normalize the includes an excludes. |
||
211 | * |
||
212 | * @param array|string $includesOrExcludes |
||
213 | * |
||
214 | * @return array|string |
||
215 | */ |
||
216 | protected function normalizeIncludesOrExcludes($includesOrExcludes = '') |
||
226 | |||
227 | /** |
||
228 | * Set the meta data. |
||
229 | * |
||
230 | * @param $array,... |
||
231 | * |
||
232 | * @return $this |
||
233 | */ |
||
234 | public function addMeta() |
||
244 | |||
245 | /** |
||
246 | * Set the resource name, to replace 'data' as the root of the collection or item. |
||
247 | * |
||
248 | * @param string $resourceName |
||
249 | * |
||
250 | * @return $this |
||
251 | */ |
||
252 | public function withResourceName($resourceName) |
||
258 | |||
259 | /** |
||
260 | * Perform the transformation to json. |
||
261 | * |
||
262 | * @return string |
||
263 | */ |
||
264 | public function toJson() |
||
268 | |||
269 | /** |
||
270 | * Perform the transformation to array. |
||
271 | * |
||
272 | * @return array |
||
273 | */ |
||
274 | public function toArray() |
||
278 | |||
279 | /** |
||
280 | * Create fractal data. |
||
281 | * |
||
282 | * @return \League\Fractal\Scope |
||
283 | * |
||
284 | * @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation |
||
285 | * @throws \Spatie\Fractalistic\Exceptions\NoTransformerSpecified |
||
286 | */ |
||
287 | public function createData() |
||
307 | |||
308 | /** |
||
309 | * Get the resource. |
||
310 | * |
||
311 | * @return \League\Fractal\Resource\ResourceInterface |
||
312 | * |
||
313 | * @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation |
||
314 | */ |
||
315 | public function getResource() |
||
337 | |||
338 | /** |
||
339 | * Convert the object into something JSON serializable. |
||
340 | */ |
||
341 | public function jsonSerialize() |
||
345 | |||
346 | /** |
||
347 | * Support for magic methods to included data. |
||
348 | * |
||
349 | * @param string $name |
||
350 | * @param array $arguments |
||
351 | * |
||
352 | * @return $this |
||
353 | */ |
||
354 | public function __call($name, array $arguments) |
||
370 | |||
371 | /** |
||
372 | * Determine if a given string starts with a given substring. |
||
373 | * |
||
374 | * @param string $haystack |
||
375 | * @param string|array $needles |
||
376 | * @return bool |
||
377 | */ |
||
378 | protected function startsWith($haystack, $needles) |
||
388 | } |
||
389 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.