Complex classes like XmlDriver 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 XmlDriver, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class XmlDriver extends FileDriver |
||
29 | { |
||
30 | public const DEFAULT_FILE_EXTENSION = '.dcm.xml'; |
||
31 | |||
32 | /** |
||
33 | * {@inheritDoc} |
||
34 | */ |
||
35 | 14 | public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) |
|
39 | |||
40 | /** |
||
41 | * {@inheritDoc} |
||
42 | */ |
||
43 | 8 | public function loadMetadataForClass($className, \Doctrine\Common\Persistence\Mapping\ClassMetadata $class) |
|
44 | { |
||
45 | /* @var $class ClassMetadata */ |
||
46 | /* @var $xmlRoot \SimpleXMLElement */ |
||
47 | 8 | $xmlRoot = $this->getElement($className); |
|
48 | 8 | if (! $xmlRoot) { |
|
49 | return; |
||
50 | } |
||
51 | |||
52 | 8 | if ($xmlRoot->getName() === 'document') { |
|
53 | 7 | if (isset($xmlRoot['repository-class'])) { |
|
54 | 7 | $class->setCustomRepositoryClass((string) $xmlRoot['repository-class']); |
|
55 | } |
||
56 | 3 | } elseif ($xmlRoot->getName() === 'mapped-superclass') { |
|
57 | 1 | $class->setCustomRepositoryClass( |
|
58 | 1 | isset($xmlRoot['repository-class']) ? (string) $xmlRoot['repository-class'] : null |
|
59 | ); |
||
60 | 1 | $class->isMappedSuperclass = true; |
|
|
|||
61 | 2 | } elseif ($xmlRoot->getName() === 'embedded-document') { |
|
62 | 1 | $class->isEmbeddedDocument = true; |
|
63 | 2 | } elseif ($xmlRoot->getName() === 'query-result-document') { |
|
64 | 1 | $class->isQueryResultDocument = true; |
|
65 | 1 | } elseif ($xmlRoot->getName() === 'gridfs-file') { |
|
66 | 1 | $class->isFile = true; |
|
67 | } |
||
68 | |||
69 | 8 | if (isset($xmlRoot['db'])) { |
|
70 | 4 | $class->setDatabase((string) $xmlRoot['db']); |
|
71 | } |
||
72 | |||
73 | 8 | if (isset($xmlRoot['collection'])) { |
|
74 | 6 | if (isset($xmlRoot['capped-collection'])) { |
|
75 | $config = ['name' => (string) $xmlRoot['collection']]; |
||
76 | $config['capped'] = (bool) $xmlRoot['capped-collection']; |
||
77 | if (isset($xmlRoot['capped-collection-max'])) { |
||
78 | $config['max'] = (int) $xmlRoot['capped-collection-max']; |
||
79 | } |
||
80 | if (isset($xmlRoot['capped-collection-size'])) { |
||
81 | $config['size'] = (int) $xmlRoot['capped-collection-size']; |
||
82 | } |
||
83 | $class->setCollection($config); |
||
84 | } else { |
||
85 | 6 | $class->setCollection((string) $xmlRoot['collection']); |
|
86 | } |
||
87 | } |
||
88 | 8 | if (isset($xmlRoot['bucket'])) { |
|
89 | $class->setCollection((string) $xmlRoot['bucket']); |
||
90 | } |
||
91 | 8 | if (isset($xmlRoot['writeConcern'])) { |
|
92 | $class->setWriteConcern((string) $xmlRoot['writeConcern']); |
||
93 | } |
||
94 | 8 | if (isset($xmlRoot['inheritance-type'])) { |
|
95 | $inheritanceType = (string) $xmlRoot['inheritance-type']; |
||
96 | $class->setInheritanceType(constant(ClassMetadata::class . '::INHERITANCE_TYPE_' . $inheritanceType)); |
||
97 | } |
||
98 | 8 | if (isset($xmlRoot['change-tracking-policy'])) { |
|
99 | 1 | $class->setChangeTrackingPolicy(constant(ClassMetadata::class . '::CHANGETRACKING_' . strtoupper((string) $xmlRoot['change-tracking-policy']))); |
|
100 | } |
||
101 | 8 | if (isset($xmlRoot->{'discriminator-field'})) { |
|
102 | $discrField = $xmlRoot->{'discriminator-field'}; |
||
103 | /* XSD only allows for "name", which is consistent with association |
||
104 | * configurations, but fall back to "fieldName" for BC. |
||
105 | */ |
||
106 | $class->setDiscriminatorField( |
||
107 | (string) ($discrField['name'] ?? $discrField['fieldName']) |
||
108 | ); |
||
109 | } |
||
110 | 8 | if (isset($xmlRoot->{'discriminator-map'})) { |
|
111 | $map = []; |
||
112 | foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} as $discrMapElement) { |
||
113 | $map[(string) $discrMapElement['value']] = (string) $discrMapElement['class']; |
||
114 | } |
||
115 | $class->setDiscriminatorMap($map); |
||
116 | } |
||
117 | 8 | if (isset($xmlRoot->{'default-discriminator-value'})) { |
|
118 | $class->setDefaultDiscriminatorValue((string) $xmlRoot->{'default-discriminator-value'}['value']); |
||
119 | } |
||
120 | 8 | if (isset($xmlRoot->{'indexes'})) { |
|
121 | 2 | foreach ($xmlRoot->{'indexes'}->{'index'} as $index) { |
|
122 | 2 | $this->addIndex($class, $index); |
|
123 | } |
||
124 | } |
||
125 | 8 | if (isset($xmlRoot->{'shard-key'})) { |
|
126 | $this->setShardKey($class, $xmlRoot->{'shard-key'}[0]); |
||
127 | } |
||
128 | 8 | if (isset($xmlRoot['read-only']) && (string) $xmlRoot['read-only'] === 'true') { |
|
129 | $class->markReadOnly(); |
||
130 | } |
||
131 | 8 | if (isset($xmlRoot->{'read-preference'})) { |
|
132 | $class->setReadPreference(...$this->transformReadPreference($xmlRoot->{'read-preference'})); |
||
133 | } |
||
134 | 8 | if (isset($xmlRoot->field)) { |
|
135 | 8 | foreach ($xmlRoot->field as $field) { |
|
136 | 8 | $mapping = []; |
|
137 | 8 | $attributes = $field->attributes(); |
|
138 | 8 | foreach ($attributes as $key => $value) { |
|
139 | 8 | $mapping[$key] = (string) $value; |
|
140 | 8 | $booleanAttributes = ['id', 'reference', 'embed', 'unique', 'sparse']; |
|
141 | 8 | if (! in_array($key, $booleanAttributes)) { |
|
142 | 8 | continue; |
|
143 | } |
||
144 | |||
145 | 8 | $mapping[$key] = ($mapping[$key] === 'true'); |
|
146 | } |
||
147 | 8 | if (isset($mapping['id']) && $mapping['id'] === true && isset($mapping['strategy'])) { |
|
148 | 2 | $mapping['options'] = []; |
|
149 | 2 | if (isset($field->{'id-generator-option'})) { |
|
150 | 1 | foreach ($field->{'id-generator-option'} as $generatorOptions) { |
|
151 | 1 | $attributesGenerator = iterator_to_array($generatorOptions->attributes()); |
|
152 | 1 | if (! isset($attributesGenerator['name']) || ! isset($attributesGenerator['value'])) { |
|
153 | continue; |
||
154 | } |
||
155 | |||
156 | 1 | $mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value']; |
|
157 | } |
||
158 | } |
||
159 | } |
||
160 | |||
161 | 8 | if (isset($attributes['not-saved'])) { |
|
162 | 1 | $mapping['notSaved'] = ((string) $attributes['not-saved'] === 'true'); |
|
163 | } |
||
164 | |||
165 | 8 | if (isset($attributes['also-load'])) { |
|
166 | $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']); |
||
167 | 8 | } elseif (isset($attributes['version'])) { |
|
168 | $mapping['version'] = ((string) $attributes['version'] === 'true'); |
||
169 | 8 | } elseif (isset($attributes['lock'])) { |
|
170 | $mapping['lock'] = ((string) $attributes['lock'] === 'true'); |
||
171 | } |
||
172 | |||
173 | 8 | $this->addFieldMapping($class, $mapping); |
|
174 | } |
||
175 | } |
||
176 | 8 | if (isset($xmlRoot->{'embed-one'})) { |
|
177 | 2 | foreach ($xmlRoot->{'embed-one'} as $embed) { |
|
178 | 2 | $this->addEmbedMapping($class, $embed, 'one'); |
|
179 | } |
||
180 | } |
||
181 | 8 | if (isset($xmlRoot->{'embed-many'})) { |
|
182 | 1 | foreach ($xmlRoot->{'embed-many'} as $embed) { |
|
183 | 1 | $this->addEmbedMapping($class, $embed, 'many'); |
|
184 | } |
||
185 | } |
||
186 | 8 | if (isset($xmlRoot->{'reference-many'})) { |
|
187 | 3 | foreach ($xmlRoot->{'reference-many'} as $reference) { |
|
188 | 3 | $this->addReferenceMapping($class, $reference, 'many'); |
|
189 | } |
||
190 | } |
||
191 | 8 | if (isset($xmlRoot->{'reference-one'})) { |
|
192 | 2 | foreach ($xmlRoot->{'reference-one'} as $reference) { |
|
193 | 2 | $this->addReferenceMapping($class, $reference, 'one'); |
|
194 | } |
||
195 | } |
||
196 | 8 | if (isset($xmlRoot->{'lifecycle-callbacks'})) { |
|
197 | 1 | foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) { |
|
198 | 1 | $class->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\ODM\MongoDB\Events::' . (string) $lifecycleCallback['type'])); |
|
199 | } |
||
200 | } |
||
201 | 8 | if (! isset($xmlRoot->{'also-load-methods'})) { |
|
202 | 8 | return; |
|
203 | } |
||
204 | |||
205 | 1 | foreach ($xmlRoot->{'also-load-methods'}->{'also-load-method'} as $alsoLoadMethod) { |
|
206 | 1 | $class->registerAlsoLoadMethod((string) $alsoLoadMethod['method'], (string) $alsoLoadMethod['field']); |
|
207 | } |
||
208 | 1 | } |
|
209 | |||
210 | 9 | private function addFieldMapping(ClassMetadata $class, $mapping) |
|
211 | { |
||
212 | 9 | if (isset($mapping['name'])) { |
|
213 | 9 | $name = $mapping['name']; |
|
214 | 1 | } elseif (isset($mapping['fieldName'])) { |
|
215 | 1 | $name = $mapping['fieldName']; |
|
216 | } else { |
||
217 | throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping'); |
||
218 | } |
||
219 | |||
220 | 9 | $class->mapField($mapping); |
|
221 | |||
222 | // Index this field if either "index", "unique", or "sparse" are set |
||
223 | 9 | if (! (isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) { |
|
224 | 9 | return; |
|
225 | } |
||
226 | |||
227 | 1 | $keys = [$name => $mapping['order'] ?? 'asc']; |
|
228 | 1 | $options = []; |
|
229 | |||
230 | 1 | if (isset($mapping['background'])) { |
|
231 | $options['background'] = (bool) $mapping['background']; |
||
232 | } |
||
233 | 1 | if (isset($mapping['drop-dups'])) { |
|
234 | $options['dropDups'] = (bool) $mapping['drop-dups']; |
||
235 | } |
||
236 | 1 | if (isset($mapping['index-name'])) { |
|
237 | $options['name'] = (string) $mapping['index-name']; |
||
238 | } |
||
239 | 1 | if (isset($mapping['sparse'])) { |
|
240 | 1 | $options['sparse'] = (bool) $mapping['sparse']; |
|
241 | } |
||
242 | 1 | if (isset($mapping['unique'])) { |
|
243 | 1 | $options['unique'] = (bool) $mapping['unique']; |
|
244 | } |
||
245 | |||
246 | 1 | $class->addIndex($keys, $options); |
|
247 | 1 | } |
|
248 | |||
249 | 2 | private function addEmbedMapping(ClassMetadata $class, $embed, $type) |
|
285 | |||
286 | 4 | private function addReferenceMapping(ClassMetadata $class, $reference, $type) |
|
287 | { |
||
288 | 4 | $cascade = array_keys((array) $reference->cascade); |
|
289 | 4 | if (count($cascade) === 1) { |
|
290 | 1 | $cascade = current($cascade) ?: next($cascade); |
|
291 | } |
||
292 | 4 | $attributes = $reference->attributes(); |
|
293 | 4 | $defaultStrategy = $type === 'one' ? ClassMetadata::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY; |
|
294 | $mapping = [ |
||
295 | 4 | 'cascade' => $cascade, |
|
296 | 4 | 'orphanRemoval' => isset($attributes['orphan-removal']) ? ((string) $attributes['orphan-removal'] === 'true') : false, |
|
297 | 4 | 'type' => $type, |
|
298 | 'reference' => true, |
||
299 | 4 | 'storeAs' => (string) ($attributes['store-as'] ?? ClassMetadata::REFERENCE_STORE_AS_DB_REF), |
|
300 | 4 | 'targetDocument' => isset($attributes['target-document']) ? (string) $attributes['target-document'] : null, |
|
301 | 4 | 'collectionClass' => isset($attributes['collection-class']) ? (string) $attributes['collection-class'] : null, |
|
302 | 4 | 'name' => (string) $attributes['field'], |
|
303 | 4 | 'strategy' => (string) ($attributes['strategy'] ?? $defaultStrategy), |
|
304 | 4 | 'inversedBy' => isset($attributes['inversed-by']) ? (string) $attributes['inversed-by'] : null, |
|
305 | 4 | 'mappedBy' => isset($attributes['mapped-by']) ? (string) $attributes['mapped-by'] : null, |
|
306 | 4 | 'repositoryMethod' => isset($attributes['repository-method']) ? (string) $attributes['repository-method'] : null, |
|
307 | 4 | 'limit' => isset($attributes['limit']) ? (int) $attributes['limit'] : null, |
|
308 | 4 | 'skip' => isset($attributes['skip']) ? (int) $attributes['skip'] : null, |
|
309 | 'prime' => [], |
||
310 | ]; |
||
311 | |||
312 | 4 | if (isset($attributes['fieldName'])) { |
|
313 | $mapping['fieldName'] = (string) $attributes['fieldName']; |
||
314 | } |
||
315 | 4 | if (isset($reference->{'discriminator-field'})) { |
|
316 | $attr = $reference->{'discriminator-field'}; |
||
317 | $mapping['discriminatorField'] = (string) $attr['name']; |
||
318 | } |
||
319 | 4 | if (isset($reference->{'discriminator-map'})) { |
|
320 | foreach ($reference->{'discriminator-map'}->{'discriminator-mapping'} as $discriminatorMapping) { |
||
321 | $attr = $discriminatorMapping->attributes(); |
||
322 | $mapping['discriminatorMap'][(string) $attr['value']] = (string) $attr['class']; |
||
323 | } |
||
324 | } |
||
325 | 4 | if (isset($reference->{'default-discriminator-value'})) { |
|
326 | $mapping['defaultDiscriminatorValue'] = (string) $reference->{'default-discriminator-value'}['value']; |
||
327 | } |
||
328 | 4 | if (isset($reference->{'sort'})) { |
|
329 | foreach ($reference->{'sort'}->{'sort'} as $sort) { |
||
330 | $attr = $sort->attributes(); |
||
331 | $mapping['sort'][(string) $attr['field']] = (string) ($attr['order'] ?? 'asc'); |
||
332 | } |
||
333 | } |
||
334 | 4 | if (isset($reference->{'criteria'})) { |
|
335 | foreach ($reference->{'criteria'}->{'criteria'} as $criteria) { |
||
336 | $attr = $criteria->attributes(); |
||
337 | $mapping['criteria'][(string) $attr['field']] = (string) $attr['value']; |
||
338 | } |
||
339 | } |
||
340 | 4 | if (isset($attributes['not-saved'])) { |
|
341 | $mapping['notSaved'] = ((string) $attributes['not-saved'] === 'true'); |
||
342 | } |
||
343 | 4 | if (isset($attributes['also-load'])) { |
|
344 | $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']); |
||
345 | } |
||
346 | 4 | if (isset($reference->{'prime'})) { |
|
347 | 1 | foreach ($reference->{'prime'}->{'field'} as $field) { |
|
348 | 1 | $attr = $field->attributes(); |
|
349 | 1 | $mapping['prime'][] = (string) $attr['name']; |
|
350 | } |
||
351 | } |
||
352 | |||
353 | 4 | $this->addFieldMapping($class, $mapping); |
|
354 | 4 | } |
|
355 | |||
356 | 2 | private function addIndex(ClassMetadata $class, \SimpleXmlElement $xmlIndex) |
|
425 | |||
426 | 2 | private function getPartialFilterExpression(\SimpleXMLElement $fields) |
|
427 | { |
||
428 | 2 | $partialFilterExpression = []; |
|
460 | |||
461 | 1 | private function setShardKey(ClassMetadata $class, \SimpleXmlElement $xmlShardkey) |
|
495 | |||
496 | /** |
||
497 | * Parses <read-preference> to a format suitable for the underlying driver. |
||
498 | * |
||
499 | * list($readPreference, $tags) = $this->transformReadPreference($xml->{read-preference}); |
||
500 | * |
||
501 | * @param \SimpleXMLElement $xmlReadPreference |
||
502 | * @return array |
||
503 | */ |
||
504 | private function transformReadPreference($xmlReadPreference) |
||
519 | |||
520 | /** |
||
521 | * {@inheritDoc} |
||
522 | */ |
||
523 | 8 | protected function loadMappingFile($file) |
|
541 | } |
||
542 |
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: