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  | 
            ||
| 14 | class Repository  | 
            ||
| 15 | { | 
            ||
| 16 | /** @var \Wandu\Database\Manager */  | 
            ||
| 17 | protected $manager;  | 
            ||
| 18 | |||
| 19 | /** @var \Wandu\Database\Contracts\ConnectionInterface */  | 
            ||
| 20 | protected $connection;  | 
            ||
| 21 | |||
| 22 | /** @var \Wandu\Database\Entity\Metadata */  | 
            ||
| 23 | protected $meta;  | 
            ||
| 24 | |||
| 25 | /** @var \Wandu\Caster\CastManagerInterface */  | 
            ||
| 26 | protected $caster;  | 
            ||
| 27 | |||
| 28 | /** @var \Wandu\Database\QueryBuilder */  | 
            ||
| 29 | protected $queryBuilder;  | 
            ||
| 30 | |||
| 31 | /** @var \ReflectionClass */  | 
            ||
| 32 | protected $reflClass;  | 
            ||
| 33 | |||
| 34 | /** @var \ReflectionProperty[] */  | 
            ||
| 35 | protected $refProperties = [];  | 
            ||
| 36 | |||
| 37 | /** @var array */  | 
            ||
| 38 | protected static $entityCache = [];  | 
            ||
| 39 | |||
| 40 | 17 | public function __construct(Manager $manager, Metadata $meta, CastManagerInterface $caster)  | 
            |
| 48 | |||
| 49 | /**  | 
            ||
| 50 | * @return \Wandu\Database\Entity\Metadata  | 
            ||
| 51 | */  | 
            ||
| 52 | public function getMeta(): Metadata  | 
            ||
| 56 | |||
| 57 | /**  | 
            ||
| 58 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query  | 
            ||
| 59 | * @param array $bindings  | 
            ||
| 60 | * @return \Generator  | 
            ||
| 61 | */  | 
            ||
| 62 | 7 | public function fetch($query = null, array $bindings = [])  | 
            |
| 68 | |||
| 69 | /**  | 
            ||
| 70 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query  | 
            ||
| 71 | * @param array $bindings  | 
            ||
| 72 | * @return \Wandu\Collection\Contracts\ListInterface  | 
            ||
| 73 | */  | 
            ||
| 74 | 2 | public function all($query = null, array $bindings = []): ListInterface  | 
            |
| 78 | |||
| 79 | /**  | 
            ||
| 80 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query  | 
            ||
| 81 | * @param array $bindings  | 
            ||
| 82 | * @return object  | 
            ||
| 83 | */  | 
            ||
| 84 | 11 | public function first($query = null, array $bindings = [])  | 
            |
| 88 | |||
| 89 | /**  | 
            ||
| 90 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query  | 
            ||
| 91 | * @param array $bindings  | 
            ||
| 92 | * @return object  | 
            ||
| 93 | * @throws \Wandu\Database\Exception\EntityNotFoundException  | 
            ||
| 94 | */  | 
            ||
| 95 | public function firstOrFail($query = null, array $bindings = [])  | 
            ||
| 103 | |||
| 104 | /**  | 
            ||
| 105 | * @param string|int $identifier  | 
            ||
| 106 | * @return object  | 
            ||
| 107 | */  | 
            ||
| 108 | 4 | public function find($identifier)  | 
            |
| 114 | |||
| 115 | /**  | 
            ||
| 116 | * @param string|int $identifier  | 
            ||
| 117 | * @return object  | 
            ||
| 118 | * @throws \Wandu\Database\Exception\EntityNotFoundException  | 
            ||
| 119 | */  | 
            ||
| 120 | public function findOrFail($identifier)  | 
            ||
| 126 | |||
| 127 | /**  | 
            ||
| 128 | * @param array $identifiers  | 
            ||
| 129 | * @return \Wandu\Collection\Contracts\ListInterface  | 
            ||
| 130 | */  | 
            ||
| 131 | public function findMany(array $identifiers = []): ListInterface  | 
            ||
| 137 | |||
| 138 | /**  | 
            ||
| 139 | * @param object $entity  | 
            ||
| 140 | * @return int  | 
            ||
| 141 | */  | 
            ||
| 142 | 3 | public function persist($entity)  | 
            |
| 151 | |||
| 152 | /**  | 
            ||
| 153 | * @param array $attributes  | 
            ||
| 154 | * @return object  | 
            ||
| 155 | */  | 
            ||
| 156 | public function create(array $attributes = [])  | 
            ||
| 159 | |||
| 160 | /**  | 
            ||
| 161 | * @param object $entity  | 
            ||
| 162 | * @return int  | 
            ||
| 163 | */  | 
            ||
| 164 | 2 | protected function executeInsert($entity)  | 
            |
| 186 | |||
| 187 | /**  | 
            ||
| 188 | * @param object $entity  | 
            ||
| 189 | * @return int  | 
            ||
| 190 | */  | 
            ||
| 191 | 2 | protected function executeUpdate($entity)  | 
            |
| 206 | |||
| 207 | /**  | 
            ||
| 208 | * @param object $entity  | 
            ||
| 209 | * @return int  | 
            ||
| 210 | */  | 
            ||
| 211 | 2 | public function delete($entity)  | 
            |
| 227 | |||
| 228 | /**  | 
            ||
| 229 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query  | 
            ||
| 230 | * @param array $bindings  | 
            ||
| 231 | * @return int  | 
            ||
| 232 | */  | 
            ||
| 233 | 3 | public function query($query, array $bindings = [])  | 
            |
| 237 | |||
| 238 | /**  | 
            ||
| 239 | * @param array $attributes  | 
            ||
| 240 | * @return object  | 
            ||
| 241 | */  | 
            ||
| 242 | 15 | public function hydrate(array $attributes = null)  | 
            |
| 271 | |||
| 272 | /**  | 
            ||
| 273 | * @param mixed $entity  | 
            ||
| 274 | * @return string|int  | 
            ||
| 275 | */  | 
            ||
| 276 | 3 | private function getIdentifier($entity)  | 
            |
| 284 | |||
| 285 | /**  | 
            ||
| 286 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query  | 
            ||
| 287 | * @return string|\Wandu\Database\Contracts\QueryInterface  | 
            ||
| 288 | */  | 
            ||
| 289 | 15 | private function normalizeSelectQuery($query = null)  | 
            |
| 303 | |||
| 304 | /**  | 
            ||
| 305 | * @param \ReflectionProperty $property  | 
            ||
| 306 | * @param object $object  | 
            ||
| 307 | * @param mixed $target  | 
            ||
| 308 | */  | 
            ||
| 309 | 17 | private function injectProperty(ReflectionProperty $property, $object, $target)  | 
            |
| 314 | |||
| 315 | /**  | 
            ||
| 316 | * @param \ReflectionProperty $property  | 
            ||
| 317 | * @param object $object  | 
            ||
| 318 | * @return mixed  | 
            ||
| 319 | */  | 
            ||
| 320 | 3 | private function pickProperty(ReflectionProperty $property, $object)  | 
            |
| 325 | |||
| 326 | /**  | 
            ||
| 327 | * @param string $name  | 
            ||
| 328 | * @return \ReflectionProperty  | 
            ||
| 329 | */  | 
            ||
| 330 | 17 | private function getPropertyReflection($name): ReflectionProperty  | 
            |
| 337 | |||
| 338 | /**  | 
            ||
| 339 | * @return \ReflectionClass  | 
            ||
| 340 | */  | 
            ||
| 341 | 17 | private function getClassReflection(): ReflectionClass  | 
            |
| 348 | |||
| 349 | /**  | 
            ||
| 350 | * @param mixed $entity  | 
            ||
| 351 | * @param string $method  | 
            ||
| 352 | */  | 
            ||
| 353 | 3 | private function assertIsInstance($entity, $method)  | 
            |
| 362 | }  | 
            ||
| 363 | 
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: