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 HasDuplicates 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 HasDuplicates, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | trait HasDuplicates |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * The container for all the options necessary for this trait. |
||
| 17 | * Options can be viewed in the Neurony\Duplicate\Options\DuplicateOptions file. |
||
| 18 | * |
||
| 19 | * @var DuplicateOptions |
||
| 20 | */ |
||
| 21 | protected $duplicateOptions; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Set the options for the HasDuplicates trait. |
||
| 25 | * |
||
| 26 | * @return DuplicateOptions |
||
| 27 | */ |
||
| 28 | abstract public function getDuplicateOptions(): DuplicateOptions; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Register a duplicating model event with the dispatcher. |
||
| 32 | * |
||
| 33 | * @param Closure|string $callback |
||
| 34 | * @return void |
||
| 35 | */ |
||
| 36 | public static function duplicating($callback): void |
||
| 40 | |||
| 41 | /** |
||
| 42 | * Register a duplicated model event with the dispatcher. |
||
| 43 | * |
||
| 44 | * @param Closure|string $callback |
||
| 45 | * @return void |
||
| 46 | */ |
||
| 47 | public static function duplicated($callback): void |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Duplicate a model instance and it's relations. |
||
| 54 | * |
||
| 55 | * @return Model|bool |
||
| 56 | * @throws Exception |
||
| 57 | */ |
||
| 58 | public function saveAsDuplicate() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Get a replicated instance of the original model's instance. |
||
| 97 | * |
||
| 98 | * @return Model |
||
| 99 | * @throws Exception |
||
| 100 | */ |
||
| 101 | protected function duplicateModel(): Model |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Duplicate a direct relation. |
||
| 113 | * Subsequently save new relation records for the initial model instance. |
||
| 114 | * |
||
| 115 | * @param Model $model |
||
| 116 | * @param string $relation |
||
| 117 | * @return Model |
||
| 118 | * @throws Exception |
||
| 119 | */ |
||
| 120 | protected function duplicateDirectRelation(Model $model, string $relation): Model |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Duplicate a pivoted relation. |
||
| 134 | * Subsequently attach new pivot records corresponding to the relation for the initial model instance. |
||
| 135 | * |
||
| 136 | * @param Model $model |
||
| 137 | * @param string $relation |
||
| 138 | * @return Model |
||
| 139 | * @throws Exception |
||
| 140 | */ |
||
| 141 | protected function duplicatePivotedRelation(Model $model, string $relation): Model |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Get the relations that should be duplicated alongside the original model. |
||
| 154 | * |
||
| 155 | * @return array |
||
| 156 | * @throws \ReflectionException |
||
| 157 | */ |
||
| 158 | protected function getRelationsForDuplication(): array |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Replicate a model instance, excluding attributes provided in the model's getDuplicateOptions() method. |
||
| 174 | * |
||
| 175 | * @return Model |
||
| 176 | */ |
||
| 177 | private function duplicateModelWithExcluding(): Model |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Update a model instance. |
||
| 198 | * With unique values for the attributes provided in the model's getDuplicateOptions() method. |
||
| 199 | * |
||
| 200 | * @param Model $model |
||
| 201 | * @return Model |
||
| 202 | */ |
||
| 203 | private function duplicateModelWithUnique(Model $model): Model |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Replicate a model relation instance, excluding attributes provided in the model's getDuplicateOptions() method. |
||
| 227 | * |
||
| 228 | * @param Model $model |
||
| 229 | * @param string $relation |
||
| 230 | * @return Model |
||
| 231 | */ |
||
| 232 | private function duplicateRelationWithExcluding(Model $model, string $relation): Model |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Update a relation for the model instance. |
||
| 248 | * With unique values for the attributes attributes provided in the model's getDuplicateOptions() method. |
||
| 249 | * |
||
| 250 | * @param Model $model |
||
| 251 | * @param string $relation |
||
| 252 | * @return Model |
||
| 253 | */ |
||
| 254 | private function duplicateRelationWithUnique(Model $model, string $relation): Model |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get additional pivot attributes that should be saved when duplicating a pivoted relation. |
||
| 280 | * Usually, these are attributes coming from the withPivot() method defined on the relation. |
||
| 281 | * |
||
| 282 | * @param Model $model |
||
| 283 | * @return array |
||
| 284 | */ |
||
| 285 | protected function establishDuplicatablePivotAttributes(Model $model): array |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Instantiate the duplicate options. |
||
| 300 | * |
||
| 301 | * @return void |
||
| 302 | */ |
||
| 303 | protected function initDuplicateOptions(): void |
||
| 309 | } |
||
| 310 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.