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 |
||
26 | trait RepositoryTrait |
||
27 | { |
||
28 | protected static $supportedMethods = ['findBy', 'findOneBy', 'findPaginatedBy', 'removeBy', 'removeOneBy']; |
||
29 | |||
30 | /** |
||
31 | * Auto flush changes. |
||
32 | * |
||
33 | * @var bool |
||
34 | */ |
||
35 | protected $autoFlush = false; |
||
36 | |||
37 | /** |
||
38 | * New object factory. |
||
39 | * |
||
40 | * @var callable |
||
41 | */ |
||
42 | protected $objectFactory; |
||
43 | |||
44 | /** |
||
45 | * Get automatic manager flushing. |
||
46 | * |
||
47 | * @return bool |
||
48 | */ |
||
49 | public function isAutoFlush(): bool |
||
53 | |||
54 | /** |
||
55 | * Set automatic manager flushing. |
||
56 | * |
||
57 | * @param bool $autoFlush |
||
58 | */ |
||
59 | public function setAutoFlush(bool $autoFlush = true) |
||
63 | |||
64 | /** |
||
65 | * Manager flush. |
||
66 | */ |
||
67 | public function flush() |
||
71 | |||
72 | /** |
||
73 | * Set object factory. |
||
74 | * |
||
75 | * @param callable $objectFactory |
||
76 | */ |
||
77 | public function setObjectFactory(callable $objectFactory) |
||
81 | |||
82 | /** |
||
83 | * Find one object by a set of criteria or create a new one. |
||
84 | * |
||
85 | * @param array $criteria |
||
86 | * @param int $lockMode |
||
87 | * @param int $lockVersion |
||
|
|||
88 | * |
||
89 | * @throws \RuntimeException |
||
90 | * |
||
91 | * @return object |
||
92 | */ |
||
93 | public function findOneByOrGetNew(array $criteria, int $lockMode = 0, int $lockVersion = null) |
||
103 | |||
104 | /** |
||
105 | * Get a new managed object instance. |
||
106 | * |
||
107 | * @throws \RuntimeException |
||
108 | * |
||
109 | * @return object |
||
110 | */ |
||
111 | public function getNew() |
||
133 | |||
134 | /** |
||
135 | * Add objects. |
||
136 | * |
||
137 | * @param object|object[]|\Traversable $objects |
||
138 | * @param bool $flush |
||
139 | * |
||
140 | * @throws \InvalidArgumentException |
||
141 | */ |
||
142 | public function add($objects, bool $flush = false) |
||
146 | |||
147 | /** |
||
148 | * Remove all objects. |
||
149 | * |
||
150 | * @param bool $flush |
||
151 | */ |
||
152 | public function removeAll(bool $flush = false) |
||
156 | |||
157 | /** |
||
158 | * Remove object filtered by a set of criteria. |
||
159 | * |
||
160 | * @param array $criteria |
||
161 | * @param bool $flush |
||
162 | */ |
||
163 | public function removeBy(array $criteria, bool $flush = false) |
||
167 | |||
168 | /** |
||
169 | * Remove first object filtered by a set of criteria. |
||
170 | * |
||
171 | * @param array $criteria |
||
172 | * @param bool $flush |
||
173 | */ |
||
174 | public function removeOneBy(array $criteria, bool $flush = false) |
||
178 | |||
179 | /** |
||
180 | * Remove objects. |
||
181 | * |
||
182 | * @param object|object[]|\Traversable|string|int $objects |
||
183 | * @param bool $flush |
||
184 | * |
||
185 | * @throws \InvalidArgumentException |
||
186 | */ |
||
187 | public function remove($objects, bool $flush = false) |
||
195 | |||
196 | /** |
||
197 | * Refresh objects. |
||
198 | * |
||
199 | * @param object|object[]|\Traversable $objects |
||
200 | * |
||
201 | * @throws \InvalidArgumentException |
||
202 | */ |
||
203 | public function refresh($objects) |
||
212 | |||
213 | /** |
||
214 | * Detach objects. |
||
215 | * |
||
216 | * @param object|object[]|\Traversable $objects |
||
217 | * |
||
218 | * @throws \InvalidArgumentException |
||
219 | */ |
||
220 | public function detach($objects) |
||
229 | |||
230 | /** |
||
231 | * Get all objects count. |
||
232 | * |
||
233 | * @return int |
||
234 | */ |
||
235 | public function countAll(): int |
||
239 | |||
240 | /** |
||
241 | * Get object count filtered by a set of criteria. |
||
242 | * |
||
243 | * @param mixed $criteria |
||
244 | * |
||
245 | * @return int |
||
246 | */ |
||
247 | abstract public function countBy($criteria): int; |
||
248 | |||
249 | /** |
||
250 | * Adds support for magic methods. |
||
251 | * |
||
252 | * @param string $method |
||
253 | * @param array $arguments |
||
254 | * |
||
255 | * @throws \BadMethodCallException |
||
256 | * |
||
257 | * @return mixed |
||
258 | */ |
||
259 | public function __call($method, $arguments) |
||
289 | |||
290 | /** |
||
291 | * Internal method call. |
||
292 | * |
||
293 | * @param string $method |
||
294 | * @param string $fieldName |
||
295 | * @param array $arguments |
||
296 | * |
||
297 | * @throws \BadMethodCallException |
||
298 | * |
||
299 | * @return mixed |
||
300 | */ |
||
301 | protected function callSupportedMethod(string $method, string $fieldName, array $arguments) |
||
323 | |||
324 | /** |
||
325 | * Run manager action. |
||
326 | * |
||
327 | * @param string $action |
||
328 | * @param object|object[]|\Traversable $objects |
||
329 | * @param bool $flush |
||
330 | * |
||
331 | * @throws \InvalidArgumentException |
||
332 | */ |
||
333 | protected function runManagerAction(string $action, $objects, bool $flush) |
||
357 | |||
358 | /** |
||
359 | * Flush managed objects. |
||
360 | * |
||
361 | * @param object|object[] $objects |
||
362 | * @param bool $flush |
||
363 | */ |
||
364 | protected function flushObjects($objects, bool $flush) |
||
370 | |||
371 | /** |
||
372 | * Check if the object is of the proper type. |
||
373 | * |
||
374 | * @param object $object |
||
375 | * |
||
376 | * @return bool |
||
377 | */ |
||
378 | protected function canBeManaged($object): bool |
||
384 | |||
385 | /** |
||
386 | * Returns the fully qualified class name of the objects managed by the repository. |
||
387 | * |
||
388 | * @return string |
||
389 | */ |
||
390 | abstract public function getClassName(): string; |
||
391 | |||
392 | /** |
||
393 | * Get object manager. |
||
394 | * |
||
395 | * @return \Doctrine\Common\Persistence\ObjectManager |
||
396 | */ |
||
397 | abstract protected function getManager(); |
||
398 | |||
399 | /** |
||
400 | * Get class metadata. |
||
401 | * |
||
402 | * @return \Doctrine\Common\Persistence\Mapping\ClassMetadata |
||
403 | */ |
||
404 | abstract protected function getClassMetadata(); |
||
405 | } |
||
406 |
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.