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 |
||
| 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 |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Verify if the data array contains the foreign keys. |
||
| 166 | * |
||
| 167 | * @param array $data |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | protected function dataHasForeignKeys(array $data = []): bool |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Verify if the data array contains the pivoted foreign keys. |
||
| 177 | * |
||
| 178 | * @param array $data |
||
| 179 | * @return bool |
||
| 180 | */ |
||
| 181 | protected function dataHasPivotForeignKeys(array $data = []): bool |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Attach the foreign keys to the data array. |
||
| 188 | * |
||
| 189 | * @param array $data |
||
| 190 | * @param string $primaryKey |
||
| 191 | * @param string $foreignKey |
||
| 192 | * @return array |
||
| 193 | */ |
||
| 194 | protected function dataWithForeignKeys(array $data, string $primaryKey, string $foreignKey): array |
||
| 203 | |||
| 204 | /** |
||
| 205 | * Attach the pivoted foreign keys to the data array. |
||
| 206 | * |
||
| 207 | * @param array $data |
||
| 208 | * @param string $primaryKey |
||
| 209 | * @param string $foreignKey |
||
| 210 | * @param string $relatedKey |
||
| 211 | * @return array |
||
| 212 | */ |
||
| 213 | protected function dataWithPivotForeignKeys(array $data, string $primaryKey, string $foreignKey, string $relatedKey): array |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Build the data array with each attribute<->value set for the given model. |
||
| 226 | * |
||
| 227 | * @param array $data |
||
| 228 | * @param array $attributes |
||
| 229 | * @param int $index |
||
| 230 | * @param string $field |
||
| 231 | * @param string|int|null $value |
||
| 232 | * @return array |
||
| 233 | */ |
||
| 234 | View Code Duplication | protected function dataWithAttributeValue(array $data, array $attributes, int $index, string $field, $value = null): array |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Build the data array with each pivoted attribute<->value set for the given model. |
||
| 245 | * |
||
| 246 | * @param array $data |
||
| 247 | * @param array $attributes |
||
| 248 | * @param int $index |
||
| 249 | * @param string $field |
||
| 250 | * @param string|int|null $value |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | View Code Duplication | protected function dataWithPivotAttributeValue(array $data, array $attributes, int $index, string $field, $value = null): array |
|
| 261 | |||
| 262 | /** |
||
| 263 | * Get the relations that should be revisionable alongside the original model. |
||
| 264 | * |
||
| 265 | * @return array |
||
| 266 | * @throws \ReflectionException |
||
| 267 | */ |
||
| 268 | protected function getRelationsForRevision(): array |
||
| 280 | } |
||
| 281 |
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: