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 object|object[] $objects |
||
| 129 | * @param bool $flush |
||
| 130 | * |
||
| 131 | * @throws \InvalidArgumentException |
||
| 132 | */ |
||
| 133 | public function add($objects, bool $flush = false) |
||
| 151 | |||
| 152 | /** |
||
| 153 | * Remove all objects. |
||
| 154 | * |
||
| 155 | * @param bool $flush |
||
| 156 | */ |
||
| 157 | public function removeAll(bool $flush = false) |
||
| 169 | |||
| 170 | /** |
||
| 171 | * Remove object filtered by a set of criteria. |
||
| 172 | * |
||
| 173 | * @param array $criteria |
||
| 174 | * @param bool $flush |
||
| 175 | */ |
||
| 176 | public function removeBy(array $criteria, bool $flush = false) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * Remove first object filtered by a set of criteria. |
||
| 191 | * |
||
| 192 | * @param array $criteria |
||
| 193 | * @param bool $flush |
||
| 194 | */ |
||
| 195 | public function removeOneBy(array $criteria, bool $flush = false) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Remove objects. |
||
| 208 | * |
||
| 209 | * @param object|object[]|string|int $objects |
||
| 210 | * @param bool $flush |
||
| 211 | * |
||
| 212 | * @throws \InvalidArgumentException |
||
| 213 | */ |
||
| 214 | public function remove($objects, bool $flush = false) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * Get all objects count. |
||
| 241 | * |
||
| 242 | * @return int |
||
| 243 | */ |
||
| 244 | public function countAll(): int |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Get object count filtered by a set of criteria. |
||
| 251 | * |
||
| 252 | * @param mixed $criteria |
||
| 253 | * |
||
| 254 | * @return int |
||
| 255 | */ |
||
| 256 | abstract public function countBy($criteria): int; |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Adds support for magic finders and removers. |
||
| 260 | * |
||
| 261 | * @param string $method |
||
| 262 | * @param array $arguments |
||
| 263 | * |
||
| 264 | * @throws \BadMethodCallException |
||
| 265 | * |
||
| 266 | * @return mixed |
||
| 267 | */ |
||
| 268 | public function __call($method, $arguments) |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Internal remove magic finder. |
||
| 298 | * |
||
| 299 | * @param string $method |
||
| 300 | * @param string $fieldName |
||
| 301 | * @param array $arguments |
||
| 302 | * |
||
| 303 | * @throws \BadMethodCallException |
||
| 304 | * |
||
| 305 | * @return mixed |
||
| 306 | */ |
||
| 307 | protected function callSupportedMethod(string $method, string $fieldName, array $arguments) |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Check if the object is of the proper type. |
||
| 332 | * |
||
| 333 | * @param object $object |
||
| 334 | * |
||
| 335 | * @return bool |
||
| 336 | */ |
||
| 337 | protected function canBeManaged($object): bool |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Flush managed object. |
||
| 346 | * |
||
| 347 | * @param object|array $object |
||
| 348 | * @param bool $flush |
||
| 349 | * |
||
| 350 | * @throws \InvalidArgumentException |
||
| 351 | */ |
||
| 352 | protected function flushObject($objects, bool $flush) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Get object manager. |
||
| 370 | * |
||
| 371 | * @return \Doctrine\Common\Persistence\ObjectManager |
||
| 372 | */ |
||
| 373 | abstract protected function getManager(); |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Get class metadata. |
||
| 377 | * |
||
| 378 | * @return \Doctrine\Common\Persistence\Mapping\ClassMetadata |
||
| 379 | */ |
||
| 380 | abstract protected function getClassMetadata(); |
||
| 381 | } |
||
| 382 |
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.