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. 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 ORM, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
27 | class ORM extends Component implements ORMInterface, SingletonInterface |
||
28 | { |
||
29 | /** |
||
30 | * Memory section to store ORM schema. |
||
31 | */ |
||
32 | const MEMORY = 'orm.schema'; |
||
33 | |||
34 | /** |
||
35 | * @invisible |
||
36 | * @var EntityMap|null |
||
37 | */ |
||
38 | private $map = null; |
||
39 | |||
40 | /** |
||
41 | * @var LocatorInterface |
||
42 | */ |
||
43 | private $locator; |
||
44 | |||
45 | /** |
||
46 | * Already created instantiators. |
||
47 | * |
||
48 | * @invisible |
||
49 | * @var InstantiatorInterface[] |
||
50 | */ |
||
51 | private $instantiators = []; |
||
52 | |||
53 | /** |
||
54 | * ORM schema. |
||
55 | * |
||
56 | * @invisible |
||
57 | * @var array |
||
58 | */ |
||
59 | private $schema = []; |
||
60 | |||
61 | /** |
||
62 | * @var DatabaseManager |
||
63 | */ |
||
64 | protected $manager; |
||
65 | |||
66 | /** |
||
67 | * @var RelationsConfig |
||
68 | */ |
||
69 | protected $config; |
||
70 | |||
71 | /** |
||
72 | * @invisible |
||
73 | * @var MemoryInterface |
||
74 | */ |
||
75 | protected $memory; |
||
76 | |||
77 | /** |
||
78 | * Container defines working scope for all Documents and DocumentEntities. |
||
79 | * |
||
80 | * @var ContainerInterface |
||
81 | */ |
||
82 | protected $container; |
||
83 | |||
84 | /** |
||
85 | * @param DatabaseManager $manager |
||
86 | * @param RelationsConfig $config |
||
87 | * @param LocatorInterface|null $locator |
||
88 | * @param EntityMap|null $map |
||
89 | * @param MemoryInterface|null $memory |
||
90 | * @param ContainerInterface|null $container |
||
91 | */ |
||
92 | public function __construct( |
||
114 | |||
115 | /** |
||
116 | * Check if ORM has associated entity cache. |
||
117 | * |
||
118 | * @return bool |
||
119 | */ |
||
120 | public function hasMap(): bool |
||
124 | |||
125 | /** |
||
126 | * Get associated entity map. |
||
127 | * |
||
128 | * @return EntityMap |
||
129 | */ |
||
130 | public function getMap(): EntityMap |
||
134 | |||
135 | /** |
||
136 | * Create version of ORM with different initial map or disable caching. |
||
137 | * |
||
138 | * @param EntityMap|null $map |
||
139 | * |
||
140 | * @return ORM |
||
141 | */ |
||
142 | public function withMap(EntityMap $map = null): ORM |
||
149 | |||
150 | /** |
||
151 | * Create instance of ORM SchemaBuilder. |
||
152 | * |
||
153 | * @param bool $locate Set to true to automatically locate available records and record sources |
||
154 | * sources in a project files (based on tokenizer scope). |
||
155 | * |
||
156 | * @return SchemaBuilder |
||
157 | * |
||
158 | * @throws SchemaException |
||
159 | */ |
||
160 | public function schemaBuilder(bool $locate = true): SchemaBuilder |
||
179 | |||
180 | /** |
||
181 | * Specify behaviour schema for ORM to be used. Attention, you have to call renderSchema() |
||
182 | * prior to passing builder into this method. |
||
183 | * |
||
184 | * @param SchemaBuilder $builder |
||
185 | * @param bool $remember Set to true to remember packed schema in memory. |
||
186 | */ |
||
187 | public function buildSchema(SchemaBuilder $builder, bool $remember = false) |
||
195 | |||
196 | /** |
||
197 | * {@inheritdoc} |
||
198 | */ |
||
199 | public function define(string $class, int $property) |
||
216 | |||
217 | /** |
||
218 | * Get source (selection repository) for specific entity class. |
||
219 | * |
||
220 | * @param string $class |
||
221 | * |
||
222 | * @return RecordSource |
||
223 | */ |
||
224 | public function source(string $class): RecordSource |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | * |
||
248 | * @param bool $isolated Set to true (by default) to create new isolated entity map for |
||
249 | * selection. |
||
250 | */ |
||
251 | public function selector(string $class, bool $isolated = true): RecordSelector |
||
256 | |||
257 | /** |
||
258 | * {@inheritdoc} |
||
259 | */ |
||
260 | public function table(string $class): Table |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | public function make( |
||
309 | |||
310 | /** |
||
311 | * {@inheritdoc} |
||
312 | */ |
||
313 | public function makeLoader(string $class, string $relation): LoaderInterface |
||
338 | |||
339 | /** |
||
340 | * {@inheritdoc} |
||
341 | */ |
||
342 | public function makeRelation(string $class, string $relation): RelationInterface |
||
366 | |||
367 | /** |
||
368 | * When ORM is cloned we are automatically cloning it's cache as well to create |
||
369 | * new isolated area. Basically we have cache enabled per selection. |
||
370 | * |
||
371 | * @see RecordSelector::getIterator() |
||
372 | */ |
||
373 | public function __clone() |
||
380 | |||
381 | /** |
||
382 | * Get object responsible for class instantiation. |
||
383 | * |
||
384 | * @param string $class |
||
385 | * |
||
386 | * @return InstantiatorInterface |
||
387 | */ |
||
388 | protected function instantiator(string $class): InstantiatorInterface |
||
407 | |||
408 | /** |
||
409 | * Load packed schema from memory. |
||
410 | * |
||
411 | * @return array |
||
412 | */ |
||
413 | protected function loadSchema(): array |
||
417 | |||
418 | /** |
||
419 | * Get ODM specific factory. |
||
420 | * |
||
421 | * @return FactoryInterface |
||
422 | */ |
||
423 | protected function getFactory(): FactoryInterface |
||
431 | } |
||
432 |