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 YamlDriver 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 YamlDriver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | class YamlDriver extends FileDriver |
||
| 35 | { |
||
| 36 | const DEFAULT_FILE_EXTENSION = '.dcm.yml'; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * {@inheritDoc} |
||
| 40 | */ |
||
| 41 | 9 | public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) |
|
| 42 | { |
||
| 43 | 9 | parent::__construct($locator, $fileExtension); |
|
| 44 | 9 | } |
|
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritDoc} |
||
| 48 | */ |
||
| 49 | 5 | public function loadMetadataForClass($className, ClassMetadata $class) |
|
| 50 | { |
||
| 51 | /* @var $class ClassMetadataInfo */ |
||
| 52 | 5 | $element = $this->getElement($className); |
|
| 53 | 5 | if ( ! $element) { |
|
|
|
|||
| 54 | return; |
||
| 55 | } |
||
| 56 | 5 | $element['type'] = isset($element['type']) ? $element['type'] : 'document'; |
|
| 57 | |||
| 58 | 5 | if (isset($element['db'])) { |
|
| 59 | 2 | $class->setDatabase($element['db']); |
|
| 60 | } |
||
| 61 | 5 | if (isset($element['collection'])) { |
|
| 62 | 5 | $class->setCollection($element['collection']); |
|
| 63 | } |
||
| 64 | 5 | if ($element['type'] == 'document') { |
|
| 65 | 5 | if (isset($element['repositoryClass'])) { |
|
| 66 | 5 | $class->setCustomRepositoryClass($element['repositoryClass']); |
|
| 67 | } |
||
| 68 | 1 | View Code Duplication | } elseif ($element['type'] === 'mappedSuperclass') { |
| 69 | $class->setCustomRepositoryClass( |
||
| 70 | isset($element['repositoryClass']) ? $element['repositoryClass'] : null |
||
| 71 | ); |
||
| 72 | $class->isMappedSuperclass = true; |
||
| 73 | 1 | } elseif ($element['type'] === 'embeddedDocument') { |
|
| 74 | 1 | $class->isEmbeddedDocument = true; |
|
| 75 | } |
||
| 76 | 5 | if (isset($element['indexes'])) { |
|
| 77 | 2 | foreach($element['indexes'] as $index) { |
|
| 78 | 2 | $class->addIndex($index['keys'], isset($index['options']) ? $index['options'] : array()); |
|
| 79 | } |
||
| 80 | } |
||
| 81 | 5 | if (isset($element['shardKey'])) { |
|
| 82 | 1 | $this->setShardKey($class, $element['shardKey']); |
|
| 83 | } |
||
| 84 | 5 | View Code Duplication | if (isset($element['inheritanceType'])) { |
| 85 | $class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_' . strtoupper($element['inheritanceType']))); |
||
| 86 | } |
||
| 87 | 5 | if (isset($element['discriminatorField'])) { |
|
| 88 | 1 | $class->setDiscriminatorField($this->parseDiscriminatorField($element['discriminatorField'])); |
|
| 89 | } |
||
| 90 | 5 | if (isset($element['discriminatorMap'])) { |
|
| 91 | 1 | $class->setDiscriminatorMap($element['discriminatorMap']); |
|
| 92 | } |
||
| 93 | 5 | if (isset($element['defaultDiscriminatorValue'])) { |
|
| 94 | 1 | $class->setDefaultDiscriminatorValue($element['defaultDiscriminatorValue']); |
|
| 95 | } |
||
| 96 | 5 | View Code Duplication | if (isset($element['changeTrackingPolicy'])) { |
| 97 | $class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_' . strtoupper($element['changeTrackingPolicy']))); |
||
| 98 | } |
||
| 99 | 5 | if (isset($element['requireIndexes'])) { |
|
| 100 | $class->setRequireIndexes($element['requireIndexes']); |
||
| 101 | } |
||
| 102 | 5 | if (isset($element['slaveOkay'])) { |
|
| 103 | $class->setSlaveOkay($element['slaveOkay']); |
||
| 104 | } |
||
| 105 | 5 | if (isset($element['fields'])) { |
|
| 106 | 5 | foreach ($element['fields'] as $fieldName => $mapping) { |
|
| 107 | 5 | if (is_string($mapping)) { |
|
| 108 | 1 | $type = $mapping; |
|
| 109 | 1 | $mapping = array(); |
|
| 110 | 1 | $mapping['type'] = $type; |
|
| 111 | } |
||
| 112 | 5 | if ( ! isset($mapping['fieldName'])) { |
|
| 113 | 4 | $mapping['fieldName'] = $fieldName; |
|
| 114 | } |
||
| 115 | 5 | if (isset($mapping['type']) && ! empty($mapping['embedded'])) { |
|
| 116 | 2 | $this->addMappingFromEmbed($class, $fieldName, $mapping, $mapping['type']); |
|
| 117 | 5 | } elseif (isset($mapping['type']) && ! empty($mapping['reference'])) { |
|
| 118 | 2 | $this->addMappingFromReference($class, $fieldName, $mapping, $mapping['type']); |
|
| 119 | } else { |
||
| 120 | 5 | $this->addFieldMapping($class, $mapping); |
|
| 121 | } |
||
| 122 | } |
||
| 123 | } |
||
| 124 | 5 | View Code Duplication | if (isset($element['embedOne'])) { |
| 125 | 2 | foreach ($element['embedOne'] as $fieldName => $embed) { |
|
| 126 | 2 | $this->addMappingFromEmbed($class, $fieldName, $embed, 'one'); |
|
| 127 | } |
||
| 128 | } |
||
| 129 | 5 | View Code Duplication | if (isset($element['embedMany'])) { |
| 130 | 2 | foreach ($element['embedMany'] as $fieldName => $embed) { |
|
| 131 | 2 | $this->addMappingFromEmbed($class, $fieldName, $embed, 'many'); |
|
| 132 | } |
||
| 133 | } |
||
| 134 | 5 | View Code Duplication | if (isset($element['referenceOne'])) { |
| 135 | 2 | foreach ($element['referenceOne'] as $fieldName => $reference) { |
|
| 136 | 2 | $this->addMappingFromReference($class, $fieldName, $reference, 'one'); |
|
| 137 | } |
||
| 138 | } |
||
| 139 | 5 | View Code Duplication | if (isset($element['referenceMany'])) { |
| 140 | 2 | foreach ($element['referenceMany'] as $fieldName => $reference) { |
|
| 141 | 2 | $this->addMappingFromReference($class, $fieldName, $reference, 'many'); |
|
| 142 | } |
||
| 143 | } |
||
| 144 | 5 | if (isset($element['lifecycleCallbacks'])) { |
|
| 145 | 2 | foreach ($element['lifecycleCallbacks'] as $type => $methods) { |
|
| 146 | 2 | foreach ($methods as $method) { |
|
| 147 | 2 | $class->addLifecycleCallback($method, constant('Doctrine\ODM\MongoDB\Events::' . $type)); |
|
| 148 | } |
||
| 149 | } |
||
| 150 | } |
||
| 151 | 5 | if (isset($element['alsoLoadMethods'])) { |
|
| 152 | 1 | foreach ($element['alsoLoadMethods'] as $methodName => $fieldName) { |
|
| 153 | 1 | $class->registerAlsoLoadMethod($methodName, $fieldName); |
|
| 154 | } |
||
| 155 | } |
||
| 156 | 5 | } |
|
| 157 | |||
| 158 | 5 | private function addFieldMapping(ClassMetadataInfo $class, $mapping) |
|
| 159 | { |
||
| 160 | 5 | View Code Duplication | if (isset($mapping['name'])) { |
| 161 | 1 | $name = $mapping['name']; |
|
| 162 | 5 | } elseif (isset($mapping['fieldName'])) { |
|
| 163 | 5 | $name = $mapping['fieldName']; |
|
| 164 | } else { |
||
| 165 | throw new \InvalidArgumentException('Cannot infer a MongoDB name from the mapping'); |
||
| 166 | } |
||
| 167 | |||
| 168 | 5 | $class->mapField($mapping); |
|
| 169 | |||
| 170 | 5 | View Code Duplication | if ( ! (isset($mapping['index']) || isset($mapping['unique']) || isset($mapping['sparse']))) { |
| 171 | 5 | return; |
|
| 172 | } |
||
| 173 | |||
| 174 | // Multiple index specifications in one field mapping is ambiguous |
||
| 175 | 4 | if ((isset($mapping['index']) && is_array($mapping['index'])) + |
|
| 176 | 4 | (isset($mapping['unique']) && is_array($mapping['unique'])) + |
|
| 177 | 4 | (isset($mapping['sparse']) && is_array($mapping['sparse'])) > 1) { |
|
| 178 | throw new \InvalidArgumentException('Multiple index specifications found among index, unique, and/or sparse fields'); |
||
| 179 | } |
||
| 180 | |||
| 181 | // Index this field if either "index", "unique", or "sparse" are set |
||
| 182 | 4 | $keys = array($name => 'asc'); |
|
| 183 | |||
| 184 | /* The "order" option is only used in the index specification and should |
||
| 185 | * not be passed along as an index option. |
||
| 186 | */ |
||
| 187 | 4 | if (isset($mapping['index']['order'])) { |
|
| 188 | 1 | $keys[$name] = $mapping['index']['order']; |
|
| 189 | 1 | unset($mapping['index']['order']); |
|
| 190 | 4 | } elseif (isset($mapping['unique']['order'])) { |
|
| 191 | 1 | $keys[$name] = $mapping['unique']['order']; |
|
| 192 | 1 | unset($mapping['unique']['order']); |
|
| 193 | 3 | } elseif (isset($mapping['sparse']['order'])) { |
|
| 194 | $keys[$name] = $mapping['sparse']['order']; |
||
| 195 | unset($mapping['sparse']['order']); |
||
| 196 | } |
||
| 197 | |||
| 198 | /* Initialize $options from any array value among index, unique, and |
||
| 199 | * sparse. Any boolean values for unique or sparse should be merged into |
||
| 200 | * the options afterwards to ensure consistent parsing. |
||
| 201 | */ |
||
| 202 | 4 | $options = array(); |
|
| 203 | 4 | $unique = null; |
|
| 204 | 4 | $sparse = null; |
|
| 205 | |||
| 206 | 4 | if (isset($mapping['index']) && is_array($mapping['index'])) { |
|
| 207 | 1 | $options = $mapping['index']; |
|
| 208 | } |
||
| 209 | |||
| 210 | 4 | View Code Duplication | if (isset($mapping['unique'])) { |
| 211 | 4 | if (is_array($mapping['unique'])) { |
|
| 212 | 1 | $options = $mapping['unique'] + array('unique' => true); |
|
| 213 | } else { |
||
| 214 | 3 | $unique = (boolean) $mapping['unique']; |
|
| 215 | } |
||
| 216 | } |
||
| 217 | |||
| 218 | 4 | View Code Duplication | if (isset($mapping['sparse'])) { |
| 219 | 3 | if (is_array($mapping['sparse'])) { |
|
| 220 | $options = $mapping['sparse'] + array('sparse' => true); |
||
| 221 | } else { |
||
| 222 | 3 | $sparse = (boolean) $mapping['sparse']; |
|
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | 4 | if (isset($unique)) { |
|
| 227 | 3 | $options['unique'] = $unique; |
|
| 228 | } |
||
| 229 | |||
| 230 | 4 | if (isset($sparse)) { |
|
| 231 | 3 | $options['sparse'] = $sparse; |
|
| 232 | } |
||
| 233 | |||
| 234 | 4 | $class->addIndex($keys, $options); |
|
| 235 | 4 | } |
|
| 236 | |||
| 237 | 4 | private function addMappingFromEmbed(ClassMetadataInfo $class, $fieldName, $embed, $type) |
|
| 261 | |||
| 262 | 4 | private function addMappingFromReference(ClassMetadataInfo $class, $fieldName, $reference, $type) |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Parses the class or field-level "discriminatorField" option. |
||
| 304 | * |
||
| 305 | * If the value is an array, check the "name" option before falling back to |
||
| 306 | * the deprecated "fieldName" option (for BC). Otherwise, the value must be |
||
| 307 | * a string. |
||
| 308 | * |
||
| 309 | * @param array|string $discriminatorField |
||
| 310 | * @return string |
||
| 311 | * @throws \InvalidArgumentException if the value is neither a string nor an |
||
| 312 | * array with a "name" or "fieldName" key. |
||
| 313 | */ |
||
| 314 | 1 | private function parseDiscriminatorField($discriminatorField) |
|
| 315 | { |
||
| 316 | 1 | if (is_string($discriminatorField)) { |
|
| 317 | 1 | return $discriminatorField; |
|
| 318 | } |
||
| 319 | |||
| 320 | 1 | if ( ! is_array($discriminatorField)) { |
|
| 321 | throw new \InvalidArgumentException('Expected array or string for discriminatorField; found: ' . gettype($discriminatorField)); |
||
| 322 | } |
||
| 323 | |||
| 324 | 1 | if (isset($discriminatorField['name'])) { |
|
| 325 | return (string) $discriminatorField['name']; |
||
| 326 | } |
||
| 327 | |||
| 328 | 1 | if (isset($discriminatorField['fieldName'])) { |
|
| 329 | 1 | return (string) $discriminatorField['fieldName']; |
|
| 330 | } |
||
| 331 | |||
| 332 | throw new \InvalidArgumentException('Expected "name" or "fieldName" key in discriminatorField array; found neither.'); |
||
| 333 | } |
||
| 334 | |||
| 335 | /** |
||
| 336 | * {@inheritDoc} |
||
| 337 | */ |
||
| 338 | 5 | protected function loadMappingFile($file) |
|
| 339 | { |
||
| 340 | 5 | return Yaml::parse(file_get_contents($file)); |
|
| 341 | } |
||
| 342 | |||
| 343 | 1 | private function setShardKey(ClassMetadataInfo $class, array $shardKey) |
|
| 360 | } |
||
| 361 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.