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 |
||
38 | class XmlDriver extends FileDriver |
||
39 | { |
||
40 | public const DEFAULT_FILE_EXTENSION = '.dcm.xml'; |
||
41 | |||
42 | private const DEFAULT_GRIDFS_MAPPINGS = [ |
||
43 | 'length' => [ |
||
44 | 'name' => 'length', |
||
45 | 'type' => 'int', |
||
46 | 'notSaved' => true, |
||
47 | ], |
||
48 | 'chunk-size' => [ |
||
49 | 'name' => 'chunkSize', |
||
50 | 'type' => 'int', |
||
51 | 'notSaved' => true, |
||
52 | ], |
||
53 | 'filename' => [ |
||
54 | 'name' => 'filename', |
||
55 | 'type' => 'string', |
||
56 | 'notSaved' => true, |
||
57 | ], |
||
58 | 'upload-date' => [ |
||
59 | 'name' => 'uploadDate', |
||
60 | 'type' => 'date', |
||
61 | 'notSaved' => true, |
||
62 | ], |
||
63 | ]; |
||
64 | |||
65 | /** |
||
66 | * {@inheritDoc} |
||
67 | */ |
||
68 | 16 | public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) |
|
72 | |||
73 | /** |
||
74 | * {@inheritDoc} |
||
75 | */ |
||
76 | 11 | public function loadMetadataForClass($className, \Doctrine\Common\Persistence\Mapping\ClassMetadata $class) |
|
77 | { |
||
78 | /** @var ClassMetadata $class */ |
||
79 | /** @var SimpleXMLElement $xmlRoot */ |
||
80 | 11 | $xmlRoot = $this->getElement($className); |
|
81 | 9 | if (! $xmlRoot) { |
|
82 | return; |
||
83 | } |
||
84 | |||
85 | 9 | if ($xmlRoot->getName() === 'document') { |
|
86 | 8 | if (isset($xmlRoot['repository-class'])) { |
|
87 | 8 | $class->setCustomRepositoryClass((string) $xmlRoot['repository-class']); |
|
88 | } |
||
89 | 3 | } elseif ($xmlRoot->getName() === 'mapped-superclass') { |
|
90 | 1 | $class->setCustomRepositoryClass( |
|
91 | 1 | isset($xmlRoot['repository-class']) ? (string) $xmlRoot['repository-class'] : null |
|
92 | ); |
||
93 | 1 | $class->isMappedSuperclass = true; |
|
94 | 2 | } elseif ($xmlRoot->getName() === 'embedded-document') { |
|
95 | 1 | $class->isEmbeddedDocument = true; |
|
96 | 2 | } elseif ($xmlRoot->getName() === 'query-result-document') { |
|
97 | 1 | $class->isQueryResultDocument = true; |
|
98 | 1 | } elseif ($xmlRoot->getName() === 'gridfs-file') { |
|
99 | 1 | $class->isFile = true; |
|
100 | |||
101 | 1 | if (isset($xmlRoot['chunk-size-bytes'])) { |
|
102 | 1 | $class->setChunkSizeBytes((int) $xmlRoot['chunk-size-bytes']); |
|
103 | } |
||
104 | } |
||
105 | |||
106 | 9 | if (isset($xmlRoot['db'])) { |
|
107 | 3 | $class->setDatabase((string) $xmlRoot['db']); |
|
108 | } |
||
109 | |||
110 | 9 | if (isset($xmlRoot['collection'])) { |
|
111 | 5 | if (isset($xmlRoot['capped-collection'])) { |
|
112 | $config = ['name' => (string) $xmlRoot['collection']]; |
||
113 | $config['capped'] = (bool) $xmlRoot['capped-collection']; |
||
114 | if (isset($xmlRoot['capped-collection-max'])) { |
||
115 | $config['max'] = (int) $xmlRoot['capped-collection-max']; |
||
116 | } |
||
117 | if (isset($xmlRoot['capped-collection-size'])) { |
||
118 | $config['size'] = (int) $xmlRoot['capped-collection-size']; |
||
119 | } |
||
120 | $class->setCollection($config); |
||
121 | } else { |
||
122 | 5 | $class->setCollection((string) $xmlRoot['collection']); |
|
123 | } |
||
124 | } |
||
125 | 9 | if (isset($xmlRoot['bucket-name'])) { |
|
126 | $class->setBucketName((string) $xmlRoot['bucket-name']); |
||
127 | } |
||
128 | 9 | if (isset($xmlRoot['write-concern'])) { |
|
129 | $class->setWriteConcern((string) $xmlRoot['write-concern']); |
||
130 | } |
||
131 | 9 | if (isset($xmlRoot['inheritance-type'])) { |
|
132 | $inheritanceType = (string) $xmlRoot['inheritance-type']; |
||
133 | $class->setInheritanceType(constant(ClassMetadata::class . '::INHERITANCE_TYPE_' . $inheritanceType)); |
||
134 | } |
||
135 | 9 | if (isset($xmlRoot['change-tracking-policy'])) { |
|
136 | 1 | $class->setChangeTrackingPolicy(constant(ClassMetadata::class . '::CHANGETRACKING_' . strtoupper((string) $xmlRoot['change-tracking-policy']))); |
|
137 | } |
||
138 | 9 | if (isset($xmlRoot->{'discriminator-field'})) { |
|
139 | $discrField = $xmlRoot->{'discriminator-field'}; |
||
140 | $class->setDiscriminatorField((string) $discrField['name']); |
||
141 | } |
||
142 | 9 | if (isset($xmlRoot->{'discriminator-map'})) { |
|
143 | $map = []; |
||
144 | foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} as $discrMapElement) { |
||
145 | $map[(string) $discrMapElement['value']] = (string) $discrMapElement['class']; |
||
146 | } |
||
147 | $class->setDiscriminatorMap($map); |
||
148 | } |
||
149 | 9 | if (isset($xmlRoot->{'default-discriminator-value'})) { |
|
150 | $class->setDefaultDiscriminatorValue((string) $xmlRoot->{'default-discriminator-value'}['value']); |
||
151 | } |
||
152 | 9 | if (isset($xmlRoot->{'indexes'})) { |
|
153 | 1 | foreach ($xmlRoot->{'indexes'}->{'index'} as $index) { |
|
154 | 1 | $this->addIndex($class, $index); |
|
155 | } |
||
156 | } |
||
157 | 9 | if (isset($xmlRoot->{'shard-key'})) { |
|
158 | $this->setShardKey($class, $xmlRoot->{'shard-key'}[0]); |
||
159 | } |
||
160 | 9 | if (isset($xmlRoot['read-only']) && (string) $xmlRoot['read-only'] === 'true') { |
|
161 | $class->markReadOnly(); |
||
162 | } |
||
163 | 9 | if (isset($xmlRoot->{'read-preference'})) { |
|
164 | $class->setReadPreference(...$this->transformReadPreference($xmlRoot->{'read-preference'})); |
||
165 | } |
||
166 | |||
167 | 9 | if (isset($xmlRoot->id)) { |
|
168 | 7 | $field = $xmlRoot->id; |
|
|
|||
169 | $mapping = [ |
||
170 | 7 | 'id' => true, |
|
171 | 'fieldName' => 'id', |
||
172 | ]; |
||
173 | |||
174 | 7 | $attributes = $field->attributes(); |
|
175 | 7 | foreach ($attributes as $key => $value) { |
|
176 | 2 | $mapping[$key] = (string) $value; |
|
177 | } |
||
178 | |||
179 | 7 | if (isset($mapping['strategy'])) { |
|
180 | 2 | $mapping['options'] = []; |
|
181 | 2 | if (isset($field->{'generator-option'})) { |
|
182 | 1 | foreach ($field->{'generator-option'} as $generatorOptions) { |
|
183 | 1 | $attributesGenerator = iterator_to_array($generatorOptions->attributes()); |
|
184 | 1 | if (! isset($attributesGenerator['name']) || ! isset($attributesGenerator['value'])) { |
|
185 | continue; |
||
186 | } |
||
187 | |||
188 | 1 | $mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value']; |
|
189 | } |
||
190 | } |
||
191 | } |
||
192 | |||
193 | 7 | $this->addFieldMapping($class, $mapping); |
|
194 | } |
||
195 | |||
196 | 9 | if (isset($xmlRoot->field)) { |
|
197 | 4 | foreach ($xmlRoot->field as $field) { |
|
198 | 4 | $mapping = []; |
|
199 | 4 | $attributes = $field->attributes(); |
|
200 | 4 | foreach ($attributes as $key => $value) { |
|
201 | 4 | $mapping[$key] = (string) $value; |
|
202 | 4 | $booleanAttributes = ['reference', 'embed', 'unique', 'sparse']; |
|
203 | 4 | if (! in_array($key, $booleanAttributes)) { |
|
204 | 4 | continue; |
|
205 | } |
||
206 | |||
207 | 1 | $mapping[$key] = ($mapping[$key] === 'true'); |
|
208 | } |
||
209 | |||
210 | 4 | if (isset($attributes['not-saved'])) { |
|
211 | 1 | $mapping['notSaved'] = ((string) $attributes['not-saved'] === 'true'); |
|
212 | } |
||
213 | |||
214 | 4 | if (isset($attributes['field-name'])) { |
|
215 | 2 | $mapping['fieldName'] = (string) $attributes['field-name']; |
|
216 | } |
||
217 | |||
218 | 4 | if (isset($attributes['also-load'])) { |
|
219 | $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']); |
||
220 | 4 | } elseif (isset($attributes['version'])) { |
|
221 | $mapping['version'] = ((string) $attributes['version'] === 'true'); |
||
222 | 4 | } elseif (isset($attributes['lock'])) { |
|
223 | $mapping['lock'] = ((string) $attributes['lock'] === 'true'); |
||
224 | } |
||
225 | |||
226 | 4 | $this->addFieldMapping($class, $mapping); |
|
227 | } |
||
228 | } |
||
229 | |||
230 | 8 | $this->addGridFSMappings($class, $xmlRoot); |
|
231 | |||
232 | 8 | if (isset($xmlRoot->{'embed-one'})) { |
|
233 | 1 | foreach ($xmlRoot->{'embed-one'} as $embed) { |
|
234 | 1 | $this->addEmbedMapping($class, $embed, 'one'); |
|
235 | } |
||
236 | } |
||
237 | 8 | if (isset($xmlRoot->{'embed-many'})) { |
|
238 | 1 | foreach ($xmlRoot->{'embed-many'} as $embed) { |
|
239 | 1 | $this->addEmbedMapping($class, $embed, 'many'); |
|
240 | } |
||
241 | } |
||
242 | 8 | if (isset($xmlRoot->{'reference-many'})) { |
|
243 | 3 | foreach ($xmlRoot->{'reference-many'} as $reference) { |
|
244 | 3 | $this->addReferenceMapping($class, $reference, 'many'); |
|
245 | } |
||
246 | } |
||
247 | 8 | if (isset($xmlRoot->{'reference-one'})) { |
|
248 | 2 | foreach ($xmlRoot->{'reference-one'} as $reference) { |
|
249 | 2 | $this->addReferenceMapping($class, $reference, 'one'); |
|
250 | } |
||
251 | } |
||
252 | 8 | if (isset($xmlRoot->{'lifecycle-callbacks'})) { |
|
253 | 1 | foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) { |
|
254 | 1 | $class->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\ODM\MongoDB\Events::' . (string) $lifecycleCallback['type'])); |
|
255 | } |
||
256 | } |
||
257 | 8 | if (! isset($xmlRoot->{'also-load-methods'})) { |
|
258 | 8 | return; |
|
259 | } |
||
260 | |||
261 | 1 | foreach ($xmlRoot->{'also-load-methods'}->{'also-load-method'} as $alsoLoadMethod) { |
|
262 | 1 | $class->registerAlsoLoadMethod((string) $alsoLoadMethod['method'], (string) $alsoLoadMethod['field']); |
|
263 | } |
||
264 | 1 | } |
|
265 | |||
266 | 9 | private function addFieldMapping(ClassMetadata $class, array $mapping) : void |
|
304 | |||
305 | 2 | private function addEmbedMapping(ClassMetadata $class, SimpleXMLElement $embed, string $type) : void |
|
341 | |||
342 | 3 | private function addReferenceMapping(ClassMetadata $class, $reference, string $type) : void |
|
411 | |||
412 | 1 | private function addIndex(ClassMetadata $class, SimpleXMLElement $xmlIndex) : void |
|
481 | |||
482 | 1 | private function getPartialFilterExpression(SimpleXMLElement $fields) : array |
|
516 | |||
517 | 1 | private function setShardKey(ClassMetadata $class, SimpleXMLElement $xmlShardkey) : void |
|
551 | |||
552 | /** |
||
553 | * Parses <read-preference> to a format suitable for the underlying driver. |
||
554 | * |
||
555 | * list($readPreference, $tags) = $this->transformReadPreference($xml->{read-preference}); |
||
556 | */ |
||
557 | private function transformReadPreference(SimpleXMLElement $xmlReadPreference) : array |
||
572 | |||
573 | /** |
||
574 | * {@inheritDoc} |
||
575 | */ |
||
576 | 11 | protected function loadMappingFile($file) : array |
|
597 | |||
598 | 11 | private function validateSchema(string $filename) : void |
|
615 | |||
616 | /** |
||
617 | * @param LibXMLError[] $xmlErrors |
||
618 | */ |
||
619 | 2 | private function formatErrors(array $xmlErrors) : string |
|
625 | |||
626 | 8 | private function addGridFSMappings(ClassMetadata $class, SimpleXMLElement $xmlRoot) : void |
|
651 | } |
||
652 |
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.