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 |
||
15 | class Repository |
||
16 | { |
||
17 | /** @var \Wandu\Database\Manager */ |
||
18 | protected $manager; |
||
19 | |||
20 | /** @var \Wandu\Database\Contracts\ConnectionInterface */ |
||
21 | protected $connection; |
||
22 | |||
23 | /** @var \Wandu\Database\Entity\Metadata */ |
||
24 | protected $meta; |
||
25 | |||
26 | /** @var \Wandu\Database\QueryBuilder */ |
||
27 | protected $queryBuilder; |
||
28 | |||
29 | /** @var \ReflectionClass */ |
||
30 | protected $reflClass; |
||
31 | |||
32 | /** @var \ReflectionProperty[] */ |
||
33 | protected $refProperties = []; |
||
34 | |||
35 | /** |
||
36 | * Repository constructor. |
||
37 | * @param \Wandu\Database\Manager $manager |
||
38 | * @param \Wandu\Database\Entity\Metadata $meta |
||
39 | */ |
||
40 | 14 | public function __construct(Manager $manager, Metadata $meta) |
|
47 | |||
48 | /** |
||
49 | * @return \Wandu\Database\Entity\Metadata |
||
50 | */ |
||
51 | public function getMeta(): Metadata |
||
55 | |||
56 | /** |
||
57 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
58 | * @param array $bindings |
||
59 | * @return \Generator |
||
60 | */ |
||
61 | 5 | public function fetch($query = null, array $bindings = []) |
|
67 | |||
68 | /** |
||
69 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
70 | * @param array $bindings |
||
71 | * @return object |
||
72 | */ |
||
73 | 9 | public function first($query = null, array $bindings = []) |
|
77 | |||
78 | /** |
||
79 | * @param string|int $identifier |
||
80 | * @return object |
||
81 | */ |
||
82 | public function find($identifier) |
||
88 | |||
89 | /** |
||
90 | * @param object $entity |
||
91 | * @return int |
||
92 | */ |
||
93 | 1 | public function insert($entity) |
|
116 | |||
117 | /** |
||
118 | * @param object $entity |
||
119 | * @return int |
||
120 | */ |
||
121 | 1 | public function update($entity) |
|
137 | |||
138 | /** |
||
139 | * @param object $entity |
||
140 | * @return int |
||
141 | */ |
||
142 | 1 | public function delete($entity) |
|
155 | |||
156 | /** |
||
157 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
158 | * @param array $bindings |
||
159 | * @return int |
||
160 | */ |
||
161 | 2 | public function query($query, array $bindings = []) |
|
165 | |||
166 | /** |
||
167 | * @param array $attributes |
||
168 | * @return object |
||
169 | */ |
||
170 | 13 | public function hydrate(array $attributes = null) |
|
191 | |||
192 | /** |
||
193 | * @param mixed $entity |
||
194 | * @return string|int |
||
195 | */ |
||
196 | 2 | private function getIdentifier($entity) |
|
207 | |||
208 | /** |
||
209 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
210 | * @return string|\Wandu\Database\Contracts\QueryInterface |
||
211 | */ |
||
212 | 13 | private function normalizeSelectQuery($query = null) |
|
226 | |||
227 | 2 | private function cast($value, $type) |
|
243 | |||
244 | /** |
||
245 | * @param \ReflectionProperty $property |
||
246 | * @param object $object |
||
247 | * @param mixed $target |
||
248 | */ |
||
249 | 14 | private function injectProperty(ReflectionProperty $property, $object, $target) |
|
254 | |||
255 | /** |
||
256 | * @param \ReflectionProperty $property |
||
257 | * @param object $object |
||
258 | * @return mixed |
||
259 | */ |
||
260 | 2 | private function pickProperty(ReflectionProperty $property, $object) |
|
265 | |||
266 | /** |
||
267 | * @param string $name |
||
268 | * @return \ReflectionProperty |
||
269 | */ |
||
270 | 14 | private function getPropertyReflection($name): ReflectionProperty |
|
277 | |||
278 | /** |
||
279 | * @return \ReflectionClass |
||
280 | */ |
||
281 | 14 | private function getClassReflection(): ReflectionClass |
|
288 | |||
289 | /** |
||
290 | * @param mixed $entity |
||
291 | * @param string $method |
||
292 | */ |
||
293 | 2 | private function assertIsInstance($entity, $method) |
|
302 | } |
||
303 |
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: