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 SaveRevisionJsonRepresentation 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 SaveRevisionJsonRepresentation, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | trait SaveRevisionJsonRepresentation |
||
8 | { |
||
9 | /** |
||
10 | * Build the entire data array for further json insert into the revisions table. |
||
11 | * |
||
12 | * Extract the actual model's data. |
||
13 | * Extract all of the model's direct relations data. |
||
14 | * Extract all of the model's pivoted relations data. |
||
15 | * |
||
16 | * @return array |
||
17 | * @throws \ReflectionException |
||
18 | */ |
||
19 | protected function buildRevisionData(): array |
||
35 | |||
36 | /** |
||
37 | * Get all the fields that should be revisioned from the model instance. |
||
38 | * Automatically unset primary and timestamp keys. |
||
39 | * Also count for revision fields if any are set on the model. |
||
40 | * |
||
41 | * @return array |
||
42 | */ |
||
43 | protected function buildRevisionDataFromModel(): array |
||
44 | { |
||
45 | $data = $this->wasRecentlyCreated === true ? $this->getAttributes() : $this->getRawOriginal(); |
||
46 | |||
47 | $fieldsToRevision = $this->revisionOptions->revisionFields; |
||
48 | $fieldsToNotRevision = $this->revisionOptions->revisionNotFields; |
||
49 | |||
50 | unset($data[$this->getKeyName()]); |
||
51 | |||
52 | if ($this->usesTimestamps() && ! $this->revisionOptions->revisionTimestamps) { |
||
53 | unset($data[$this->getCreatedAtColumn()]); |
||
54 | unset($data[$this->getUpdatedAtColumn()]); |
||
55 | } |
||
56 | |||
57 | if ($fieldsToRevision && is_array($fieldsToRevision) && ! empty($fieldsToRevision)) { |
||
58 | foreach ($data as $field => $value) { |
||
59 | if (! in_array($field, $fieldsToRevision)) { |
||
60 | unset($data[$field]); |
||
61 | } |
||
62 | } |
||
63 | } elseif ($fieldsToNotRevision && is_array($fieldsToNotRevision) && ! empty($fieldsToNotRevision)) { |
||
64 | foreach ($data as $field => $value) { |
||
65 | if (in_array($field, $fieldsToNotRevision)) { |
||
66 | unset($data[$field]); |
||
67 | } |
||
68 | } |
||
69 | } |
||
70 | |||
71 | return $data; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Extract revisionable data from a model's relation. |
||
76 | * Extract the type, class and related records. |
||
77 | * Store the extracted data into an array to be json inserted into the revisions table. |
||
78 | * |
||
79 | * @param string $relation |
||
80 | * @param array $attributes |
||
81 | * @return array |
||
82 | */ |
||
83 | protected function buildRevisionDataFromDirectRelation(string $relation, array $attributes = []): array |
||
109 | |||
110 | /** |
||
111 | * Extract revisionable data from a model's relation pivot table. |
||
112 | * Extract the type, class, related records and pivot values. |
||
113 | * Store the extracted data into an array to be json inserted into the revisions table. |
||
114 | * |
||
115 | * @param string $relation |
||
116 | * @param array $attributes |
||
117 | * @return array |
||
118 | */ |
||
119 | protected function buildRevisionDataFromPivotedRelation(string $relation, array $attributes = []): array |
||
164 | |||
165 | /** |
||
166 | * Verify if the data array contains the foreign keys. |
||
167 | * |
||
168 | * @param array $data |
||
169 | * @return bool |
||
170 | */ |
||
171 | protected function dataHasForeignKeys(array $data = []): bool |
||
175 | |||
176 | /** |
||
177 | * Verify if the data array contains the pivoted foreign keys. |
||
178 | * |
||
179 | * @param array $data |
||
180 | * @return bool |
||
181 | */ |
||
182 | protected function dataHasPivotForeignKeys(array $data = []): bool |
||
186 | |||
187 | /** |
||
188 | * Attach the foreign keys to the data array. |
||
189 | * |
||
190 | * @param array $data |
||
191 | * @param string $primaryKey |
||
192 | * @param string $foreignKey |
||
193 | * @return array |
||
194 | */ |
||
195 | protected function dataWithForeignKeys(array $data, string $primaryKey, string $foreignKey): array |
||
204 | |||
205 | /** |
||
206 | * Attach the pivoted foreign keys to the data array. |
||
207 | * |
||
208 | * @param array $data |
||
209 | * @param string $primaryKey |
||
210 | * @param string $foreignKey |
||
211 | * @param string $relatedKey |
||
212 | * @return array |
||
213 | */ |
||
214 | protected function dataWithPivotForeignKeys(array $data, string $primaryKey, string $foreignKey, string $relatedKey): array |
||
224 | |||
225 | /** |
||
226 | * Build the data array with each attribute<->value set for the given model. |
||
227 | * |
||
228 | * @param array $data |
||
229 | * @param array $attributes |
||
230 | * @param int $index |
||
231 | * @param string $field |
||
232 | * @param string|int|null $value |
||
233 | * @return array |
||
234 | */ |
||
235 | View Code Duplication | protected function dataWithAttributeValue(array $data, array $attributes, int $index, string $field, $value = null): array |
|
243 | |||
244 | /** |
||
245 | * Build the data array with each pivoted attribute<->value set for the given model. |
||
246 | * |
||
247 | * @param array $data |
||
248 | * @param array $attributes |
||
249 | * @param int $index |
||
250 | * @param string $field |
||
251 | * @param string|int|null $value |
||
252 | * @return array |
||
253 | */ |
||
254 | View Code Duplication | protected function dataWithPivotAttributeValue(array $data, array $attributes, int $index, string $field, $value = null): array |
|
262 | |||
263 | /** |
||
264 | * Get the relations that should be revisionable alongside the original model. |
||
265 | * |
||
266 | * @return array |
||
267 | * @throws \ReflectionException |
||
268 | */ |
||
269 | protected function getRelationsForRevision(): array |
||
281 | } |
||
282 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: