Complex classes like ActivityObjectDomainTrait 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 ActivityObjectDomainTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | trait ActivityObjectDomainTrait { |
||
26 | |||
27 | /** |
||
28 | */ |
||
29 | protected $pool; |
||
30 | |||
31 | /** |
||
32 | * Adds Activities to ActivityObject |
||
33 | * |
||
34 | * @param mixed $id |
||
35 | * @param mixed $data |
||
36 | * @return PayloadInterface |
||
37 | */ |
||
38 | public function addActivities($id, $data) { |
||
39 | // find |
||
40 | $model = $this->get($id); |
||
41 | |||
42 | if ($model === null) { |
||
43 | return new NotFound(['message' => 'ActivityObject not found.']); |
||
44 | } |
||
45 | |||
46 | // pass add to internal logic |
||
47 | try { |
||
48 | $this->doAddActivities($model, $data); |
||
49 | } catch (ErrorsException $e) { |
||
|
|||
50 | return new NotValid(['errors' => $e->getErrors()]); |
||
51 | } |
||
52 | |||
53 | // save and dispatch events |
||
54 | $this->dispatch(ActivityObjectEvent::PRE_ACTIVITIES_ADD, $model, $data); |
||
55 | $this->dispatch(ActivityObjectEvent::PRE_SAVE, $model, $data); |
||
56 | $rows = $model->save(); |
||
57 | $this->dispatch(ActivityObjectEvent::POST_ACTIVITIES_ADD, $model, $data); |
||
58 | $this->dispatch(ActivityObjectEvent::POST_SAVE, $model, $data); |
||
59 | |||
60 | if ($rows > 0) { |
||
61 | return Updated(['model' => $model]); |
||
62 | } |
||
63 | |||
64 | return NotUpdated(['model' => $model]); |
||
65 | } |
||
66 | |||
67 | /** |
||
68 | * Creates a new ActivityObject with the provided data |
||
69 | * |
||
70 | * @param mixed $data |
||
71 | * @return PayloadInterface |
||
72 | */ |
||
73 | public function create($data) { |
||
74 | // hydrate |
||
75 | $serializer = ActivityObject::getSerializer(); |
||
76 | $model = $serializer->hydrate(new ActivityObject(), $data); |
||
77 | $this->hydrateRelationships($model, $data); |
||
78 | |||
79 | // dispatch pre save hooks |
||
80 | $this->dispatch(ActivityObjectEvent::PRE_CREATE, $model, $data); |
||
81 | $this->dispatch(ActivityObjectEvent::PRE_SAVE, $model, $data); |
||
82 | |||
83 | // validate |
||
84 | $validator = $this->getValidator(); |
||
85 | if ($validator !== null && !$validator->validate($model)) { |
||
86 | return new NotValid([ |
||
87 | 'errors' => $validator->getValidationFailures() |
||
88 | ]); |
||
89 | } |
||
90 | |||
91 | // save and dispatch post save hooks |
||
92 | $model->save(); |
||
93 | $this->dispatch(ActivityObjectEvent::POST_CREATE, $model, $data); |
||
94 | $this->dispatch(ActivityObjectEvent::POST_SAVE, $model, $data); |
||
95 | |||
96 | return new Created(['model' => $model]); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Deletes a ActivityObject with the given id |
||
101 | * |
||
102 | * @param mixed $id |
||
103 | * @return PayloadInterface |
||
104 | */ |
||
105 | public function delete($id) { |
||
106 | // find |
||
107 | $model = $this->get($id); |
||
108 | |||
109 | if ($model === null) { |
||
110 | return new NotFound(['message' => 'ActivityObject not found.']); |
||
111 | } |
||
112 | |||
113 | // delete |
||
114 | $this->dispatch(ActivityObjectEvent::PRE_DELETE, $model); |
||
115 | $model->delete(); |
||
116 | |||
117 | if ($model->isDeleted()) { |
||
118 | $this->dispatch(ActivityObjectEvent::POST_DELETE, $model); |
||
119 | return new Deleted(['model' => $model]); |
||
120 | } |
||
121 | |||
122 | return new NotDeleted(['message' => 'Could not delete ActivityObject']); |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Returns a paginated result |
||
127 | * |
||
128 | * @param Parameters $params |
||
129 | * @return PayloadInterface |
||
130 | */ |
||
131 | public function paginate(Parameters $params) { |
||
158 | |||
159 | /** |
||
160 | * Returns one ActivityObject with the given id |
||
161 | * |
||
162 | * @param mixed $id |
||
163 | * @return PayloadInterface |
||
164 | */ |
||
165 | public function read($id) { |
||
176 | |||
177 | /** |
||
178 | * Removes Activities from ActivityObject |
||
179 | * |
||
180 | * @param mixed $id |
||
181 | * @param mixed $data |
||
182 | * @return PayloadInterface |
||
183 | */ |
||
184 | public function removeActivities($id, $data) { |
||
212 | |||
213 | /** |
||
214 | * Updates a ActivityObject with the given idand the provided data |
||
215 | * |
||
216 | * @param mixed $id |
||
217 | * @param mixed $data |
||
218 | * @return PayloadInterface |
||
219 | */ |
||
220 | public function update($id, $data) { |
||
258 | |||
259 | /** |
||
260 | * Updates Activities on ActivityObject |
||
261 | * |
||
262 | * @param mixed $id |
||
263 | * @param mixed $data |
||
264 | * @return PayloadInterface |
||
265 | */ |
||
266 | public function updateActivities($id, $data) { |
||
294 | |||
295 | /** |
||
296 | * @param mixed $query |
||
297 | * @param mixed $filter |
||
298 | * @return void |
||
299 | */ |
||
300 | protected function applyFilter($query, $filter) { |
||
320 | |||
321 | /** |
||
322 | * @param string $type |
||
323 | * @param ActivityObject $model |
||
324 | * @param array $data |
||
325 | */ |
||
326 | protected function dispatch($type, ActivityObject $model, array $data = []) { |
||
348 | |||
349 | /** |
||
350 | * Interal mechanism to add Activities to ActivityObject |
||
351 | * |
||
352 | * @param ActivityObject $model |
||
353 | * @param mixed $data |
||
354 | */ |
||
355 | protected function doAddActivities(ActivityObject $model, $data) { |
||
370 | |||
371 | /** |
||
372 | * Interal mechanism to remove Activities from ActivityObject |
||
373 | * |
||
374 | * @param ActivityObject $model |
||
375 | * @param mixed $data |
||
376 | */ |
||
377 | protected function doRemoveActivities(ActivityObject $model, $data) { |
||
392 | |||
393 | /** |
||
394 | * Internal update mechanism of Activities on ActivityObject |
||
395 | * |
||
396 | * @param ActivityObject $model |
||
397 | * @param mixed $data |
||
398 | */ |
||
399 | protected function doUpdateActivities(ActivityObject $model, $data) { |
||
418 | |||
419 | /** |
||
420 | * Returns one ActivityObject with the given id from cache |
||
421 | * |
||
422 | * @param mixed $id |
||
423 | * @return ActivityObject|null |
||
424 | */ |
||
425 | protected function get($id) { |
||
437 | |||
438 | /** |
||
439 | * Returns the service container |
||
440 | * |
||
441 | * @return ServiceContainer |
||
442 | */ |
||
443 | abstract protected function getServiceContainer(); |
||
444 | } |
||
445 |
Scrutinizer analyzes your
composer.json
/composer.lock
file if available to determine the classes, and functions that are defined by your dependencies.It seems like the listed class was neither found in your dependencies, nor was it found in the analyzed files in your repository. If you are using some other form of dependency management, you might want to disable this analysis.