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 | 15 | public function __construct( |
|
| 52 | |||
| 53 | /** |
||
| 54 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 55 | * @param array $bindings |
||
| 56 | * @return \Generator |
||
| 57 | */ |
||
| 58 | 7 | public function fetch($query = null, array $bindings = []) |
|
| 64 | |||
| 65 | /** |
||
| 66 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 67 | * @param array $bindings |
||
| 68 | * @return \Wandu\Collection\Contracts\ListInterface |
||
| 69 | */ |
||
| 70 | 2 | public function all($query = null, array $bindings = []): ListInterface |
|
| 74 | |||
| 75 | /** |
||
| 76 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 77 | * @param array $bindings |
||
| 78 | * @return ?object |
||
| 79 | */ |
||
| 80 | 10 | public function first($query = null, array $bindings = []) |
|
| 87 | |||
| 88 | /** |
||
| 89 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 90 | * @param array $bindings |
||
| 91 | * @return object |
||
| 92 | * @throws \Wandu\Database\Exception\EntityNotFoundException |
||
| 93 | */ |
||
| 94 | public function firstOrFail($query = null, array $bindings = []) |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @param string|int $identifier |
||
| 104 | * @return object |
||
| 105 | */ |
||
| 106 | public function find($identifier) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param string|int $identifier |
||
| 115 | * @return object |
||
| 116 | * @throws \Wandu\Database\Exception\EntityNotFoundException |
||
| 117 | */ |
||
| 118 | public function findOrFail($identifier) |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @param array $identifiers |
||
| 127 | * @return \Wandu\Collection\Contracts\ListInterface |
||
| 128 | */ |
||
| 129 | public function findMany(array $identifiers = []): ListInterface |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @param object $entity |
||
| 138 | * @return int |
||
| 139 | */ |
||
| 140 | 2 | public function persist($entity) |
|
| 149 | |||
| 150 | /** |
||
| 151 | * @param object $entity |
||
| 152 | * @return int |
||
| 153 | */ |
||
| 154 | 1 | protected function executeInsert($entity) |
|
| 176 | |||
| 177 | /** |
||
| 178 | * @param object $entity |
||
| 179 | * @return int |
||
| 180 | */ |
||
| 181 | 1 | protected function executeUpdate($entity) |
|
| 196 | |||
| 197 | /** |
||
| 198 | * @param object $entity |
||
| 199 | * @return int |
||
| 200 | */ |
||
| 201 | 1 | public function delete($entity) |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 220 | * @param array $bindings |
||
| 221 | * @return int |
||
| 222 | */ |
||
| 223 | 2 | protected function query($query, array $bindings = []) |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @param array $attributes |
||
| 237 | * @return object |
||
| 238 | */ |
||
| 239 | 14 | public function hydrate(array $attributes = null) |
|
| 268 | |||
| 269 | /** |
||
| 270 | * @param mixed $entity |
||
| 271 | * @return string|int |
||
| 272 | */ |
||
| 273 | 2 | private function getIdentifier($entity) |
|
| 281 | |||
| 282 | 14 | private function normalizeSelectQuery($query = null, array $bindings = []) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @param \ReflectionProperty $property |
||
| 303 | * @param object $object |
||
| 304 | * @param mixed $target |
||
| 305 | */ |
||
| 306 | 15 | private function injectProperty(ReflectionProperty $property, $object, $target) |
|
| 311 | |||
| 312 | /** |
||
| 313 | * @param \ReflectionProperty $property |
||
| 314 | * @param object $object |
||
| 315 | * @return mixed |
||
| 316 | */ |
||
| 317 | 2 | private function pickProperty(ReflectionProperty $property, $object) |
|
| 322 | |||
| 323 | /** |
||
| 324 | * @param string $name |
||
| 325 | * @return \ReflectionProperty |
||
| 326 | */ |
||
| 327 | 15 | private function getPropertyReflection($name): ReflectionProperty |
|
| 334 | |||
| 335 | /** |
||
| 336 | * @return \ReflectionClass |
||
| 337 | */ |
||
| 338 | 15 | private function getClassReflection(): ReflectionClass |
|
| 345 | |||
| 346 | /** |
||
| 347 | * @param mixed $entity |
||
| 348 | * @param string $method |
||
| 349 | */ |
||
| 350 | 2 | private function assertIsInstance($entity, $method) |
|
| 359 | } |
||
| 360 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: