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 |
||
8 | class ModelSerializer |
||
9 | { |
||
10 | /** |
||
11 | * The model instance to transform. |
||
12 | * |
||
13 | * @var \Illuminate\Database\Eloquent\Model |
||
14 | */ |
||
15 | protected $record; |
||
16 | |||
17 | /** |
||
18 | * The relationships to return. |
||
19 | * |
||
20 | * @var array |
||
21 | */ |
||
22 | protected $relationships; |
||
23 | |||
24 | /** |
||
25 | * The subset of record attributes to return. |
||
26 | * |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $fields; |
||
30 | |||
31 | /** |
||
32 | * The relationships to load and include. |
||
33 | * |
||
34 | * @var array |
||
35 | */ |
||
36 | protected $include; |
||
37 | |||
38 | /** |
||
39 | * Meta information to include. |
||
40 | * |
||
41 | * @var \Illuminate\Support\Collection |
||
42 | */ |
||
43 | protected $meta; |
||
44 | |||
45 | /** |
||
46 | * Resource links to include. |
||
47 | * |
||
48 | * @var \Illuminate\Support\Collection |
||
49 | */ |
||
50 | protected $links; |
||
51 | |||
52 | /** |
||
53 | * Create a new JSON API resource serializer. |
||
54 | * |
||
55 | * @param Model $record The model instance to serialise |
||
56 | * @param array|null $fields Subset of fields to return |
||
57 | * @param array|null $include Relations to include |
||
58 | */ |
||
59 | public function __construct($record, array $fields = [], array $include = []) |
||
68 | |||
69 | /** |
||
70 | * Limit which relations can be included. |
||
71 | * |
||
72 | * @param array $include |
||
73 | */ |
||
74 | public function scopeIncludes($include) |
||
78 | |||
79 | /** |
||
80 | * Add meta information to the returned object. |
||
81 | * |
||
82 | * @param string|array $key |
||
83 | * @param string|int|null $value |
||
84 | */ |
||
85 | public function addMeta($key, $value = null) |
||
89 | |||
90 | /** |
||
91 | * Add one or more links to the returned object. |
||
92 | * |
||
93 | * @param string|array $key |
||
94 | * @param string|int|null $value |
||
95 | */ |
||
96 | public function addLinks($key, $value = null) |
||
100 | |||
101 | /** |
||
102 | * Return a JSON API resource identifier object for the primary record. |
||
103 | * |
||
104 | * @return array |
||
105 | */ |
||
106 | public function toResourceIdentifier() |
||
113 | |||
114 | /** |
||
115 | * Return a base JSON API resource object for the primary record containing |
||
116 | * only immediate attributes. |
||
117 | * |
||
118 | * @return array |
||
119 | */ |
||
120 | public function toBaseResourceObject() |
||
126 | |||
127 | /** |
||
128 | * Return a full JSON API resource object for the primary record. |
||
129 | * |
||
130 | * @return array |
||
131 | */ |
||
132 | public function toResourceObject() |
||
140 | |||
141 | /** |
||
142 | * Serialise complete JSON API document to an array. |
||
143 | * |
||
144 | * @return array |
||
145 | */ |
||
146 | public function serialiseToObject() |
||
155 | |||
156 | /** |
||
157 | * Serialise complete JSON API document to a JSON string. |
||
158 | * |
||
159 | * @return array |
||
160 | */ |
||
161 | public function serializeToJson() |
||
165 | |||
166 | /** |
||
167 | * Return the primary record type name. |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | protected function getRecordType() |
||
177 | |||
178 | /** |
||
179 | * Return the attribute object data for the primary record. |
||
180 | * |
||
181 | * @return array |
||
182 | */ |
||
183 | protected function transformRecordAttributes() |
||
194 | |||
195 | /** |
||
196 | * Return a collection of JSON API resource identifier objects by each |
||
197 | * relation on the primary record. |
||
198 | * |
||
199 | * @return \Illuminate\Support\Collection |
||
200 | */ |
||
201 | View Code Duplication | protected function transformRecordRelations() |
|
215 | |||
216 | /** |
||
217 | * Return a collection of JSON API resource objects for each included |
||
218 | * relationship. |
||
219 | * |
||
220 | * @return \Illuminate\Support\Collection |
||
221 | */ |
||
222 | View Code Duplication | protected function transformIncludedRelations() |
|
236 | |||
237 | /** |
||
238 | * Run a map over each item in a loaded relation on the primary record. |
||
239 | * |
||
240 | * @param string $relation |
||
241 | * @param \Closure $callback |
||
242 | * |
||
243 | * @return Collection|Model|null |
||
244 | */ |
||
245 | protected function mapRelation($relation, $callback) |
||
257 | } |
||
258 |
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.