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|int $identifier |
||
93 | * @return object |
||
94 | */ |
||
95 | 4 | public function find($identifier) |
|
101 | |||
102 | /** |
||
103 | * @param array $identifiers |
||
104 | * @return \Wandu\Collection\Contracts\ListInterface |
||
105 | */ |
||
106 | public function findMany(array $identifiers = []): ListInterface |
||
112 | |||
113 | /** |
||
114 | * @param object $entity |
||
115 | * @return int |
||
116 | */ |
||
117 | 1 | public function insert($entity) |
|
140 | |||
141 | /** |
||
142 | * @param object $entity |
||
143 | * @return int |
||
144 | */ |
||
145 | 1 | public function update($entity) |
|
161 | |||
162 | /** |
||
163 | * @param object $entity |
||
164 | * @return int |
||
165 | */ |
||
166 | 1 | public function delete($entity) |
|
179 | |||
180 | /** |
||
181 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
182 | * @param array $bindings |
||
183 | * @return int |
||
184 | */ |
||
185 | 2 | public function query($query, array $bindings = []) |
|
189 | |||
190 | /** |
||
191 | * @param array $attributes |
||
192 | * @return object |
||
193 | */ |
||
194 | 15 | public function hydrate(array $attributes = null) |
|
223 | |||
224 | /** |
||
225 | * @param mixed $entity |
||
226 | * @return string|int |
||
227 | */ |
||
228 | 2 | private function getIdentifier($entity) |
|
239 | |||
240 | /** |
||
241 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
242 | * @return string|\Wandu\Database\Contracts\QueryInterface |
||
243 | */ |
||
244 | 15 | private function normalizeSelectQuery($query = null) |
|
258 | |||
259 | /** |
||
260 | * @param \ReflectionProperty $property |
||
261 | * @param object $object |
||
262 | * @param mixed $target |
||
263 | */ |
||
264 | 16 | private function injectProperty(ReflectionProperty $property, $object, $target) |
|
269 | |||
270 | /** |
||
271 | * @param \ReflectionProperty $property |
||
272 | * @param object $object |
||
273 | * @return mixed |
||
274 | */ |
||
275 | 2 | private function pickProperty(ReflectionProperty $property, $object) |
|
280 | |||
281 | /** |
||
282 | * @param string $name |
||
283 | * @return \ReflectionProperty |
||
284 | */ |
||
285 | 16 | private function getPropertyReflection($name): ReflectionProperty |
|
292 | |||
293 | /** |
||
294 | * @return \ReflectionClass |
||
295 | */ |
||
296 | 16 | private function getClassReflection(): ReflectionClass |
|
303 | |||
304 | /** |
||
305 | * @param mixed $entity |
||
306 | * @param string $method |
||
307 | */ |
||
308 | 2 | private function assertIsInstance($entity, $method) |
|
317 | } |
||
318 |
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: