Complex classes like Versionable 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 Versionable, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | trait Versionable |
||
| 18 | { |
||
| 19 | static protected $versioning = true; |
||
| 20 | |||
| 21 | // You can add these properties to you versionable model |
||
| 22 | //protected $versionable = []; |
||
| 23 | //protected $dontVersionable = ['*']; |
||
| 24 | |||
| 25 | 6 | public static function bootVersionable() |
|
| 39 | |||
| 40 | 6 | private static function createVersionForModel(Model $model): void |
|
| 41 | { |
||
| 42 | 6 | if (self::$versioning && $model->shouldVersioning()) { |
|
| 43 | 6 | Version::createForModel($model); |
|
| 44 | 6 | $model->removeOldVersions($model->getKeepVersionsCount()); |
|
| 45 | } |
||
| 46 | 6 | } |
|
| 47 | |||
| 48 | /** |
||
| 49 | * @return \Illuminate\Database\Eloquent\Relations\MorphMany |
||
| 50 | */ |
||
| 51 | 6 | public function versions(): MorphMany |
|
| 55 | |||
| 56 | /** |
||
| 57 | * @return \Illuminate\Database\Eloquent\Relations\MorphOne |
||
| 58 | */ |
||
| 59 | 4 | public function lastVersion(): MorphOne |
|
| 63 | |||
| 64 | /** |
||
| 65 | * @param int $id |
||
| 66 | * |
||
| 67 | * @return \Illuminate\Database\Eloquent\Model|null |
||
| 68 | */ |
||
| 69 | 1 | public function getVersion($id) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * @param int $id |
||
| 76 | * |
||
| 77 | * @return mixed |
||
| 78 | */ |
||
| 79 | 1 | public function revertToVersion($id) |
|
| 83 | |||
| 84 | /** |
||
| 85 | * @param int $keep |
||
| 86 | */ |
||
| 87 | 6 | public function removeOldVersions(int $keep): void |
|
| 95 | |||
| 96 | 1 | public function removeAllVersions() |
|
| 100 | |||
| 101 | /** |
||
| 102 | * @return bool |
||
| 103 | */ |
||
| 104 | 6 | public function shouldVersioning(): bool |
|
| 105 | { |
||
| 106 | 6 | return !empty($this->getVersionableAttributes()); |
|
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | 6 | public function getVersionableAttributes(): array |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param array $attributes |
||
| 136 | * |
||
| 137 | * @return $this |
||
| 138 | * |
||
| 139 | * @throws \Exception |
||
| 140 | */ |
||
| 141 | public function setVersionable(array $attributes) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * @param array $attributes |
||
| 154 | * |
||
| 155 | * @return $this |
||
| 156 | * |
||
| 157 | * @throws \Exception |
||
| 158 | */ |
||
| 159 | public function setDontVersionable(array $attributes) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * @return array |
||
| 172 | */ |
||
| 173 | 6 | public function getVersionable(): array |
|
| 177 | |||
| 178 | /** |
||
| 179 | * @return array |
||
| 180 | */ |
||
| 181 | public function getDontVersionable(): array |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | 6 | public function getVersionStrategy() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * @param string $strategy |
||
| 196 | * |
||
| 197 | * @return $this |
||
| 198 | * |
||
| 199 | * @throws \Exception |
||
| 200 | */ |
||
| 201 | 1 | public function setVersionStrategy(string $strategy) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @return string |
||
| 214 | */ |
||
| 215 | 6 | public function getVersionModel(): string |
|
| 219 | |||
| 220 | /** |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | 6 | public function getKeepVersionsCount(): string |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Get the versionable attributes of a given array. |
||
| 230 | * |
||
| 231 | * @param array $attributes |
||
| 232 | * |
||
| 233 | * @return array |
||
| 234 | */ |
||
| 235 | 6 | public function versionableFromArray(array $attributes): array |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @param callable $callback |
||
| 250 | */ |
||
| 251 | 1 | public static function withoutVersion(callable $callback) |
|
| 259 | /** |
||
| 260 | * transform the contents by versionTrans |
||
| 261 | * |
||
| 262 | * @return array |
||
| 263 | */ |
||
| 264 | public function versionsTrans() |
||
| 273 | /** |
||
| 274 | * |
||
| 275 | * @param array $data |
||
| 276 | * |
||
| 277 | * @return array |
||
| 278 | */ |
||
| 279 | public function transformContent($data) |
||
| 295 | } |
||
| 296 |
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.