Complex classes like ActivityDomainTrait 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 ActivityDomainTrait, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | trait ActivityDomainTrait { |
||
| 24 | |||
| 25 | /** |
||
| 26 | */ |
||
| 27 | protected $pool; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Creates a new Activity with the provided data |
||
| 31 | * |
||
| 32 | * @param mixed $data |
||
| 33 | * @return PayloadInterface |
||
| 34 | */ |
||
| 35 | public function create($data) { |
||
| 36 | // hydrate |
||
| 37 | $serializer = Activity::getSerializer(); |
||
| 38 | $model = $serializer->hydrate(new Activity(), $data); |
||
| 39 | $this->hydrateRelationships($model, $data); |
||
|
|
|||
| 40 | |||
| 41 | // dispatch pre save hooks |
||
| 42 | $this->dispatch(ActivityEvent::PRE_CREATE, $model, $data); |
||
| 43 | $this->dispatch(ActivityEvent::PRE_SAVE, $model, $data); |
||
| 44 | |||
| 45 | // validate |
||
| 46 | $validator = $this->getValidator(); |
||
| 47 | if ($validator !== null && !$validator->validate($model)) { |
||
| 48 | return new NotValid([ |
||
| 49 | 'errors' => $validator->getValidationFailures() |
||
| 50 | ]); |
||
| 51 | } |
||
| 52 | |||
| 53 | // save and dispatch post save hooks |
||
| 54 | $model->save(); |
||
| 55 | $this->dispatch(ActivityEvent::POST_CREATE, $model, $data); |
||
| 56 | $this->dispatch(ActivityEvent::POST_SAVE, $model, $data); |
||
| 57 | |||
| 58 | return new Created(['model' => $model]); |
||
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Deletes a Activity with the given id |
||
| 63 | * |
||
| 64 | * @param mixed $id |
||
| 65 | * @return PayloadInterface |
||
| 66 | */ |
||
| 67 | public function delete($id) { |
||
| 68 | // find |
||
| 69 | $model = $this->get($id); |
||
| 70 | |||
| 71 | if ($model === null) { |
||
| 72 | return new NotFound(['message' => 'Activity not found.']); |
||
| 73 | } |
||
| 74 | |||
| 75 | // delete |
||
| 76 | $this->dispatch(ActivityEvent::PRE_DELETE, $model); |
||
| 77 | $model->delete(); |
||
| 78 | |||
| 79 | if ($model->isDeleted()) { |
||
| 80 | $this->dispatch(ActivityEvent::POST_DELETE, $model); |
||
| 81 | return new Deleted(['model' => $model]); |
||
| 82 | } |
||
| 83 | |||
| 84 | return new NotDeleted(['message' => 'Could not delete Activity']); |
||
| 85 | } |
||
| 86 | |||
| 87 | /** |
||
| 88 | * Returns a paginated result |
||
| 89 | * |
||
| 90 | * @param Parameters $params |
||
| 91 | * @return PayloadInterface |
||
| 92 | */ |
||
| 93 | public function paginate(Parameters $params) { |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Returns one Activity with the given id |
||
| 123 | * |
||
| 124 | * @param mixed $id |
||
| 125 | * @return PayloadInterface |
||
| 126 | */ |
||
| 127 | public function read($id) { |
||
| 138 | |||
| 139 | /** |
||
| 140 | * Sets the Actor id |
||
| 141 | * |
||
| 142 | * @param mixed $id |
||
| 143 | * @param mixed $relatedId |
||
| 144 | * @return PayloadInterface |
||
| 145 | */ |
||
| 146 | public function setActorId($id, $relatedId) { |
||
| 167 | |||
| 168 | /** |
||
| 169 | * Sets the Object id |
||
| 170 | * |
||
| 171 | * @param mixed $id |
||
| 172 | * @param mixed $relatedId |
||
| 173 | * @return PayloadInterface |
||
| 174 | */ |
||
| 175 | public function setObjectId($id, $relatedId) { |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Sets the Target id |
||
| 199 | * |
||
| 200 | * @param mixed $id |
||
| 201 | * @param mixed $relatedId |
||
| 202 | * @return PayloadInterface |
||
| 203 | */ |
||
| 204 | public function setTargetId($id, $relatedId) { |
||
| 225 | |||
| 226 | /** |
||
| 227 | * Updates a Activity with the given idand the provided data |
||
| 228 | * |
||
| 229 | * @param mixed $id |
||
| 230 | * @param mixed $data |
||
| 231 | * @return PayloadInterface |
||
| 232 | */ |
||
| 233 | public function update($id, $data) { |
||
| 271 | |||
| 272 | /** |
||
| 273 | * @param mixed $query |
||
| 274 | * @param mixed $filter |
||
| 275 | * @return void |
||
| 276 | */ |
||
| 277 | protected function applyFilter($query, $filter) { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param string $type |
||
| 300 | * @param Activity $model |
||
| 301 | * @param array $data |
||
| 302 | */ |
||
| 303 | protected function dispatch($type, Activity $model, array $data = []) { |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Internal mechanism to set the Actor id |
||
| 328 | * |
||
| 329 | * @param Activity $model |
||
| 330 | * @param mixed $relatedId |
||
| 331 | */ |
||
| 332 | protected function doSetActorId(Activity $model, $relatedId) { |
||
| 341 | |||
| 342 | /** |
||
| 343 | * Internal mechanism to set the Object id |
||
| 344 | * |
||
| 345 | * @param Activity $model |
||
| 346 | * @param mixed $relatedId |
||
| 347 | */ |
||
| 348 | protected function doSetObjectId(Activity $model, $relatedId) { |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Internal mechanism to set the Target id |
||
| 360 | * |
||
| 361 | * @param Activity $model |
||
| 362 | * @param mixed $relatedId |
||
| 363 | */ |
||
| 364 | protected function doSetTargetId(Activity $model, $relatedId) { |
||
| 373 | |||
| 374 | /** |
||
| 375 | * Returns one Activity with the given id from cache |
||
| 376 | * |
||
| 377 | * @param mixed $id |
||
| 378 | * @return Activity|null |
||
| 379 | */ |
||
| 380 | protected function get($id) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Returns the service container |
||
| 395 | * |
||
| 396 | * @return ServiceContainer |
||
| 397 | */ |
||
| 398 | abstract protected function getServiceContainer(); |
||
| 399 | } |
||
| 400 |
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.