Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
9 | class Repository implements Contracts\Repository |
||
10 | { |
||
11 | private $type; |
||
12 | private $entities = []; |
||
13 | private $keyMap = []; |
||
14 | private $findCache = []; |
||
15 | private $original = []; |
||
16 | |||
17 | private $magicMethodRules = [ |
||
18 | 'by' => false, |
||
19 | 'firstBy' => true, |
||
20 | 'oneBy' => true, |
||
21 | ]; |
||
22 | |||
23 | 59 | public function __construct(Contracts\Type $type) |
|
27 | |||
28 | 59 | public function create($params = null) |
|
29 | { |
||
30 | 59 | if ($params && !is_array($params)) { |
|
31 | 59 | $params = [$params]; |
|
32 | 59 | } |
|
33 | |||
34 | 59 | if (!is_array($params)) { |
|
35 | 1 | $params = []; |
|
36 | 1 | } |
|
37 | |||
38 | 59 | $data = []; |
|
39 | 59 | foreach ($params as $k => $v) { |
|
40 | 59 | if (is_numeric($k)) { |
|
41 | 59 | if ($v instanceof Contracts\Entity) { |
|
42 | 4 | $type = $this->type->getManager()->findRepository($v)->getType(); |
|
43 | 4 | $k = $this->type->getReferenceProperty($type); |
|
44 | 2 | } else { |
|
45 | 59 | $primitive = []; |
|
46 | 59 | foreach ($this->type->getProperties() as $property) { |
|
47 | 59 | if (!$this->type->isReference($property)) { |
|
48 | 59 | $primitive[] = $property; |
|
49 | 59 | } |
|
50 | 59 | } |
|
51 | 59 | if (count($primitive) == 2) { |
|
52 | 59 | $k = $primitive[1]; |
|
53 | 59 | } else { |
|
54 | 1 | throw new Exception("Can't calculate key name"); |
|
55 | } |
||
56 | } |
||
57 | 59 | } |
|
58 | 59 | View Code Duplication | if (!$this->type->hasProperty($k)) { |
|
|||
59 | 2 | $name = $this->type->getName(); |
|
60 | 2 | throw new Exception("Unknown property $name.$k"); |
|
61 | } |
||
62 | 59 | $data[$k] = $this->type->encodeProperty($k, $v); |
|
63 | 59 | } |
|
64 | 59 | foreach ($this->type->getRequiredProperties() as $property) { |
|
65 | 59 | if (!array_key_exists($property, $data)) { |
|
66 | 59 | $convention = $this->type->getManager()->getMeta()->getConvention(); |
|
67 | 59 | $propertyType = $this->type->getPropertyType($property); |
|
68 | 59 | $data[$property] = $convention->getDefaultValue($propertyType); |
|
69 | 59 | } |
|
70 | 59 | } |
|
71 | |||
72 | 59 | $this->flushCache(); |
|
73 | |||
74 | 59 | return $this->createInstance($data); |
|
75 | } |
||
76 | |||
77 | 59 | private function createInstance($data) |
|
83 | |||
84 | 59 | public function __call($method, $arguments) |
|
85 | { |
||
86 | 59 | foreach ($this->magicMethodRules as $prefix => $oneItem) { |
|
87 | 59 | if (substr($method, 0, strlen($prefix)) == $prefix) { |
|
88 | 59 | $tail = substr($method, strlen($prefix)); |
|
89 | 59 | $fields = array_map('strtolower', explode('And', $tail)); |
|
90 | |||
91 | 59 | return $this->find(array_combine($fields, $arguments), $oneItem); |
|
92 | } |
||
93 | 59 | } |
|
94 | |||
95 | 1 | throw new BadMethodCallException("Method $method not found"); |
|
96 | } |
||
97 | |||
98 | 17 | public function findOne($params) |
|
102 | |||
103 | 59 | public function find($params = [], $oneItem = false) |
|
104 | { |
||
105 | 59 | $query = []; |
|
106 | |||
107 | 59 | if (is_string($params) && 1 * $params == $params) { |
|
108 | 1 | $params = 1 * $params; |
|
109 | 1 | } |
|
110 | |||
111 | 59 | if (is_int($params)) { |
|
112 | 10 | if (isset($this->keyMap[$params])) { |
|
113 | 3 | return $this->entities[$this->keyMap[$params]]; |
|
114 | } |
||
115 | $query = [ |
||
116 | 7 | 'id' => $params, |
|
117 | 7 | ]; |
|
118 | 7 | $oneItem = true; |
|
119 | 7 | } |
|
120 | |||
121 | 59 | if ($params instanceof Contracts\Entity) { |
|
122 | 2 | $params = [$params]; |
|
123 | 2 | } |
|
124 | |||
125 | 59 | if (is_array($params)) { |
|
126 | 59 | foreach ($params as $key => $value) { |
|
127 | 59 | if (is_numeric($key) && $value instanceof Contracts\Entity) { |
|
128 | 2 | $type = $this->type->getManager()->findRepository($value)->getType(); |
|
129 | 2 | $key = $this->type->getReferenceProperty($type); |
|
130 | 2 | } |
|
131 | 59 | if ($this->type->hasProperty($key)) { |
|
132 | 59 | $query[$key] = $this->type->encodeProperty($key, $value); |
|
133 | 59 | } else { |
|
134 | 1 | $name = $this->getType()->getName(); |
|
135 | 1 | throw new Exception("Unknown property $name.$key"); |
|
136 | } |
||
137 | 59 | } |
|
138 | 59 | } |
|
139 | |||
140 | 59 | $findKey = md5(json_encode($query).($oneItem ? 'x' : '')); |
|
141 | 59 | if (array_key_exists($findKey, $this->findCache)) { |
|
142 | 59 | return $this->findCache[$findKey]; |
|
143 | } |
||
144 | |||
145 | 59 | $index = $this->type->findIndex(array_keys($query)); |
|
146 | 59 | if (!is_numeric($index)) { |
|
147 | 2 | throw new Exception('No index found for '.json_encode(array_keys($query))); |
|
148 | } |
||
149 | |||
150 | 59 | $values = count($query) ? $this->type->getIndexTuple($index, $query) : []; |
|
151 | 59 | $data = $this->type->getSpace()->select($values, $index); |
|
152 | |||
153 | 59 | $result = []; |
|
154 | 59 | if (!empty($data->getData())) { |
|
155 | 59 | foreach ($data->getData() as $tuple) { |
|
156 | 59 | $data = $this->type->fromTuple($tuple); |
|
157 | 59 | View Code Duplication | if (isset($data['id']) && array_key_exists($data['id'], $this->keyMap)) { |
158 | 59 | $entity = $this->entities[$this->keyMap[$data['id']]]; |
|
159 | 59 | $entity->update($data); |
|
160 | 59 | } else { |
|
161 | 59 | $entity = $this->createInstance($data); |
|
162 | } |
||
163 | 59 | if ($oneItem) { |
|
164 | 59 | return $this->findCache[$findKey] = $entity; |
|
165 | } |
||
166 | 13 | $result[] = $entity; |
|
167 | 13 | } |
|
168 | 13 | } |
|
169 | 59 | if (!$oneItem) { |
|
170 | 16 | return $this->findCache[$findKey] = $result; |
|
171 | } |
||
172 | 59 | } |
|
173 | |||
174 | /** |
||
175 | * @return Entity |
||
176 | */ |
||
177 | 59 | public function knows(Contracts\Entity $entity) |
|
181 | |||
182 | 7 | public function remove(Contracts\Entity $entity) |
|
190 | |||
191 | 1 | public function removeAll() |
|
192 | { |
||
193 | 1 | foreach ($this->find([]) as $entity) { |
|
194 | 1 | $this->remove($entity); |
|
195 | 1 | } |
|
196 | 1 | $this->flushCache(); |
|
197 | 1 | } |
|
198 | |||
199 | 59 | public function flushCache() |
|
203 | |||
204 | 59 | public function save(Contracts\Entity $entity) |
|
205 | { |
||
206 | 59 | if (!$this->knows($entity)) { |
|
207 | 1 | throw new LogicException('Entity is not related with this repository'); |
|
208 | } |
||
209 | |||
210 | 59 | if (!$entity->getId()) { |
|
211 | 59 | $this->generateId($entity); |
|
212 | 59 | $tuple = $this->type->getCompleteTuple($entity->toArray()); |
|
213 | 59 | $this->type->getSpace()->insert($tuple); |
|
214 | 59 | } else { |
|
215 | 17 | $array = $entity->toArray(false); |
|
216 | 17 | $changes = []; |
|
217 | 17 | $id = $entity->getId(); |
|
218 | 17 | if (!array_key_exists($id, $this->original)) { |
|
219 | $changes = $array; |
||
220 | } else { |
||
221 | 17 | foreach ($array as $k => $v) { |
|
222 | 17 | if (!array_key_exists($k, $this->original[$id])) { |
|
223 | 3 | $changes[$k] = $v; |
|
224 | 17 | } elseif ($v !== $this->original[$id][$k]) { |
|
225 | 14 | $changes[$k] = $v; |
|
226 | 14 | } |
|
227 | 17 | } |
|
228 | } |
||
229 | |||
230 | 17 | foreach ($changes as $k => $v) { |
|
231 | 17 | View Code Duplication | if (!$this->type->hasProperty($k)) { |
232 | 1 | $name = $this->type->getName(); |
|
233 | 1 | throw new Exception("Unknown property $name.$k"); |
|
234 | } |
||
235 | 16 | } |
|
236 | |||
237 | 16 | if (count($changes)) { |
|
238 | 16 | $operations = []; |
|
239 | 16 | foreach ($this->type->getTuple($changes) as $key => $value) { |
|
240 | 16 | $operations[] = ['=', $key, $value]; |
|
241 | 16 | } |
|
242 | try { |
||
243 | 16 | $result = $this->type->getSpace()->update($id, $operations); |
|
244 | 15 | $current = $this->type->fromTuple($result->getData()[0]); |
|
245 | 15 | $this->original[$id] = $current; |
|
246 | 15 | $entity->update($current); |
|
247 | 16 | } catch (Exception $e) { |
|
248 | 1 | $this->type->getSpace()->delete([$id]); |
|
249 | 1 | $tuple = $this->type->getCompleteTuple($entity->toArray()); |
|
250 | 1 | $this->type->getSpace()->insert($tuple); |
|
251 | } |
||
252 | 16 | $this->original[$id] = $entity->toArray(); |
|
253 | 16 | } |
|
254 | } |
||
255 | |||
256 | 59 | $this->flushCache(); |
|
257 | |||
258 | 59 | return $entity; |
|
259 | } |
||
260 | |||
261 | 59 | private function register(Contracts\Entity $entity) |
|
262 | { |
||
263 | 59 | if (!$this->knows($entity)) { |
|
264 | 59 | $this->entities[] = $entity; |
|
265 | 59 | } |
|
266 | 59 | if ($entity->getId() && !array_key_exists($entity->getId(), $this->keyMap)) { |
|
267 | 59 | $this->keyMap[$entity->getId()] = array_search($entity, $this->entities); |
|
268 | 59 | } |
|
269 | |||
270 | 59 | if ($entity->getId()) { |
|
271 | 59 | $this->original[$entity->getId()] = $entity->toArray(); |
|
272 | 59 | } |
|
273 | |||
274 | 59 | return $entity; |
|
275 | } |
||
276 | |||
277 | 59 | private function generateId(Contracts\Entity $entity) |
|
278 | { |
||
279 | 59 | $manager = $this->type->getManager(); |
|
280 | 59 | $name = $this->type->getName(); |
|
281 | 59 | $spaceId = $this->type->getSpaceId(); |
|
282 | |||
283 | 59 | $sequence = $manager->get('sequence')->oneBySpace($spaceId); |
|
284 | 59 | if (!$sequence) { |
|
285 | 59 | $sequence = $manager->get('sequence')->create([ |
|
286 | 59 | 'space' => $spaceId, |
|
287 | 59 | 'value' => 0, |
|
288 | 59 | ]); |
|
289 | 59 | $manager->save($sequence); |
|
290 | 59 | } |
|
291 | |||
292 | 59 | $nextValue = +$manager->getMeta() |
|
293 | 59 | ->get('sequence') |
|
294 | 59 | ->getSpace() |
|
295 | 59 | ->update($sequence->id, [['+', 2, 1]]) |
|
296 | 59 | ->getData()[0][2]; |
|
297 | |||
298 | 59 | $entity->setId($nextValue); |
|
299 | |||
300 | 59 | $this->register($entity); |
|
301 | |||
302 | 59 | return $entity; |
|
303 | } |
||
304 | |||
305 | 59 | public function getType() |
|
309 | |||
310 | 1 | public function evaluate($query) |
|
311 | { |
||
312 | 1 | $result = []; |
|
313 | 1 | $tuples = $this->type->getManager()->getClient()->evaluate($query)->getData()[0]; |
|
314 | 1 | foreach ($tuples as $tuple) { |
|
315 | 1 | $data = $this->type->fromTuple($tuple); |
|
316 | 1 | View Code Duplication | if (isset($data['id']) && array_key_exists($data['id'], $this->keyMap)) { |
327 | } |
||
328 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.