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\Manager */ |
||
| 19 | protected $manager; |
||
| 20 | |||
| 21 | /** @var \Wandu\Database\Contracts\ConnectionInterface */ |
||
| 22 | protected $connection; |
||
| 23 | |||
| 24 | /** @var \Wandu\Database\Entity\Metadata */ |
||
| 25 | protected $meta; |
||
| 26 | |||
| 27 | /** @var \Wandu\Caster\CastManagerInterface */ |
||
| 28 | protected $caster; |
||
| 29 | |||
| 30 | /** @var \Wandu\Database\QueryBuilder */ |
||
| 31 | protected $queryBuilder; |
||
| 32 | |||
| 33 | /** @var \ReflectionClass */ |
||
| 34 | protected $reflClass; |
||
| 35 | |||
| 36 | /** @var \ReflectionProperty[] */ |
||
| 37 | protected $refProperties = []; |
||
| 38 | |||
| 39 | /** @var array */ |
||
| 40 | protected static $entityCache = []; |
||
| 41 | |||
| 42 | 16 | public function __construct(Manager $manager, Metadata $meta, CastManagerInterface $caster) |
|
| 50 | |||
| 51 | /** |
||
| 52 | * @return \Wandu\Database\Entity\Metadata |
||
| 53 | */ |
||
| 54 | public function getMeta(): Metadata |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 61 | * @param array $bindings |
||
| 62 | * @return \Generator |
||
| 63 | */ |
||
| 64 | 7 | public function fetch($query = null, array $bindings = []) |
|
| 70 | |||
| 71 | /** |
||
| 72 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 73 | * @param array $bindings |
||
| 74 | * @return \Wandu\Collection\Contracts\ListInterface |
||
| 75 | */ |
||
| 76 | 2 | public function all($query = null, array $bindings = []): ListInterface |
|
| 80 | |||
| 81 | /** |
||
| 82 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 83 | * @param array $bindings |
||
| 84 | * @return object |
||
| 85 | */ |
||
| 86 | 11 | public function first($query = null, array $bindings = []) |
|
| 90 | |||
| 91 | /** |
||
| 92 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 93 | * @param array $bindings |
||
| 94 | * @return object |
||
| 95 | 4 | * @throws \Wandu\Database\Exception\EntityNotFoundException |
|
| 96 | */ |
||
| 97 | public function firstOrFail($query = null, array $bindings = []) |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string|int $identifier |
||
| 108 | 1 | * @return object |
|
| 109 | 1 | */ |
|
| 110 | 1 | public function find($identifier) |
|
| 116 | |||
| 117 | 1 | /** |
|
| 118 | * @param string|int $identifier |
||
| 119 | 1 | * @return object |
|
| 120 | 1 | * @throws \Wandu\Database\Exception\EntityNotFoundException |
|
| 121 | 1 | */ |
|
| 122 | 1 | public function findOrFail($identifier) |
|
| 128 | |||
| 129 | 1 | /** |
|
| 130 | * @param array $identifiers |
||
| 131 | 1 | * @return \Wandu\Collection\Contracts\ListInterface |
|
| 132 | 1 | */ |
|
| 133 | 1 | public function findMany(array $identifiers = []): ListInterface |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @param object $entity |
||
| 142 | * @return int |
||
| 143 | */ |
||
| 144 | public function insert($entity) |
||
| 167 | |||
| 168 | 1 | /** |
|
| 169 | * @param object $entity |
||
| 170 | 1 | * @return int |
|
| 171 | 1 | */ |
|
| 172 | public function update($entity) |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param object $entity |
||
| 191 | * @return int |
||
| 192 | */ |
||
| 193 | public function delete($entity) |
||
| 206 | 15 | ||
| 207 | /** |
||
| 208 | 15 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
|
| 209 | 15 | * @param array $bindings |
|
| 210 | 15 | * @return int |
|
| 211 | 3 | */ |
|
| 212 | 15 | public function query($query, array $bindings = []) |
|
| 216 | |||
| 217 | 15 | /** |
|
| 218 | * @param array $attributes |
||
| 219 | * @return object |
||
| 220 | 15 | */ |
|
| 221 | 15 | public function hydrate(array $attributes = null) |
|
| 250 | |||
| 251 | /** |
||
| 252 | 10 | * @param mixed $entity |
|
| 253 | 10 | * @return string|int |
|
| 254 | */ |
||
| 255 | private function getIdentifier($entity) |
||
| 266 | 16 | ||
| 267 | 16 | /** |
|
| 268 | 16 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
|
| 269 | * @return string|\Wandu\Database\Contracts\QueryInterface |
||
| 270 | */ |
||
| 271 | private function normalizeSelectQuery($query = null) |
||
| 285 | 16 | ||
| 286 | /** |
||
| 287 | 16 | * @param \ReflectionProperty $property |
|
| 288 | 16 | * @param object $object |
|
| 289 | * @param mixed $target |
||
| 290 | 9 | */ |
|
| 291 | private function injectProperty(ReflectionProperty $property, $object, $target) |
||
| 296 | 16 | ||
| 297 | /** |
||
| 298 | 16 | * @param \ReflectionProperty $property |
|
| 299 | 16 | * @param object $object |
|
| 300 | * @return mixed |
||
| 301 | 16 | */ |
|
| 302 | private function pickProperty(ReflectionProperty $property, $object) |
||
| 307 | |||
| 308 | 2 | /** |
|
| 309 | * @param string $name |
||
| 310 | 2 | * @return \ReflectionProperty |
|
| 311 | 2 | */ |
|
| 312 | 2 | private function getPropertyReflection($name): ReflectionProperty |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @return \ReflectionClass |
||
| 322 | */ |
||
| 323 | private function getClassReflection(): ReflectionClass |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param mixed $entity |
||
| 333 | * @param string $method |
||
| 334 | */ |
||
| 335 | private function assertIsInstance($entity, $method) |
||
| 344 | } |
||
| 345 |
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: