Complex classes like Repository 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 Repository, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class Repository |
||
| 17 | { |
||
| 18 | /** @var \Wandu\Database\Contracts\Connection */ |
||
| 19 | protected $connection; |
||
| 20 | |||
| 21 | /** @var \Wandu\Database\Entity\Metadata */ |
||
| 22 | protected $meta; |
||
| 23 | |||
| 24 | /** @var \Wandu\Caster\CastManagerInterface */ |
||
| 25 | protected $caster; |
||
| 26 | |||
| 27 | /** @var \Wandu\Database\QueryBuilder */ |
||
| 28 | protected $queryBuilder; |
||
| 29 | |||
| 30 | /** @var \ReflectionClass */ |
||
| 31 | protected $reflClass; |
||
| 32 | |||
| 33 | /** @var \ReflectionProperty[] */ |
||
| 34 | protected $refProperties = []; |
||
| 35 | |||
| 36 | /** @var array */ |
||
| 37 | protected static $entityCache = []; |
||
| 38 | |||
| 39 | 12 | public function __construct(Connection $connection, QueryBuilder $queryBuilder, Metadata $meta, Configuration $config) |
|
| 46 | |||
| 47 | /** |
||
| 48 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 49 | * @param array $bindings |
||
| 50 | * @return \Generator |
||
| 51 | */ |
||
| 52 | 5 | public function fetch($query = null, array $bindings = []) |
|
| 58 | |||
| 59 | /** |
||
| 60 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 61 | * @param array $bindings |
||
| 62 | * @return \Wandu\Collection\Contracts\ListInterface |
||
| 63 | */ |
||
| 64 | public function all($query = null, array $bindings = []): ListInterface |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 71 | * @param array $bindings |
||
| 72 | * @return ?object |
||
| 73 | */ |
||
| 74 | 7 | public function first($query = null, array $bindings = []) |
|
| 81 | |||
| 82 | /** |
||
| 83 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 84 | * @param array $bindings |
||
| 85 | * @return object |
||
| 86 | * @throws \Wandu\Database\Exception\EntityNotFoundException |
||
| 87 | */ |
||
| 88 | public function firstOrFail($query = null, array $bindings = []) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param string|int $identifier |
||
| 98 | * @return object |
||
| 99 | */ |
||
| 100 | 1 | public function find($identifier) |
|
| 106 | |||
| 107 | /** |
||
| 108 | * @param string|int $identifier |
||
| 109 | * @return object |
||
| 110 | * @throws \Wandu\Database\Exception\EntityNotFoundException |
||
| 111 | */ |
||
| 112 | public function findOrFail($identifier) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param array $identifiers |
||
| 121 | * @return \Wandu\Collection\Contracts\ListInterface |
||
| 122 | */ |
||
| 123 | public function findMany(array $identifiers = []): ListInterface |
||
| 129 | |||
| 130 | /** |
||
| 131 | * @param object $entity |
||
| 132 | * @return int |
||
| 133 | */ |
||
| 134 | 2 | public function persist($entity) |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @param object $entity |
||
| 146 | * @return int |
||
| 147 | */ |
||
| 148 | 1 | protected function executeInsert($entity) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * @param object $entity |
||
| 173 | * @return int |
||
| 174 | */ |
||
| 175 | 1 | protected function executeUpdate($entity) |
|
| 190 | |||
| 191 | /** |
||
| 192 | * @param object $entity |
||
| 193 | * @return int |
||
| 194 | */ |
||
| 195 | 1 | public function delete($entity) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 214 | * @param array $bindings |
||
| 215 | * @return int |
||
| 216 | */ |
||
| 217 | 2 | protected function query($query, array $bindings = []) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * @param array $attributes |
||
| 231 | * @return object |
||
| 232 | */ |
||
| 233 | 11 | public function hydrate(array $attributes = null) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @param mixed $entity |
||
| 265 | * @return string|int |
||
| 266 | */ |
||
| 267 | 2 | private function getIdentifier($entity) |
|
| 275 | |||
| 276 | 11 | private function normalizeSelectQuery($query = null, array $bindings = []) |
|
| 294 | |||
| 295 | /** |
||
| 296 | * @param \ReflectionProperty $property |
||
| 297 | * @param object $object |
||
| 298 | * @param mixed $target |
||
| 299 | */ |
||
| 300 | 12 | private function injectProperty(ReflectionProperty $property, $object, $target) |
|
| 305 | |||
| 306 | /** |
||
| 307 | * @param \ReflectionProperty $property |
||
| 308 | * @param object $object |
||
| 309 | * @return mixed |
||
| 310 | */ |
||
| 311 | 2 | private function pickProperty(ReflectionProperty $property, $object) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * @param string $name |
||
| 319 | * @return \ReflectionProperty |
||
| 320 | */ |
||
| 321 | 12 | private function getPropertyReflection($name): ReflectionProperty |
|
| 328 | |||
| 329 | /** |
||
| 330 | * @return \ReflectionClass |
||
| 331 | */ |
||
| 332 | 12 | private function getClassReflection(): ReflectionClass |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @param mixed $entity |
||
| 342 | * @param string $method |
||
| 343 | */ |
||
| 344 | 2 | private function assertIsInstance($entity, $method) |
|
| 353 | } |
||
| 354 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: