Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
34 | class XmlDriver extends FileDriver |
||
35 | { |
||
36 | const DEFAULT_FILE_EXTENSION = '.dcm.xml'; |
||
37 | |||
38 | /** |
||
39 | * {@inheritDoc} |
||
40 | */ |
||
41 | 10 | public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) |
|
42 | { |
||
43 | 10 | parent::__construct($locator, $fileExtension); |
|
44 | 10 | } |
|
45 | |||
46 | /** |
||
47 | * {@inheritDoc} |
||
48 | */ |
||
49 | 6 | public function loadMetadataForClass($className, ClassMetadata $class) |
|
50 | { |
||
51 | /* @var $class ClassMetadataInfo */ |
||
52 | /* @var $xmlRoot \SimpleXMLElement */ |
||
53 | 6 | $xmlRoot = $this->getElement($className); |
|
54 | 6 | if ( ! $xmlRoot) { |
|
55 | return; |
||
56 | } |
||
57 | |||
58 | 6 | if ($xmlRoot->getName() == 'document') { |
|
59 | 6 | if (isset($xmlRoot['repository-class'])) { |
|
60 | $class->setCustomRepositoryClass((string) $xmlRoot['repository-class']); |
||
61 | } |
||
62 | 6 | View Code Duplication | } elseif ($xmlRoot->getName() == 'mapped-superclass') { |
|
|||
63 | 2 | $class->setCustomRepositoryClass( |
|
64 | 2 | isset($xmlRoot['repository-class']) ? (string) $xmlRoot['repository-class'] : null |
|
65 | 2 | ); |
|
66 | 2 | $class->isMappedSuperclass = true; |
|
67 | 3 | } elseif ($xmlRoot->getName() == 'embedded-document') { |
|
68 | 1 | $class->isEmbeddedDocument = true; |
|
69 | 2 | } |
|
70 | 6 | if (isset($xmlRoot['db'])) { |
|
71 | 2 | $class->setDatabase((string) $xmlRoot['db']); |
|
72 | 2 | } |
|
73 | 6 | if (isset($xmlRoot['collection'])) { |
|
74 | 5 | if (isset($xmlRoot['capped-collection'])) { |
|
75 | $config = array('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 | 5 | $class->setCollection((string) $xmlRoot['collection']); |
|
86 | } |
||
87 | 5 | } |
|
88 | 6 | if (isset($xmlRoot['inheritance-type'])) { |
|
89 | $inheritanceType = (string) $xmlRoot['inheritance-type']; |
||
90 | $class->setInheritanceType(constant('Doctrine\ODM\MongoDB\Mapping\ClassMetadata::INHERITANCE_TYPE_' . $inheritanceType)); |
||
91 | } |
||
92 | 6 | View Code Duplication | if (isset($xmlRoot['change-tracking-policy'])) { |
93 | 2 | $class->setChangeTrackingPolicy(constant('Doctrine\ODM\MongoDB\Mapping\ClassMetadata::CHANGETRACKING_' . strtoupper((string) $xmlRoot['change-tracking-policy']))); |
|
94 | 2 | } |
|
95 | 6 | if (isset($xmlRoot->{'discriminator-field'})) { |
|
96 | 1 | $discrField = $xmlRoot->{'discriminator-field'}; |
|
97 | /* XSD only allows for "name", which is consistent with association |
||
98 | * configurations, but fall back to "fieldName" for BC. |
||
99 | */ |
||
100 | 1 | $class->setDiscriminatorField( |
|
101 | 1 | isset($discrField['name']) ? (string) $discrField['name'] : (string) $discrField['fieldName'] |
|
102 | 1 | ); |
|
103 | 1 | } |
|
104 | 6 | View Code Duplication | if (isset($xmlRoot->{'discriminator-map'})) { |
105 | 1 | $map = array(); |
|
106 | 1 | foreach ($xmlRoot->{'discriminator-map'}->{'discriminator-mapping'} AS $discrMapElement) { |
|
107 | 1 | $map[(string) $discrMapElement['value']] = (string) $discrMapElement['class']; |
|
108 | 1 | } |
|
109 | 1 | $class->setDiscriminatorMap($map); |
|
110 | 1 | } |
|
111 | 6 | if (isset($xmlRoot->{'default-discriminator-value'})) { |
|
112 | 1 | $class->setDefaultDiscriminatorValue((string) $xmlRoot->{'default-discriminator-value'}['value']); |
|
113 | 1 | } |
|
114 | 6 | if (isset($xmlRoot->{'indexes'})) { |
|
115 | 1 | foreach ($xmlRoot->{'indexes'}->{'index'} as $index) { |
|
116 | 1 | $this->addIndex($class, $index); |
|
117 | 1 | } |
|
118 | 1 | } |
|
119 | 6 | if (isset($xmlRoot['require-indexes'])) { |
|
120 | 1 | $class->setRequireIndexes('true' === (string) $xmlRoot['require-indexes']); |
|
121 | 1 | } |
|
122 | 6 | if (isset($xmlRoot['slave-okay'])) { |
|
123 | 1 | $class->setSlaveOkay('true' === (string) $xmlRoot['slave-okay']); |
|
124 | 1 | } |
|
125 | 6 | if (isset($xmlRoot->field)) { |
|
126 | 6 | foreach ($xmlRoot->field as $field) { |
|
127 | 6 | $mapping = array(); |
|
128 | 6 | $attributes = $field->attributes(); |
|
129 | 6 | foreach ($attributes as $key => $value) { |
|
130 | 6 | $mapping[$key] = (string) $value; |
|
131 | 6 | $booleanAttributes = array('id', 'reference', 'embed', 'unique', 'sparse', 'file', 'distance'); |
|
132 | 6 | if (in_array($key, $booleanAttributes)) { |
|
133 | 6 | $mapping[$key] = ('true' === $mapping[$key]); |
|
134 | 6 | } |
|
135 | 6 | } |
|
136 | 6 | if (isset($mapping['id']) && $mapping['id'] === true && isset($mapping['strategy'])) { |
|
137 | 3 | $mapping['options'] = array(); |
|
138 | 3 | if (isset($field->{'id-generator-option'})) { |
|
139 | 1 | foreach ($field->{'id-generator-option'} as $generatorOptions) { |
|
140 | 1 | $attributesGenerator = iterator_to_array($generatorOptions->attributes()); |
|
141 | 1 | if (isset($attributesGenerator['name']) && isset($attributesGenerator['value'])) { |
|
142 | 1 | $mapping['options'][(string) $attributesGenerator['name']] = (string) $attributesGenerator['value']; |
|
143 | 1 | } |
|
144 | 1 | } |
|
145 | 1 | } |
|
146 | 3 | } |
|
147 | |||
148 | 6 | if (isset($attributes['not-saved'])) { |
|
149 | $mapping['notSaved'] = ('true' === (string) $attributes['not-saved']); |
||
150 | } |
||
151 | |||
152 | 6 | if (isset($attributes['also-load'])) { |
|
153 | $mapping['alsoLoadFields'] = explode(',', $attributes['also-load']); |
||
154 | 6 | } elseif (isset($attributes['version'])) { |
|
155 | 1 | $mapping['version'] = ('true' === (string) $attributes['version']); |
|
156 | 6 | } elseif (isset($attributes['lock'])) { |
|
157 | 1 | $mapping['lock'] = ('true' === (string) $attributes['lock']); |
|
158 | 1 | } |
|
159 | |||
160 | 6 | $this->addFieldMapping($class, $mapping); |
|
161 | 6 | } |
|
162 | 6 | } |
|
163 | 6 | if (isset($xmlRoot->{'embed-one'})) { |
|
164 | 2 | foreach ($xmlRoot->{'embed-one'} as $embed) { |
|
165 | 2 | $this->addEmbedMapping($class, $embed, 'one'); |
|
166 | 2 | } |
|
167 | 2 | } |
|
168 | 6 | if (isset($xmlRoot->{'embed-many'})) { |
|
169 | 2 | foreach ($xmlRoot->{'embed-many'} as $embed) { |
|
170 | 2 | $this->addEmbedMapping($class, $embed, 'many'); |
|
171 | 2 | } |
|
172 | 2 | } |
|
173 | 6 | if (isset($xmlRoot->{'reference-many'})) { |
|
174 | 3 | foreach ($xmlRoot->{'reference-many'} as $reference) { |
|
175 | 3 | $this->addReferenceMapping($class, $reference, 'many'); |
|
176 | 3 | } |
|
177 | 3 | } |
|
178 | 6 | if (isset($xmlRoot->{'reference-one'})) { |
|
179 | 3 | foreach ($xmlRoot->{'reference-one'} as $reference) { |
|
180 | 3 | $this->addReferenceMapping($class, $reference, 'one'); |
|
181 | 3 | } |
|
182 | 3 | } |
|
183 | 6 | if (isset($xmlRoot->{'lifecycle-callbacks'})) { |
|
184 | 2 | foreach ($xmlRoot->{'lifecycle-callbacks'}->{'lifecycle-callback'} as $lifecycleCallback) { |
|
185 | 2 | $class->addLifecycleCallback((string) $lifecycleCallback['method'], constant('Doctrine\ODM\MongoDB\Events::' . (string) $lifecycleCallback['type'])); |
|
186 | 2 | } |
|
187 | 2 | } |
|
188 | 6 | if (isset($xmlRoot->{'also-load-methods'})) { |
|
189 | 1 | foreach ($xmlRoot->{'also-load-methods'}->{'also-load-method'} as $alsoLoadMethod) { |
|
190 | 1 | $class->registerAlsoLoadMethod((string) $alsoLoadMethod['method'], (string) $alsoLoadMethod['field']); |
|
191 | 1 | } |
|
192 | 1 | } |
|
193 | 6 | } |
|
194 | |||
195 | 6 | private function addFieldMapping(ClassMetadataInfo $class, $mapping) |
|
196 | { |
||
197 | 6 | View Code Duplication | if (isset($mapping['name'])) { |
198 | 6 | $name = $mapping['name']; |
|
199 | 6 | } elseif (isset($mapping['fieldName'])) { |
|
200 | 1 | $name = $mapping['fieldName']; |
|
201 | 1 | } else { |
|
202 | throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping'); |
||
203 | } |
||
204 | |||
205 | 6 | $class->mapField($mapping); |
|
206 | |||
207 | // Index this field if either "index", "unique", or "sparse" are set |
||
208 | 6 | View Code Duplication | if ( ! (isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) { |
209 | 6 | return; |
|
210 | } |
||
211 | |||
212 | 2 | $keys = array($name => isset($mapping['order']) ? $mapping['order'] : 'asc'); |
|
213 | 2 | $options = array(); |
|
214 | |||
215 | 2 | if (isset($mapping['background'])) { |
|
216 | $options['background'] = (boolean) $mapping['background']; |
||
217 | } |
||
218 | 2 | if (isset($mapping['drop-dups'])) { |
|
219 | 1 | $options['dropDups'] = (boolean) $mapping['drop-dups']; |
|
220 | 1 | } |
|
221 | 2 | if (isset($mapping['index-name'])) { |
|
222 | $options['name'] = (string) $mapping['index-name']; |
||
223 | } |
||
224 | 2 | if (isset($mapping['safe'])) { |
|
225 | $options['safe'] = (boolean) $mapping['safe']; |
||
226 | } |
||
227 | 2 | if (isset($mapping['sparse'])) { |
|
228 | 1 | $options['sparse'] = (boolean) $mapping['sparse']; |
|
229 | 1 | } |
|
230 | 2 | if (isset($mapping['unique'])) { |
|
231 | 2 | $options['unique'] = (boolean) $mapping['unique']; |
|
232 | 2 | } |
|
233 | |||
234 | 2 | $class->addIndex($keys, $options); |
|
235 | 2 | } |
|
236 | |||
237 | 2 | private function addEmbedMapping(ClassMetadataInfo $class, $embed, $type) |
|
271 | |||
272 | 3 | private function addReferenceMapping(ClassMetadataInfo $class, $reference, $type) |
|
273 | { |
||
274 | 3 | $cascade = array_keys((array) $reference->cascade); |
|
331 | |||
332 | 1 | private function addIndex(ClassMetadataInfo $class, \SimpleXmlElement $xmlIndex) |
|
379 | |||
380 | /** |
||
381 | * {@inheritDoc} |
||
382 | */ |
||
383 | 6 | protected function loadMappingFile($file) |
|
399 | } |
||
400 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.