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 |
||
29 | class ORM extends Component implements ORMInterface, SingletonInterface |
||
30 | { |
||
31 | /** |
||
32 | * Memory section to store ORM schema. |
||
33 | */ |
||
34 | const MEMORY = 'orm.schema'; |
||
35 | |||
36 | /** |
||
37 | * @invisible |
||
38 | * @var EntityMap|null |
||
39 | */ |
||
40 | private $map = null; |
||
41 | |||
42 | /** |
||
43 | * @var LocatorInterface |
||
44 | */ |
||
45 | private $locator; |
||
46 | |||
47 | /** |
||
48 | * Already created instantiators. |
||
49 | * |
||
50 | * @invisible |
||
51 | * @var InstantiatorInterface[] |
||
52 | */ |
||
53 | private $instantiators = []; |
||
54 | |||
55 | /** |
||
56 | * ORM schema. |
||
57 | * |
||
58 | * @invisible |
||
59 | * @var array |
||
60 | */ |
||
61 | private $schema = []; |
||
62 | |||
63 | /** |
||
64 | * @var DatabaseManager |
||
65 | */ |
||
66 | protected $manager; |
||
67 | |||
68 | /** |
||
69 | * @var RelationsConfig |
||
70 | */ |
||
71 | protected $config; |
||
72 | |||
73 | /** |
||
74 | * @invisible |
||
75 | * @var MemoryInterface |
||
76 | */ |
||
77 | protected $memory; |
||
78 | |||
79 | /** |
||
80 | * Container defines working scope for all Documents and DocumentEntities. |
||
81 | * |
||
82 | * @var ContainerInterface |
||
83 | */ |
||
84 | protected $container; |
||
85 | |||
86 | /** |
||
87 | * @param DatabaseManager $manager |
||
88 | * @param RelationsConfig $config |
||
89 | * @param LocatorInterface|null $locator |
||
90 | * @param EntityMap|null $map |
||
91 | * @param MemoryInterface|null $memory |
||
92 | * @param ContainerInterface|null $container |
||
93 | */ |
||
94 | public function __construct( |
||
95 | DatabaseManager $manager, |
||
96 | RelationsConfig $config, |
||
97 | //Following arguments can be resolved automatically |
||
98 | LocatorInterface $locator = null, |
||
99 | EntityMap $map = null, |
||
100 | MemoryInterface $memory = null, |
||
101 | ContainerInterface $container = null |
||
102 | ) { |
||
103 | $this->manager = $manager; |
||
104 | $this->config = $config; |
||
105 | |||
106 | //If null is passed = no caching is expected |
||
107 | $this->map = $map; |
||
108 | |||
109 | $this->locator = $locator ?? new NullLocator(); |
||
110 | $this->memory = $memory ?? new NullMemory(); |
||
111 | $this->container = $container ?? new Container(); |
||
112 | |||
113 | //Loading schema from memory (if any) |
||
114 | $this->schema = $this->loadSchema(); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | public function hasMap(): bool |
||
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | public function getMap(): EntityMap |
||
136 | |||
137 | /** |
||
138 | * {@inheritdoc} |
||
139 | */ |
||
140 | public function withMap(EntityMap $map = null): ORMInterface |
||
147 | |||
148 | /** |
||
149 | * Create instance of ORM SchemaBuilder. |
||
150 | * |
||
151 | * @param bool $locate Set to true to automatically locate available records and record sources |
||
152 | * sources in a project files (based on tokenizer scope). |
||
153 | * |
||
154 | * @return SchemaBuilder |
||
155 | * |
||
156 | * @throws SchemaException |
||
157 | */ |
||
158 | public function schemaBuilder(bool $locate = true): SchemaBuilder |
||
177 | |||
178 | /** |
||
179 | * Specify behaviour schema for ORM to be used. Attention, you have to call renderSchema() |
||
180 | * prior to passing builder into this method. |
||
181 | * |
||
182 | * @param SchemaBuilder $builder |
||
183 | * @param bool $remember Set to true to remember packed schema in memory. |
||
184 | */ |
||
185 | public function setSchema(SchemaBuilder $builder, bool $remember = false) |
||
186 | { |
||
187 | $this->schema = $builder->packSchema(); |
||
188 | |||
189 | if ($remember) { |
||
190 | $this->memory->saveData(static::MEMORY, $this->schema); |
||
191 | } |
||
192 | } |
||
193 | |||
194 | /** |
||
195 | * {@inheritdoc} |
||
196 | */ |
||
197 | public function define(string $class, int $property) |
||
214 | |||
215 | /** |
||
216 | * Get source (selection repository) for specific entity class. |
||
217 | * |
||
218 | * @param string $class |
||
219 | * |
||
220 | * @return RecordSource |
||
221 | */ |
||
222 | public function source(string $class): RecordSource |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | * |
||
246 | * @param bool $isolated Set to true (by default) to create new isolated entity map for |
||
247 | * selection. |
||
248 | */ |
||
249 | public function selector(string $class, bool $isolated = true): RecordSelector |
||
254 | |||
255 | /** |
||
256 | * {@inheritdoc} |
||
257 | */ |
||
258 | public function table(string $class): Table |
||
266 | |||
267 | /** |
||
268 | * {@inheritdoc} |
||
269 | */ |
||
270 | public function database(string $alias = null): Database |
||
274 | |||
275 | /** |
||
276 | * {@inheritdoc} |
||
277 | */ |
||
278 | public function make( |
||
315 | |||
316 | /** |
||
317 | * {@inheritdoc} |
||
318 | */ |
||
319 | public function makeLoader(string $class, string $relation): LoaderInterface |
||
344 | |||
345 | /** |
||
346 | * {@inheritdoc} |
||
347 | */ |
||
348 | public function makeRelation(string $class, string $relation): RelationInterface |
||
372 | |||
373 | /** |
||
374 | * When ORM is cloned we are automatically cloning it's cache as well to create |
||
375 | * new isolated area. Basically we have cache enabled per selection. |
||
376 | * |
||
377 | * @see RecordSelector::getIterator() |
||
378 | */ |
||
379 | public function __clone() |
||
386 | |||
387 | /** |
||
388 | * Get object responsible for class instantiation. |
||
389 | * |
||
390 | * @param string $class |
||
391 | * |
||
392 | * @return InstantiatorInterface |
||
393 | */ |
||
394 | protected function instantiator(string $class): InstantiatorInterface |
||
413 | |||
414 | /** |
||
415 | * Load packed schema from memory. |
||
416 | * |
||
417 | * @return array |
||
418 | */ |
||
419 | protected function loadSchema(): array |
||
423 | |||
424 | /** |
||
425 | * Get ODM specific factory. |
||
426 | * |
||
427 | * @return FactoryInterface |
||
428 | */ |
||
429 | protected function getFactory(): FactoryInterface |
||
437 | } |
||
438 |