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 array */ |
||
| 41 | protected $fieldsets = []; |
||
| 42 | |||
| 43 | /** @var string */ |
||
| 44 | protected $dataType; |
||
| 45 | |||
| 46 | /** @var mixed */ |
||
| 47 | protected $data; |
||
| 48 | |||
| 49 | /** @var string */ |
||
| 50 | protected $resourceName; |
||
| 51 | |||
| 52 | /** @var array */ |
||
| 53 | protected $meta = []; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @param null|mixed $data |
||
| 57 | * @param null|callable|\League\Fractal\TransformerAbstract $transformer |
||
| 58 | * @param null|\League\Fractal\Serializer\SerializerAbstract $serializer |
||
| 59 | * |
||
| 60 | * @return \Spatie\Fractalistic\Fractal |
||
| 61 | */ |
||
| 62 | public static function create($data = null, $transformer = null, $serializer = null) |
||
| 73 | |||
| 74 | /** @param \League\Fractal\Manager $manager */ |
||
| 75 | public function __construct(Manager $manager) |
||
| 79 | |||
| 80 | /** |
||
| 81 | * Set the collection data that must be transformed. |
||
| 82 | * |
||
| 83 | * @param mixed $data |
||
| 84 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
| 85 | * @param string|null $resourceName |
||
| 86 | * |
||
| 87 | * @return $this |
||
| 88 | */ |
||
| 89 | public function collection($data, $transformer = null, $resourceName = null) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Set the item data that must be transformed. |
||
| 98 | * |
||
| 99 | * @param mixed $data |
||
| 100 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
| 101 | * @param string|null $resourceName |
||
| 102 | * |
||
| 103 | * @return $this |
||
| 104 | */ |
||
| 105 | public function item($data, $transformer = null, $resourceName = null) |
||
| 111 | |||
| 112 | /** |
||
| 113 | * Set the data that must be transformed. |
||
| 114 | * |
||
| 115 | * @param string $dataType |
||
| 116 | * @param mixed $data |
||
| 117 | * @param \League\Fractal\TransformerAbstract|callable|null $transformer |
||
| 118 | * |
||
| 119 | * @return $this |
||
| 120 | */ |
||
| 121 | public function data($dataType, $data, $transformer = null) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param mixed $data |
||
| 136 | * |
||
| 137 | * @return string |
||
| 138 | */ |
||
| 139 | protected function determineDataType($data) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Set the class or function that will perform the transform. |
||
| 154 | * |
||
| 155 | * @param \League\Fractal\TransformerAbstract|callable $transformer |
||
| 156 | * |
||
| 157 | * @return $this |
||
| 158 | */ |
||
| 159 | public function transformWith($transformer) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Set the serializer to be used. |
||
| 172 | * |
||
| 173 | * @param \League\Fractal\Serializer\SerializerAbstract $serializer |
||
| 174 | * |
||
| 175 | * @return $this |
||
| 176 | */ |
||
| 177 | public function serializeWith(SerializerAbstract $serializer) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Set a Fractal paginator for the data. |
||
| 186 | * |
||
| 187 | * @param \League\Fractal\Pagination\PaginatorInterface $paginator |
||
| 188 | * |
||
| 189 | * @return $this |
||
| 190 | */ |
||
| 191 | public function paginateWith(PaginatorInterface $paginator) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Set a Fractal cursor for the data. |
||
| 200 | * |
||
| 201 | * @param \League\Fractal\Pagination\CursorInterface $cursor |
||
| 202 | * |
||
| 203 | * @return $this |
||
| 204 | */ |
||
| 205 | public function withCursor(CursorInterface $cursor) |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Specify the includes. |
||
| 214 | * |
||
| 215 | * @param array|string $includes Array or string of resources to include. |
||
| 216 | * |
||
| 217 | * @return $this |
||
| 218 | */ |
||
| 219 | public function parseIncludes($includes) |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Specify the excludes. |
||
| 230 | * |
||
| 231 | * @param array|string $excludes Array or string of resources to exclude. |
||
| 232 | * @return $this |
||
| 233 | */ |
||
| 234 | public function parseExcludes($excludes) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Specify the fieldsets to include in the response. |
||
| 245 | * |
||
| 246 | * @param array|string $fieldsets array with key = resourceName and value = fields to include |
||
| 247 | * (array or comma separated string with field names) |
||
| 248 | * |
||
| 249 | * @return $this |
||
| 250 | */ |
||
| 251 | public function parseFieldsets($fieldsets){ |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Normalize the includes an excludes. |
||
| 265 | * |
||
| 266 | * @param array|string $includesOrExcludes |
||
| 267 | * |
||
| 268 | * @return array|string |
||
| 269 | */ |
||
| 270 | protected function normalizeIncludesOrExcludes($includesOrExcludes = '') |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Set the meta data. |
||
| 283 | * |
||
| 284 | * @param $array,... |
||
| 285 | * |
||
| 286 | * @return $this |
||
| 287 | */ |
||
| 288 | public function addMeta() |
||
| 298 | |||
| 299 | /** |
||
| 300 | * Set the resource name, to replace 'data' as the root of the collection or item. |
||
| 301 | * |
||
| 302 | * @param string $resourceName |
||
| 303 | * |
||
| 304 | * @return $this |
||
| 305 | */ |
||
| 306 | public function withResourceName($resourceName) |
||
| 312 | |||
| 313 | /** |
||
| 314 | * Upper limit to how many levels of included data are allowed. |
||
| 315 | * |
||
| 316 | * @param int $recursionLimit |
||
| 317 | * |
||
| 318 | * @return $this |
||
| 319 | */ |
||
| 320 | public function limitRecursion(int $recursionLimit) |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Perform the transformation to json. |
||
| 329 | * |
||
| 330 | * @return string |
||
| 331 | */ |
||
| 332 | public function toJson() |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Perform the transformation to array. |
||
| 339 | * |
||
| 340 | * @return array |
||
| 341 | */ |
||
| 342 | public function toArray() |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Create fractal data. |
||
| 349 | * |
||
| 350 | * @return \League\Fractal\Scope |
||
| 351 | * |
||
| 352 | * @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation |
||
| 353 | * @throws \Spatie\Fractalistic\Exceptions\NoTransformerSpecified |
||
| 354 | */ |
||
| 355 | public function createData() |
||
| 381 | |||
| 382 | /** |
||
| 383 | * Get the resource. |
||
| 384 | * |
||
| 385 | * @return \League\Fractal\Resource\ResourceInterface |
||
| 386 | * |
||
| 387 | * @throws \Spatie\Fractalistic\Exceptions\InvalidTransformation |
||
| 388 | */ |
||
| 389 | public function getResource() |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Convert the object into something JSON serializable. |
||
| 414 | */ |
||
| 415 | public function jsonSerialize() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Support for magic methods to included data. |
||
| 422 | * |
||
| 423 | * @param string $name |
||
| 424 | * @param array $arguments |
||
| 425 | * |
||
| 426 | * @return $this |
||
| 427 | */ |
||
| 428 | public function __call($name, array $arguments) |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Determine if a given string starts with a given substring. |
||
| 447 | * |
||
| 448 | * @param string $haystack |
||
| 449 | * @param string|array $needles |
||
| 450 | * @return bool |
||
| 451 | */ |
||
| 452 | protected function startsWith($haystack, $needles) |
||
| 462 | } |
||
| 463 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.