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 | * @throws \Wandu\Database\Exception\EntityNotFoundException |
||
| 96 | */ |
||
| 97 | public function firstOrFail($query = null, array $bindings = []) |
||
| 98 | { |
||
| 99 | $attributes = $this->connection->first($this->normalizeSelectQuery($query), $bindings); |
||
| 100 | if (isset($attributes)) { |
||
| 101 | return $this->hydrate($attributes); |
||
| 102 | } |
||
| 103 | throw new EntityNotFoundException(); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @param string|int $identifier |
||
| 108 | * @return object |
||
| 109 | */ |
||
| 110 | 4 | public function find($identifier) |
|
| 111 | { |
||
| 112 | return $this->first(function (SelectQuery $select) use ($identifier) { |
||
| 113 | 4 | return $select->where($this->meta->getPrimaryKey(), $identifier); |
|
| 114 | 4 | }); |
|
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @param string|int $identifier |
||
| 119 | * @return object |
||
| 120 | * @throws \Wandu\Database\Exception\EntityNotFoundException |
||
| 121 | */ |
||
| 122 | public function findOrFail($identifier) |
||
| 123 | { |
||
| 124 | return $this->firstOrFail(function (SelectQuery $select) use ($identifier) { |
||
| 125 | return $select->where($this->meta->getPrimaryKey(), $identifier); |
||
| 126 | }); |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @param array $identifiers |
||
| 131 | * @return \Wandu\Collection\Contracts\ListInterface |
||
| 132 | */ |
||
| 133 | public function findMany(array $identifiers = []): ListInterface |
||
| 134 | { |
||
| 135 | 1 | return $this->all(function (SelectQuery $select) use ($identifiers) { |
|
| 136 | 1 | return $select->where($this->meta->getPrimaryKey(), 'IN', $identifiers); |
|
|
|
|||
| 137 | 1 | }); |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * @param object $entity |
||
| 142 | * @return int |
||
| 143 | */ |
||
| 144 | 1 | public function insert($entity) |
|
| 145 | { |
||
| 146 | 1 | $this->assertIsInstance($entity, __METHOD__); |
|
| 147 | 1 | $primaryKey = $this->meta->getPrimaryKey(); |
|
| 148 | 1 | $primaryProperty = null; |
|
| 149 | 1 | $columns = $this->meta->getColumns(); |
|
| 150 | 1 | $attributesToStore = []; |
|
| 151 | 1 | foreach ($columns as $propertyName => $column) { |
|
| 152 | 1 | if ($primaryKey === $column->name) { |
|
| 153 | 1 | $primaryProperty = $propertyName; |
|
| 154 | 1 | continue; |
|
| 155 | } |
||
| 156 | 1 | $attributesToStore[$column->name] = $this->pickProperty($this->getPropertyReflection($propertyName), $entity); |
|
| 157 | } |
||
| 158 | 1 | $rowAffected = $this->query($this->queryBuilder->insert($attributesToStore)); |
|
| 159 | 1 | if ($this->meta->isIncrements()) { |
|
| 160 | 1 | $lastInsertId = $this->connection->getLastInsertId(); |
|
| 161 | 1 | if ($primaryProperty) { |
|
| 162 | 1 | $this->injectProperty($this->getPropertyReflection($primaryProperty), $entity, $lastInsertId); |
|
| 163 | } |
||
| 164 | } |
||
| 165 | 1 | return $rowAffected; |
|
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @param object $entity |
||
| 170 | * @return int |
||
| 171 | */ |
||
| 172 | 1 | public function update($entity) |
|
| 173 | { |
||
| 174 | 1 | $this->assertIsInstance($entity, __METHOD__); |
|
| 175 | 1 | $primaryKey = $this->meta->getPrimaryKey(); |
|
| 176 | |||
| 177 | 1 | $identifier = $this->getIdentifier($entity); |
|
| 178 | 1 | $attributesToStore = []; |
|
| 179 | 1 | foreach ($this->meta->getColumns() as $propertyName => $column) { |
|
| 180 | 1 | if ($primaryKey === $column->name) continue; |
|
| 181 | 1 | $attributesToStore[$column->name] = $this->pickProperty($this->getPropertyReflection($propertyName), $entity); |
|
| 182 | } |
||
| 183 | |||
| 184 | 1 | return $this->query( |
|
| 185 | 1 | $this->queryBuilder->update($attributesToStore)->where($primaryKey, $identifier) |
|
| 186 | ); |
||
| 187 | } |
||
| 188 | |||
| 189 | /** |
||
| 190 | * @param object $entity |
||
| 191 | * @return int |
||
| 192 | */ |
||
| 193 | 1 | public function delete($entity) |
|
| 206 | |||
| 207 | /** |
||
| 208 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 209 | * @param array $bindings |
||
| 210 | * @return int |
||
| 211 | */ |
||
| 212 | 2 | public function query($query, array $bindings = []) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * @param array $attributes |
||
| 219 | * @return object |
||
| 220 | */ |
||
| 221 | 15 | public function hydrate(array $attributes = null) |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @param mixed $entity |
||
| 253 | * @return string|int |
||
| 254 | */ |
||
| 255 | 2 | private function getIdentifier($entity) |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 269 | * @return string|\Wandu\Database\Contracts\QueryInterface |
||
| 270 | */ |
||
| 271 | 15 | private function normalizeSelectQuery($query = null) |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @param \ReflectionProperty $property |
||
| 288 | * @param object $object |
||
| 289 | * @param mixed $target |
||
| 290 | */ |
||
| 291 | 16 | private function injectProperty(ReflectionProperty $property, $object, $target) |
|
| 296 | |||
| 297 | /** |
||
| 298 | * @param \ReflectionProperty $property |
||
| 299 | * @param object $object |
||
| 300 | * @return mixed |
||
| 301 | */ |
||
| 302 | 2 | private function pickProperty(ReflectionProperty $property, $object) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * @param string $name |
||
| 310 | * @return \ReflectionProperty |
||
| 311 | */ |
||
| 312 | 16 | private function getPropertyReflection($name): ReflectionProperty |
|
| 319 | |||
| 320 | /** |
||
| 321 | * @return \ReflectionClass |
||
| 322 | */ |
||
| 323 | 16 | private function getClassReflection(): ReflectionClass |
|
| 330 | |||
| 331 | /** |
||
| 332 | * @param mixed $entity |
||
| 333 | * @param string $method |
||
| 334 | */ |
||
| 335 | 2 | 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: