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 |
||
| 11 | class Repository |
||
| 12 | { |
||
| 13 | /** @var \Wandu\Database\Contracts\ConnectionInterface */ |
||
| 14 | protected $connection; |
||
| 15 | |||
| 16 | /** @var \Wandu\Database\Repository\RepositorySettings */ |
||
| 17 | protected $settings; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Repository constructor. |
||
| 21 | * @param \Wandu\Database\Contracts\ConnectionInterface $connection |
||
| 22 | * @param \Wandu\Database\Repository\RepositorySettings $settings |
||
| 23 | */ |
||
| 24 | public function __construct(ConnectionInterface $connection, RepositorySettings $settings) |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 32 | * @param array $bindings |
||
| 33 | * @return \Generator |
||
| 34 | */ |
||
| 35 | public function fetch($query, array $bindings = []) |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 44 | * @param array $bindings |
||
| 45 | * @return object |
||
| 46 | */ |
||
| 47 | public function first($query, array $bindings = []) |
||
| 51 | |||
| 52 | public function find($identifier) |
||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * @param object $entity |
||
| 61 | * @return int |
||
| 62 | */ |
||
| 63 | public function insert($entity) |
||
| 81 | |||
| 82 | public function update($entity) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param object $entity |
||
| 103 | * @return int |
||
| 104 | */ |
||
| 105 | public function delete($entity) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 123 | * @param array $bindings |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | public function query($query, array $bindings = []) |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @param array $attributes |
||
| 133 | * @return object |
||
| 134 | */ |
||
| 135 | public function hydrate(array $attributes = []) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 160 | * @return string|\Wandu\Database\Contracts\QueryInterface |
||
| 161 | */ |
||
| 162 | private function normalizeQuery($query) |
||
| 173 | |||
| 174 | private function cast($value, $type) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param \ReflectionProperty $property |
||
| 193 | * @param object $object |
||
| 194 | * @param mixed $target |
||
| 195 | */ |
||
| 196 | private function injectProperty(ReflectionProperty $property, $object, $target) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * @param \ReflectionProperty $property |
||
| 204 | * @param object $object |
||
| 205 | * @return mixed |
||
| 206 | */ |
||
| 207 | private function pickProperty(ReflectionProperty $property, $object) |
||
| 212 | |||
| 213 | /** |
||
| 214 | * @return \ReflectionClass |
||
| 215 | */ |
||
| 216 | private function getClassReflection() |
||
| 225 | |||
| 226 | /** |
||
| 227 | * @param string $name |
||
| 228 | * @return \ReflectionProperty |
||
| 229 | */ |
||
| 230 | private function getPropertyReflection($name) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param mixed $entity |
||
| 241 | * @param string $method |
||
| 242 | */ |
||
| 243 | private function assertIsInstance($entity, $method) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param mixed $entity |
||
| 254 | * @return boolean |
||
| 255 | */ |
||
| 256 | private function isInstance($entity) |
||
| 260 | } |
||
| 261 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.