Complex classes like RepositoryTrait 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 RepositoryTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
21 | trait RepositoryTrait |
||
22 | { |
||
23 | /** |
||
24 | * Auto flush changes. |
||
25 | * |
||
26 | * @var bool |
||
27 | */ |
||
28 | protected $autoFlush = false; |
||
29 | |||
30 | /** |
||
31 | * New object factory. |
||
32 | * |
||
33 | * @var callable |
||
34 | */ |
||
35 | protected $objectFactory; |
||
36 | |||
37 | /** |
||
38 | * Get automatic manager flushing. |
||
39 | * |
||
40 | * @return bool |
||
41 | */ |
||
42 | public function isAutoFlush(): bool |
||
46 | |||
47 | /** |
||
48 | * Set automatic manager flushing. |
||
49 | * |
||
50 | * @param bool $autoFlush |
||
51 | */ |
||
52 | public function setAutoFlush(bool $autoFlush = true) |
||
56 | |||
57 | /** |
||
58 | * Manager flush. |
||
59 | */ |
||
60 | public function flush() |
||
64 | |||
65 | /** |
||
66 | * Set object factory. |
||
67 | * |
||
68 | * @param callable $objectFactory |
||
69 | */ |
||
70 | public function setObjectFactory(callable $objectFactory) |
||
74 | |||
75 | /** |
||
76 | * Find one object by a set of criteria or create a new one. |
||
77 | * |
||
78 | * @param array $criteria |
||
79 | * |
||
80 | * @throws \RuntimeException |
||
81 | * |
||
82 | * @return object |
||
83 | */ |
||
84 | public function findOneByOrGetNew(array $criteria) |
||
94 | |||
95 | /** |
||
96 | * Get a new managed object instance. |
||
97 | * |
||
98 | * @throws \RuntimeException |
||
99 | * |
||
100 | * @return object |
||
101 | */ |
||
102 | public function getNew() |
||
124 | |||
125 | /** |
||
126 | * Add objects. |
||
127 | * |
||
128 | * @param \stdClass|\stdClass[] $objects |
||
129 | * @param bool $flush |
||
130 | * |
||
131 | * @throws \InvalidArgumentException |
||
132 | */ |
||
133 | public function add($objects, bool $flush = false) |
||
153 | |||
154 | /** |
||
155 | * Remove all objects. |
||
156 | * |
||
157 | * @param bool $flush |
||
158 | */ |
||
159 | public function removeAll(bool $flush = false) |
||
171 | |||
172 | /** |
||
173 | * Remove object filtered by a set of criteria. |
||
174 | * |
||
175 | * @param array $criteria |
||
176 | * @param bool $flush |
||
177 | */ |
||
178 | public function removeBy(array $criteria, bool $flush = false) |
||
190 | |||
191 | /** |
||
192 | * Remove first object filtered by a set of criteria. |
||
193 | * |
||
194 | * @param array $criteria |
||
195 | * @param bool $flush |
||
196 | */ |
||
197 | public function removeOneBy(array $criteria, bool $flush = false) |
||
211 | |||
212 | /** |
||
213 | * Remove objects. |
||
214 | * |
||
215 | * @param object|object[]|string|int $objects |
||
216 | * @param bool $flush |
||
217 | * |
||
218 | * @throws \InvalidArgumentException |
||
219 | */ |
||
220 | public function remove($objects, bool $flush = false) |
||
246 | |||
247 | /** |
||
248 | * Get all objects count. |
||
249 | * |
||
250 | * @return int |
||
251 | */ |
||
252 | public function countAll(): int |
||
256 | |||
257 | /** |
||
258 | * Get object count filtered by a set of criteria. |
||
259 | * |
||
260 | * @param mixed $criteria |
||
261 | * |
||
262 | * @return int |
||
263 | */ |
||
264 | abstract public function countBy($criteria): int; |
||
265 | |||
266 | /** |
||
267 | * Adds support for magic finders and removers. |
||
268 | * |
||
269 | * @param string $method |
||
270 | * @param array $arguments |
||
271 | * |
||
272 | * @throws \BadMethodCallException |
||
273 | * |
||
274 | * @return mixed |
||
275 | */ |
||
276 | public function __call($method, $arguments) |
||
303 | |||
304 | /** |
||
305 | * Internal remove magic finder. |
||
306 | * |
||
307 | * @param string $method |
||
308 | * @param string $fieldName |
||
309 | * @param array $arguments |
||
310 | * |
||
311 | * @throws \BadMethodCallException |
||
312 | * |
||
313 | * @return mixed |
||
314 | */ |
||
315 | protected function callSupportedMethod(string $method, string $fieldName, array $arguments) |
||
337 | |||
338 | /** |
||
339 | * Check if the object is of the proper type. |
||
340 | * |
||
341 | * @param object $object |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | protected function canBeManaged($object): bool |
||
349 | |||
350 | /** |
||
351 | * Get object manager. |
||
352 | * |
||
353 | * @return \Doctrine\Common\Persistence\ObjectManager |
||
354 | */ |
||
355 | abstract protected function getManager(); |
||
356 | |||
357 | /** |
||
358 | * Get class metadata. |
||
359 | * |
||
360 | * @return \Doctrine\Common\Persistence\Mapping\ClassMetadata |
||
361 | */ |
||
362 | abstract protected function getClassMetadata(); |
||
363 | } |
||
364 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.