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) |
|
| 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) |
|
| 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) |
|
| 334 | |||
| 335 | /** |
||
| 336 | * {@inheritDoc} |
||
| 337 | */ |
||
| 338 | 5 | protected function loadMappingFile($file) |
|
| 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.