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:
Complex classes like Fractal often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Fractal, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class Fractal implements JsonSerializable |
||
15 | { |
||
16 | /** @var \League\Fractal\Manager */ |
||
17 | protected $manager; |
||
18 | |||
19 | /** @var int */ |
||
20 | protected $recursionLimit = 10; |
||
21 | |||
22 | /** @var \League\Fractal\Serializer\SerializerAbstract */ |
||
23 | protected $serializer; |
||
24 | |||
25 | /** @var \League\Fractal\TransformerAbstract|callable */ |
||
26 | protected $transformer; |
||
27 | |||
28 | /** @var \League\Fractal\Pagination\PaginatorInterface */ |
||
29 | protected $paginator; |
||
30 | |||
31 | /** @var \League\Fractal\Pagination\CursorInterface */ |
||
32 | protected $cursor; |
||
33 | |||
34 | /** @var array */ |
||
35 | protected $includes = []; |
||
36 | |||
37 | /** @var array */ |
||
38 | protected $excludes = []; |
||
39 | |||
40 | /** @var string */ |
||
41 | protected $dataType; |
||
42 | |||
43 | /** @var mixed */ |
||
44 | protected $data; |
||
45 | |||
46 | /** @var string */ |
||
47 | protected $resourceName; |
||
48 | |||
49 | /** @var array */ |
||
50 | protected $meta = []; |
||
51 | |||
52 | /** |
||
53 | * @param null|mixed $data |
||
54 | * @param null|callable|\League\Fractal\TransformerAbstract $transformer |
||
55 | * @param null|\League\Fractal\Serializer\SerializerAbstract $serializer |
||
56 | * |
||
57 | * @return \Spatie\Fractalistic\Fractal |
||
58 | */ |
||
59 | public static function create($data = null, $transformer = null, $serializer = null) |
||
70 | |||
71 | /** @param \League\Fractal\Manager $manager */ |
||
72 | public function __construct(Manager $manager) |
||
76 | |||
77 | /** |
||
78 | * Set the collection data that must be transformed. |
||
79 | * |
||
80 | * @param mixed $data |
||
81 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
82 | * @param string|null $resourceName |
||
83 | * |
||
84 | * @return $this |
||
85 | */ |
||
86 | public function collection($data, $transformer = null, $resourceName = null) |
||
92 | |||
93 | /** |
||
94 | * Set the item data that must be transformed. |
||
95 | * |
||
96 | * @param mixed $data |
||
97 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
98 | * @param string|null $resourceName |
||
99 | * |
||
100 | * @return $this |
||
101 | */ |
||
102 | public function item($data, $transformer = null, $resourceName = null) |
||
108 | |||
109 | /** |
||
110 | * Set the data that must be transformed. |
||
111 | * |
||
112 | * @param string $dataType |
||
113 | * @param mixed $data |
||
114 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
115 | * |
||
116 | * @return $this |
||
117 | */ |
||
118 | public function data($dataType, $data, $transformer = null) |
||
130 | |||
131 | /** |
||
132 | * @param mixed $data |
||
133 | * |
||
134 | * @return string |
||
135 | */ |
||
136 | protected function determineDataType($data) |
||
148 | |||
149 | /** |
||
150 | * Set the class or function that will perform the transform. |
||
151 | * |
||
152 | * @param \League\Fractal\TransformerAbstract|callable $transformer |
||
153 | * |
||
154 | * @return $this |
||
155 | */ |
||
156 | public function transformWith($transformer) |
||
166 | |||
167 | /** |
||
168 | * Set the serializer to be used. |
||
169 | * |
||
170 | * @param \League\Fractal\Serializer\SerializerAbstract $serializer |
||
171 | * |
||
172 | * @return $this |
||
173 | */ |
||
174 | public function serializeWith(SerializerAbstract $serializer) |
||
180 | |||
181 | /** |
||
182 | * Set a Fractal paginator for the data. |
||
183 | * |
||
184 | * @param \League\Fractal\Pagination\PaginatorInterface $paginator |
||
185 | * |
||
186 | * @return $this |
||
187 | */ |
||
188 | public function paginateWith(PaginatorInterface $paginator) |
||
194 | |||
195 | /** |
||
196 | * Set a Fractal cursor for the data. |
||
197 | * |
||
198 | * @param \League\Fractal\Pagination\CursorInterface $cursor |
||
199 | * |
||
200 | * @return $this |
||
201 | */ |
||
202 | public function withCursor(CursorInterface $cursor) |
||
208 | |||
209 | /** |
||
210 | * Specify the includes. |
||
211 | * |
||
212 | * @param array|string $includes Array or string of resources to include. |
||
213 | * |
||
214 | * @return $this |
||
215 | */ |
||
216 | public function parseIncludes($includes) |
||
224 | |||
225 | /** |
||
226 | * Specify the excludes. |
||
227 | * |
||
228 | * @param array|string $excludes Array or string of resources to exclude. |
||
229 | * @return $this |
||
230 | */ |
||
231 | public function parseExcludes($excludes) |
||
239 | |||
240 | /** |
||
241 | * Normalize the includes an excludes. |
||
242 | * |
||
243 | * @param array|string $includesOrExcludes |
||
244 | * |
||
245 | * @return array|string |
||
246 | */ |
||
247 | protected function normalizeIncludesOrExcludes($includesOrExcludes = '') |
||
257 | |||
258 | /** |
||
259 | * Set the meta data. |
||
260 | * |
||
261 | * @param $array,... |
||
262 | * |
||
263 | * @return $this |
||
264 | */ |
||
265 | public function addMeta() |
||
275 | |||
276 | /** |
||
277 | * Set the resource name, to replace 'data' as the root of the collection or item. |
||
278 | * |
||
279 | * @param string $resourceName |
||
280 | * |
||
281 | * @return $this |
||
282 | */ |
||
283 | public function withResourceName($resourceName) |
||
289 | |||
290 | /** |
||
291 | * Upper limit to how many levels of included data are allowed. |
||
292 | * |
||
293 | * @param int $recursionLimit |
||
294 | * |
||
295 | * @return $this |
||
296 | */ |
||
297 | public function limitRecursion(int $recursionLimit) |
||
303 | |||
304 | /** |
||
305 | * Perform the transformation to json. |
||
306 | * |
||
307 | * @return string |
||
308 | */ |
||
309 | public function toJson() |
||
313 | |||
314 | /** |
||
315 | * Perform the transformation to array. |
||
316 | * |
||
317 | * @return array |
||
318 | */ |
||
319 | public function toArray() |
||
323 | |||
324 | /** |
||
325 | * Create fractal data. |
||
326 | * |
||
327 | * @return \League\Fractal\Scope |
||
328 | * |
||
329 | * @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation |
||
330 | * @throws \Spatie\Fractalistic\Exceptions\NoTransformerSpecified |
||
331 | */ |
||
332 | public function createData() |
||
354 | |||
355 | /** |
||
356 | * Get the resource. |
||
357 | * |
||
358 | * @return \League\Fractal\Resource\ResourceInterface |
||
359 | * |
||
360 | * @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation |
||
361 | */ |
||
362 | public function getResource() |
||
384 | |||
385 | /** |
||
386 | * Convert the object into something JSON serializable. |
||
387 | */ |
||
388 | public function jsonSerialize() |
||
392 | |||
393 | /** |
||
394 | * Support for magic methods to included data. |
||
395 | * |
||
396 | * @param string $name |
||
397 | * @param array $arguments |
||
398 | * |
||
399 | * @return $this |
||
400 | */ |
||
401 | public function __call($name, array $arguments) |
||
417 | |||
418 | /** |
||
419 | * Determine if a given string starts with a given substring. |
||
420 | * |
||
421 | * @param string $haystack |
||
422 | * @param string|array $needles |
||
423 | * @return bool |
||
424 | */ |
||
425 | protected function startsWith($haystack, $needles) |
||
435 | } |
||
436 |
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.