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 |
||
12 | class Repository |
||
13 | { |
||
14 | /** @var \Wandu\Database\Contracts\ConnectionInterface */ |
||
15 | protected $connection; |
||
16 | |||
17 | /** @var \Wandu\Database\Repository\RepositorySettings */ |
||
18 | protected $settings; |
||
19 | |||
20 | /** |
||
21 | * Repository constructor. |
||
22 | * @param \Wandu\Database\Contracts\ConnectionInterface $connection |
||
23 | * @param \Wandu\Database\Repository\RepositorySettings $settings |
||
24 | */ |
||
25 | 13 | public function __construct(ConnectionInterface $connection, RepositorySettings $settings) |
|
30 | |||
31 | /** |
||
32 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
33 | * @param array $bindings |
||
34 | * @return \Generator |
||
35 | */ |
||
36 | 5 | public function fetch($query = null, array $bindings = []) |
|
42 | |||
43 | /** |
||
44 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
45 | * @param array $bindings |
||
46 | * @return object |
||
47 | */ |
||
48 | 7 | public function first($query = null, array $bindings = []) |
|
52 | |||
53 | /** |
||
54 | * @param string|int $identifier |
||
55 | * @return object |
||
56 | */ |
||
57 | public function find($identifier) |
||
64 | |||
65 | /** |
||
66 | * @param object $entity |
||
67 | * @return int |
||
68 | */ |
||
69 | 1 | public function insert($entity) |
|
87 | |||
88 | /** |
||
89 | * @param object $entity |
||
90 | * @return int |
||
91 | */ |
||
92 | 1 | public function update($entity) |
|
110 | |||
111 | /** |
||
112 | * @param object $entity |
||
113 | * @return int |
||
114 | */ |
||
115 | 1 | public function delete($entity) |
|
130 | |||
131 | /** |
||
132 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
133 | * @param array $bindings |
||
134 | * @return int |
||
135 | */ |
||
136 | 2 | public function query($query, array $bindings = []) |
|
140 | |||
141 | /** |
||
142 | * @param array $attributes |
||
143 | * @return object |
||
144 | */ |
||
145 | 11 | public function hydrate(array $attributes = null) |
|
171 | |||
172 | /** |
||
173 | * @param string|callable|\Wandu\Database\Contracts\QueryInterface $query |
||
174 | * @return string|\Wandu\Database\Contracts\QueryInterface |
||
175 | */ |
||
176 | 11 | private function normalizeQuery($query = null) |
|
190 | |||
191 | private function cast($value, $type) |
||
207 | |||
208 | /** |
||
209 | * @param \ReflectionProperty $property |
||
210 | * @param object $object |
||
211 | * @param mixed $target |
||
212 | */ |
||
213 | 12 | private function injectProperty(ReflectionProperty $property, $object, $target) |
|
218 | |||
219 | /** |
||
220 | * @param \ReflectionProperty $property |
||
221 | * @param object $object |
||
222 | * @return mixed |
||
223 | */ |
||
224 | 2 | private function pickProperty(ReflectionProperty $property, $object) |
|
229 | |||
230 | /** |
||
231 | * @return \ReflectionClass |
||
232 | */ |
||
233 | 12 | private function getClassReflection() |
|
242 | |||
243 | /** |
||
244 | * @param string $name |
||
245 | * @return \ReflectionProperty |
||
246 | */ |
||
247 | 12 | private function getPropertyReflection($name) |
|
255 | |||
256 | /** |
||
257 | * @param mixed $entity |
||
258 | * @param string $method |
||
259 | */ |
||
260 | 2 | private function assertIsInstance($entity, $method) |
|
268 | |||
269 | /** |
||
270 | * @param mixed $entity |
||
271 | * @return boolean |
||
272 | */ |
||
273 | 2 | private function isInstance($entity) |
|
277 | } |
||
278 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.