Total Complexity | 44 |
Total Lines | 370 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like ORM 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.
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 ORM, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
29 | class ORM implements ORMInterface |
||
30 | { |
||
31 | /** @var CommandGenerator */ |
||
32 | private $generator; |
||
33 | |||
34 | /** @var FactoryInterface|SourceProviderInterface */ |
||
35 | private $factory; |
||
36 | |||
37 | /** @var ProxyFactoryInterface|null */ |
||
38 | private $proxyFactory; |
||
39 | |||
40 | /** @var HeapInterface */ |
||
41 | private $heap; |
||
42 | |||
43 | /** @var SchemaInterface|null */ |
||
44 | private $schema; |
||
45 | |||
46 | /** @var MapperInterface[] */ |
||
47 | private $mappers = []; |
||
48 | |||
49 | /** @var RepositoryInterface[] */ |
||
50 | private $repositories = []; |
||
51 | |||
52 | /** @var RelationMap[] */ |
||
53 | private $relmaps = []; |
||
54 | |||
55 | /** @var array */ |
||
56 | private $indexes = []; |
||
57 | |||
58 | /** @var SourceInterface[] */ |
||
59 | private $sources = []; |
||
60 | |||
61 | /** |
||
62 | * @param FactoryInterface|SourceProviderInterface $factory |
||
63 | * @param SchemaInterface|null $schema |
||
64 | */ |
||
65 | public function __construct(FactoryInterface $factory, SchemaInterface $schema = null) |
||
66 | { |
||
67 | $this->generator = new CommandGenerator(); |
||
68 | $this->factory = $factory; |
||
69 | $this->schema = $schema; |
||
70 | |||
71 | $this->heap = new Heap(); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Automatically resolve role based on object name. |
||
76 | * |
||
77 | * @param string|object $entity |
||
78 | * @return string |
||
79 | */ |
||
80 | public function resolveRole($entity): string |
||
97 | } |
||
98 | |||
99 | /** |
||
100 | * @inheritdoc |
||
101 | */ |
||
102 | public function get(string $role, string $key, $value, bool $load = true) |
||
103 | { |
||
104 | $role = $this->resolveRole($role); |
||
105 | if (!is_null($e = $this->heap->find($role, $key, $value))) { |
||
106 | return $e; |
||
107 | } |
||
108 | |||
109 | if (!$load) { |
||
110 | return null; |
||
111 | } |
||
112 | |||
113 | return $this->getRepository($role)->findByPK($value); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * @inheritdoc |
||
118 | */ |
||
119 | public function make(string $role, array $data = [], int $node = Node::NEW) |
||
120 | { |
||
121 | $m = $this->getMapper($role); |
||
122 | |||
123 | // unique entity identifier |
||
124 | $pk = $this->schema->define($role, Schema::PRIMARY_KEY); |
||
125 | $id = $data[$pk] ?? null; |
||
126 | |||
127 | if ($node !== Node::NEW && !empty($id)) { |
||
128 | $e = $this->heap->find($role, $pk, $id); |
||
129 | if ($e !== null) { |
||
130 | $node = $this->heap->get($e); |
||
131 | |||
132 | // entity already been loaded, let's update it's relations with new context |
||
133 | return $m->hydrate($e, $this->getRelmap($role)->init($node, $data)); |
||
134 | } |
||
135 | } |
||
136 | |||
137 | // init entity class and prepared (typecasted) data |
||
138 | list($e, $prepared) = $m->init($data); |
||
139 | |||
140 | $node = new Node($node, $prepared, $m->getRole()); |
||
141 | |||
142 | $this->heap->attach($e, $node, $this->getIndexes($m->getRole())); |
||
143 | |||
144 | // hydrate entity with it's data, relations and proxies |
||
145 | return $m->hydrate($e, $this->getRelmap($role)->init($node, $prepared)); |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | public function withFactory(FactoryInterface $factory): ORMInterface |
||
152 | { |
||
153 | $orm = clone $this; |
||
154 | $orm->factory = $factory; |
||
155 | |||
156 | return $orm; |
||
157 | } |
||
158 | |||
159 | /** |
||
160 | * @inheritdoc |
||
161 | */ |
||
162 | public function getFactory(): FactoryInterface |
||
163 | { |
||
164 | return $this->factory; |
||
165 | } |
||
166 | |||
167 | /** |
||
168 | * @inheritdoc |
||
169 | */ |
||
170 | public function withSchema(SchemaInterface $schema): ORMInterface |
||
171 | { |
||
172 | $orm = clone $this; |
||
173 | $orm->schema = $schema; |
||
174 | |||
175 | return $orm; |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * @inheritdoc |
||
180 | */ |
||
181 | public function getSchema(): SchemaInterface |
||
182 | { |
||
183 | if (is_null($this->schema)) { |
||
184 | throw new ORMException("ORM is not configured, schema is missing"); |
||
185 | } |
||
186 | |||
187 | return $this->schema; |
||
188 | } |
||
189 | |||
190 | /** |
||
191 | * @inheritdoc |
||
192 | */ |
||
193 | public function withHeap(HeapInterface $heap): ORMInterface |
||
194 | { |
||
195 | $orm = clone $this; |
||
196 | $orm->heap = $heap; |
||
197 | |||
198 | return $orm; |
||
199 | } |
||
200 | |||
201 | /** |
||
202 | * @inheritdoc |
||
203 | */ |
||
204 | public function getHeap(): HeapInterface |
||
205 | { |
||
206 | return $this->heap; |
||
207 | } |
||
208 | |||
209 | /** |
||
210 | * @inheritdoc |
||
211 | */ |
||
212 | public function getMapper($entity): MapperInterface |
||
213 | { |
||
214 | $role = $this->resolveRole($entity); |
||
215 | if (isset($this->mappers[$role])) { |
||
216 | return $this->mappers[$role]; |
||
217 | } |
||
218 | |||
219 | return $this->mappers[$role] = $this->factory->mapper($this, $this->schema, $role); |
||
220 | } |
||
221 | |||
222 | /** |
||
223 | * @inheritdoc |
||
224 | */ |
||
225 | public function getRepository($entity): RepositoryInterface |
||
226 | { |
||
227 | $role = $this->resolveRole($entity); |
||
228 | if (isset($this->repositories[$role])) { |
||
229 | return $this->repositories[$role]; |
||
230 | } |
||
231 | |||
232 | $selector = new Select($this, $role); |
||
233 | $selector->constrain($this->getSource($role)->getConstrain()); |
||
234 | |||
235 | $repositoryClass = $this->getSchema()->define($role, Schema::REPOSITORY) ?? Repository::class; |
||
236 | |||
237 | return $this->repositories[$role] = new $repositoryClass($selector); |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @inheritdoc |
||
242 | */ |
||
243 | public function getSource(string $role): SourceInterface |
||
265 | } |
||
266 | |||
267 | /** |
||
268 | * Overlay existing promise factory. |
||
269 | * |
||
270 | * @param ProxyFactoryInterface $proxyFactory |
||
271 | * @return ORM |
||
272 | */ |
||
273 | public function withProxyFactory(ProxyFactoryInterface $proxyFactory): self |
||
274 | { |
||
275 | $orm = clone $this; |
||
276 | $orm->proxyFactory = $proxyFactory; |
||
277 | |||
278 | return $orm; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * @inheritdoc |
||
283 | * |
||
284 | * Returns references by default. |
||
285 | */ |
||
286 | public function promise(string $role, array $scope) |
||
287 | { |
||
288 | $e = $this->heap->find($role, key($scope), current($scope)); |
||
289 | if ($e !== null) { |
||
290 | return $e; |
||
291 | } |
||
292 | |||
293 | if ($this->proxyFactory !== null) { |
||
294 | return $this->proxyFactory->proxy($this, $role, $scope); |
||
295 | } |
||
296 | |||
297 | return new Reference($role, $scope); |
||
298 | } |
||
299 | |||
300 | /** |
||
301 | * @inheritdoc |
||
302 | */ |
||
303 | public function queueStore($entity, int $mode = TransactionInterface::MODE_CASCADE): ContextCarrierInterface |
||
304 | { |
||
305 | if ($entity instanceof ReferenceInterface) { |
||
306 | // we do not expect to store promises |
||
307 | return new Nil(); |
||
308 | } |
||
309 | |||
310 | $mapper = $this->getMapper($entity); |
||
311 | |||
312 | $node = $this->heap->get($entity); |
||
313 | if (is_null($node)) { |
||
314 | // automatic entity registration |
||
315 | $node = new Node(Node::NEW, [], $mapper->getRole()); |
||
316 | $this->heap->attach($entity, $node); |
||
317 | } |
||
318 | |||
319 | $cmd = $this->generator->generateStore($mapper, $entity, $node); |
||
320 | if ($mode != TransactionInterface::MODE_CASCADE) { |
||
321 | return $cmd; |
||
322 | } |
||
323 | |||
324 | // generate set of commands required to store entity relations |
||
325 | return $this->getRelmap($node->getRole())->queueRelations( |
||
326 | $cmd, |
||
327 | $entity, |
||
328 | $node, |
||
329 | $mapper->extract($entity) |
||
330 | ); |
||
331 | } |
||
332 | |||
333 | /** |
||
334 | * @inheritdoc |
||
335 | */ |
||
336 | public function queueDelete($entity, int $mode = TransactionInterface::MODE_CASCADE): CommandInterface |
||
337 | { |
||
338 | $node = $this->heap->get($entity); |
||
339 | if ($entity instanceof ReferenceInterface || is_null($node)) { |
||
340 | // nothing to do, what about promises? |
||
341 | return new Nil(); |
||
342 | } |
||
343 | |||
344 | // currently we rely on db to delete all nested records (or soft deletes) |
||
345 | return $this->generator->generateDelete($this->getMapper($node->getRole()), $entity, $node); |
||
346 | } |
||
347 | |||
348 | /** |
||
349 | * Reset related objects cache. |
||
350 | */ |
||
351 | public function __clone() |
||
358 | } |
||
359 | |||
360 | /** |
||
361 | * Get list of keys entity must be indexed in a Heap by. |
||
362 | * |
||
363 | * @param string $role |
||
364 | * @return array |
||
365 | */ |
||
366 | protected function getIndexes(string $role): array |
||
376 | } |
||
377 | |||
378 | /** |
||
379 | * Get relation map associated with the given class. |
||
380 | * |
||
381 | * @param string $entity |
||
382 | * @return RelationMap |
||
383 | */ |
||
384 | protected function getRelmap($entity): RelationMap |
||
385 | { |
||
399 | } |
||
400 | } |
||
401 |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.