Complex classes like ApplicationDomainTrait 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 ApplicationDomainTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
25 | trait ApplicationDomainTrait { |
||
26 | |||
27 | /** |
||
28 | */ |
||
29 | protected $pool; |
||
30 | |||
31 | /** |
||
32 | * Adds ApplicationUris to Application |
||
33 | * |
||
34 | * @param mixed $id |
||
35 | * @param mixed $data |
||
36 | * @return PayloadInterface |
||
37 | */ |
||
38 | public function addApplicationUris($id, $data) { |
||
39 | // find |
||
40 | $model = $this->get($id); |
||
41 | |||
42 | if ($model === null) { |
||
43 | return new NotFound(['message' => 'Application not found.']); |
||
44 | } |
||
45 | |||
46 | // pass add to internal logic |
||
47 | try { |
||
48 | $this->doAddApplicationUris($model, $data); |
||
49 | } catch (ErrorsException $e) { |
||
|
|||
50 | return new NotValid(['errors' => $e->getErrors()]); |
||
51 | } |
||
52 | |||
53 | // save and dispatch events |
||
54 | $this->dispatch(ApplicationEvent::PRE_APPLICATION_URIS_ADD, $model, $data); |
||
55 | $this->dispatch(ApplicationEvent::PRE_SAVE, $model, $data); |
||
56 | $rows = $model->save(); |
||
57 | $this->dispatch(ApplicationEvent::POST_APPLICATION_URIS_ADD, $model, $data); |
||
58 | $this->dispatch(ApplicationEvent::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 Application with the provided data |
||
69 | * |
||
70 | * @param mixed $data |
||
71 | * @return PayloadInterface |
||
72 | */ |
||
73 | public function create($data) { |
||
74 | // hydrate |
||
75 | $serializer = Application::getSerializer(); |
||
76 | $model = $serializer->hydrate(new Application(), $data); |
||
77 | $this->hydrateRelationships($model, $data); |
||
78 | |||
79 | // dispatch pre save hooks |
||
80 | $this->dispatch(ApplicationEvent::PRE_CREATE, $model, $data); |
||
81 | $this->dispatch(ApplicationEvent::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(ApplicationEvent::POST_CREATE, $model, $data); |
||
94 | $this->dispatch(ApplicationEvent::POST_SAVE, $model, $data); |
||
95 | |||
96 | return new Created(['model' => $model]); |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * Deletes a Application 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' => 'Application not found.']); |
||
111 | } |
||
112 | |||
113 | // delete |
||
114 | $this->dispatch(ApplicationEvent::PRE_DELETE, $model); |
||
115 | $model->delete(); |
||
116 | |||
117 | if ($model->isDeleted()) { |
||
118 | $this->dispatch(ApplicationEvent::POST_DELETE, $model); |
||
119 | return new Deleted(['model' => $model]); |
||
120 | } |
||
121 | |||
122 | return new NotDeleted(['message' => 'Could not delete Application']); |
||
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 Application with the given id |
||
161 | * |
||
162 | * @param mixed $id |
||
163 | * @return PayloadInterface |
||
164 | */ |
||
165 | public function read($id) { |
||
176 | |||
177 | /** |
||
178 | * Removes ApplicationUris from Application |
||
179 | * |
||
180 | * @param mixed $id |
||
181 | * @param mixed $data |
||
182 | * @return PayloadInterface |
||
183 | */ |
||
184 | public function removeApplicationUris($id, $data) { |
||
212 | |||
213 | /** |
||
214 | * Updates a Application 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 ApplicationUris on Application |
||
261 | * |
||
262 | * @param mixed $id |
||
263 | * @param mixed $data |
||
264 | * @return PayloadInterface |
||
265 | */ |
||
266 | public function updateApplicationUris($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 Application $model |
||
324 | * @param array $data |
||
325 | */ |
||
326 | protected function dispatch($type, Application $model, array $data = []) { |
||
348 | |||
349 | /** |
||
350 | * Interal mechanism to add ApplicationUris to Application |
||
351 | * |
||
352 | * @param Application $model |
||
353 | * @param mixed $data |
||
354 | */ |
||
355 | protected function doAddApplicationUris(Application $model, $data) { |
||
370 | |||
371 | /** |
||
372 | * Interal mechanism to remove ApplicationUris from Application |
||
373 | * |
||
374 | * @param Application $model |
||
375 | * @param mixed $data |
||
376 | */ |
||
377 | protected function doRemoveApplicationUris(Application $model, $data) { |
||
392 | |||
393 | /** |
||
394 | * Internal update mechanism of ApplicationUris on Application |
||
395 | * |
||
396 | * @param Application $model |
||
397 | * @param mixed $data |
||
398 | */ |
||
399 | protected function doUpdateApplicationUris(Application $model, $data) { |
||
418 | |||
419 | /** |
||
420 | * Returns one Application with the given id from cache |
||
421 | * |
||
422 | * @param mixed $id |
||
423 | * @return Application|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.