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 |
||
14 | View Code Duplication | class PolymorphicCollection implements ElementInterface |
|
|
|||
15 | { |
||
16 | /** |
||
17 | * @var array |
||
18 | */ |
||
19 | protected $resources = []; |
||
20 | |||
21 | /** |
||
22 | * Create a new collection instance. |
||
23 | * |
||
24 | * @param mixed $data |
||
25 | * @param \Tobscure\JsonApi\SerializerRegistryInterface $serializers |
||
26 | */ |
||
27 | public function __construct($data, SerializerRegistryInterface $serializers) |
||
31 | |||
32 | /** |
||
33 | * Convert an array of raw data to Resource objects. |
||
34 | * |
||
35 | * @param mixed $data |
||
36 | * @param \Tobscure\JsonApi\SerializerRegistryInterface $serializers |
||
37 | * @return \Tobscure\JsonApi\Resource[] |
||
38 | */ |
||
39 | protected function buildResources($data, SerializerRegistryInterface $serializers) |
||
53 | |||
54 | /** |
||
55 | * {@inheritdoc} |
||
56 | */ |
||
57 | public function getResources() |
||
61 | |||
62 | /** |
||
63 | * Set the resources array. |
||
64 | * |
||
65 | * @param array $resources |
||
66 | * |
||
67 | * @return void |
||
68 | */ |
||
69 | public function setResources($resources) |
||
73 | |||
74 | /** |
||
75 | * Request a relationship to be included for all resources. |
||
76 | * |
||
77 | * @param string|array $relationships |
||
78 | * |
||
79 | * @return $this |
||
80 | */ |
||
81 | public function with($relationships) |
||
89 | |||
90 | /** |
||
91 | * Request a restricted set of fields. |
||
92 | * |
||
93 | * @param array|null $fields |
||
94 | * |
||
95 | * @return $this |
||
96 | */ |
||
97 | public function fields($fields) |
||
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | public function toArray() |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | public function toIdentifier() |
||
125 | } |
||
126 |
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.