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 | 12 | 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 | 5 | public function fetch($query = null, array $bindings = []) |
|
| 36 | { |
||
| 37 | 5 | foreach ($this->connection->fetch($this->normalizeQuery($query), $bindings) as $row) { |
|
| 38 | 5 | yield $this->hydrate($row); |
|
| 39 | } |
||
| 40 | 5 | } |
|
| 41 | |||
| 42 | /** |
||
| 43 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 44 | * @param array $bindings |
||
| 45 | * @return object |
||
| 46 | */ |
||
| 47 | 6 | public function first($query = null, array $bindings = []) |
|
| 51 | |||
| 52 | public function find($identifier) |
||
| 56 | |||
| 57 | |||
| 58 | |||
| 59 | /** |
||
| 60 | * @param object $entity |
||
| 61 | * @return int |
||
| 62 | */ |
||
| 63 | 1 | public function insert($entity) |
|
| 64 | { |
||
| 65 | 1 | $this->assertIsInstance($entity, __METHOD__); |
|
| 66 | 1 | $identifierKey = $this->settings->getIdentifier(); |
|
| 67 | 1 | $columns = $this->settings->getColumns(); |
|
| 68 | 1 | $attributesToStore = []; |
|
| 69 | 1 | foreach ($columns as $columnName => $propertyName) { |
|
| 70 | 1 | if ($identifierKey === $propertyName) continue; |
|
| 71 | 1 | $attributesToStore[$columnName] = $this->pickProperty($this->getPropertyReflection($propertyName), $entity); |
|
| 72 | } |
||
| 73 | 1 | $queryBuilder = $this->connection->createQueryBuilder($this->settings->getTable()); |
|
| 74 | 1 | $rowAffected = $this->query($queryBuilder->insert($attributesToStore)); |
|
| 75 | 1 | if ($this->settings->isIncrements()) { |
|
| 76 | 1 | $lastInsertId = $this->connection->getLastInsertId(); |
|
| 77 | 1 | $this->injectProperty($this->getPropertyReflection($columns[$identifierKey]), $entity, $lastInsertId); |
|
| 78 | } |
||
| 79 | 1 | return $rowAffected; |
|
| 80 | } |
||
| 81 | |||
| 82 | 1 | public function update($entity) |
|
| 83 | { |
||
| 84 | 1 | $this->assertIsInstance($entity, __METHOD__); |
|
| 85 | 1 | $identifierKey = $this->settings->getIdentifier(); |
|
| 86 | 1 | $columns = $this->settings->getColumns(); |
|
| 87 | 1 | $identifier = $this->pickProperty($this->getPropertyReflection($columns[$identifierKey]), $entity); |
|
| 88 | 1 | if (!$identifier) { |
|
| 89 | throw new IdentifierNotFoundException(); |
||
| 90 | } |
||
| 91 | 1 | $attributesToStore = []; |
|
| 92 | 1 | foreach ($columns as $columnName => $propertyName) { |
|
| 93 | 1 | if ($identifierKey === $propertyName) continue; |
|
| 94 | 1 | $attributesToStore[$columnName] = $this->pickProperty($this->getPropertyReflection($propertyName), $entity); |
|
| 95 | } |
||
| 96 | |||
| 97 | 1 | $queryBuilder = $this->connection->createQueryBuilder($this->settings->getTable()); |
|
| 98 | 1 | return $this->query($queryBuilder->update($attributesToStore)->where($identifierKey, $identifier)); |
|
| 99 | } |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param object $entity |
||
| 103 | * @return int |
||
| 104 | */ |
||
| 105 | 1 | public function delete($entity) |
|
| 120 | |||
| 121 | /** |
||
| 122 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 123 | * @param array $bindings |
||
| 124 | * @return int |
||
| 125 | */ |
||
| 126 | 2 | public function query($query, array $bindings = []) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @param array $attributes |
||
| 133 | * @return object |
||
| 134 | */ |
||
| 135 | 10 | public function hydrate(array $attributes = []) |
|
| 136 | { |
||
| 137 | 10 | $model = $this->settings->getModel(); |
|
| 138 | 10 | $casts = $this->settings->getCasts(); |
|
| 139 | 10 | $columns = $this->settings->getColumns(); // map |
|
| 140 | |||
| 141 | 10 | if ($model) { |
|
| 142 | 10 | $classReflection = $this->getClassReflection(); |
|
| 143 | 10 | $entity = $classReflection->newInstanceWithoutConstructor(); |
|
| 144 | 10 | foreach ($attributes as $name => $attribute) { |
|
| 145 | 10 | $value = isset($casts[$name]) ? $this->cast($attribute, $casts[$name]) : $attribute; |
|
| 146 | 10 | $this->injectProperty($this->getPropertyReflection($columns[$name]), $entity, $value); |
|
| 147 | } |
||
| 148 | } else { |
||
| 149 | $entity = new stdClass(); |
||
| 150 | foreach ($attributes as $name => $attribute) { |
||
| 151 | $value = isset($casts[$name]) ? $this->cast($attribute, $casts[$name]) : $attribute; |
||
| 152 | $entity->{$columns[$name]} = $value; |
||
| 153 | } |
||
| 154 | } |
||
| 155 | 10 | return $entity; |
|
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
| 160 | * @return string|\Wandu\Database\Contracts\QueryInterface |
||
| 161 | */ |
||
| 162 | 10 | private function normalizeQuery($query = null) |
|
| 163 | { |
||
| 164 | 10 | if (!isset($query) || is_callable($query)) { |
|
| 165 | 5 | $connection = $this->connection; |
|
| 166 | 5 | $queryBuilder = $connection->createQueryBuilder($this->settings->getTable())->select(); |
|
| 167 | 5 | if (!isset($query)) { |
|
| 168 | return $queryBuilder; |
||
| 169 | } |
||
| 170 | 5 | while (is_callable($query)) { |
|
| 171 | 5 | $query = call_user_func($query, $queryBuilder, $connection); |
|
| 172 | } |
||
| 173 | } |
||
| 174 | 10 | return $query; |
|
| 175 | } |
||
| 176 | |||
| 177 | 10 | private function cast($value, $type) |
|
| 178 | { |
||
| 179 | // "string", "integer", "float", "boolean", "array", "datetime", "date", "time" |
||
| 180 | switch ($type) { |
||
| 181 | 10 | case 'string': |
|
| 182 | 10 | return (string) $value; |
|
| 183 | 10 | case 'integer': |
|
| 184 | 10 | return (int) $value; |
|
| 185 | case 'float': |
||
| 186 | return (float) $value; |
||
| 187 | case 'boolean': |
||
| 188 | return (bool) $value; |
||
| 189 | default: |
||
| 190 | return $value; |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param \ReflectionProperty $property |
||
| 196 | * @param object $object |
||
| 197 | * @param mixed $target |
||
| 198 | */ |
||
| 199 | 11 | private function injectProperty(ReflectionProperty $property, $object, $target) |
|
| 204 | |||
| 205 | /** |
||
| 206 | * @param \ReflectionProperty $property |
||
| 207 | * @param object $object |
||
| 208 | * @return mixed |
||
| 209 | */ |
||
| 210 | 2 | private function pickProperty(ReflectionProperty $property, $object) |
|
| 215 | |||
| 216 | /** |
||
| 217 | * @return \ReflectionClass |
||
| 218 | */ |
||
| 219 | 11 | private function getClassReflection() |
|
| 228 | |||
| 229 | /** |
||
| 230 | * @param string $name |
||
| 231 | * @return \ReflectionProperty |
||
| 232 | */ |
||
| 233 | 11 | private function getPropertyReflection($name) |
|
| 241 | |||
| 242 | /** |
||
| 243 | * @param mixed $entity |
||
| 244 | * @param string $method |
||
| 245 | */ |
||
| 246 | 2 | private function assertIsInstance($entity, $method) |
|
| 247 | { |
||
| 248 | 2 | if (!$this->isInstance($entity)) { |
|
| 249 | 2 | throw new InvalidArgumentException( |
|
| 250 | 2 | "Argument 1 passed to {$method}() must be of the type " . $this->getClassReflection()->getName() |
|
| 251 | ); |
||
| 252 | } |
||
| 253 | 2 | } |
|
| 254 | |||
| 255 | /** |
||
| 256 | * @param mixed $entity |
||
| 257 | * @return boolean |
||
| 258 | */ |
||
| 259 | 2 | private function isInstance($entity) |
|
| 263 | } |
||
| 264 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.