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 |
||
| 35 | class YamlDriver extends FileDriver |
||
| 36 | { |
||
| 37 | const DEFAULT_FILE_EXTENSION = '.dcm.yml'; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritDoc} |
||
| 41 | */ |
||
| 42 | 14 | public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) |
|
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritDoc} |
||
| 49 | */ |
||
| 50 | 10 | public function loadMetadataForClass($className, ClassMetadata $class) |
|
| 51 | { |
||
| 52 | /* @var $class ClassMetadataInfo */ |
||
| 53 | 10 | $element = $this->getElement($className); |
|
| 54 | 10 | if ( ! $element) { |
|
|
|
|||
| 55 | return; |
||
| 56 | } |
||
| 57 | 10 | $element['type'] = isset($element['type']) ? $element['type'] : 'document'; |
|
| 58 | |||
| 59 | 10 | if (isset($element['db'])) { |
|
| 60 | 3 | $class->setDatabase($element['db']); |
|
| 61 | } |
||
| 62 | 10 | if (isset($element['collection'])) { |
|
| 63 | 10 | $class->setCollection($element['collection']); |
|
| 64 | } |
||
| 65 | 10 | if (isset($element['writeConcern'])) { |
|
| 66 | 2 | $class->setWriteConcern($element['writeConcern']); |
|
| 67 | } |
||
| 68 | 10 | if ($element['type'] == 'document') { |
|
| 69 | 10 | if (isset($element['repositoryClass'])) { |
|
| 70 | $class->setCustomRepositoryClass($element['repositoryClass']); |
||
| 71 | } |
||
| 72 | 1 | View Code Duplication | } elseif ($element['type'] === 'mappedSuperclass') { |
| 73 | $class->setCustomRepositoryClass( |
||
| 74 | isset($element['repositoryClass']) ? $element['repositoryClass'] : null |
||
| 75 | ); |
||
| 76 | $class->isMappedSuperclass = true; |
||
| 77 | 1 | } elseif ($element['type'] === 'embeddedDocument') { |
|
| 78 | 1 | $class->isEmbeddedDocument = true; |
|
| 79 | 1 | } elseif ($element['type'] === 'queryResultDocument') { |
|
| 80 | 1 | $class->isQueryResultDocument = true; |
|
| 81 | } |
||
| 82 | 10 | if (isset($element['indexes'])) { |
|
| 83 | 3 | foreach($element['indexes'] as $index) { |
|
| 84 | 3 | $class->addIndex($index['keys'], isset($index['options']) ? $index['options'] : array()); |
|
| 85 | } |
||
| 86 | } |
||
| 87 | 10 | if (isset($element['shardKey'])) { |
|
| 88 | 2 | $this->setShardKey($class, $element['shardKey']); |
|
| 89 | } |
||
| 90 | 10 | View Code Duplication | if (isset($element['inheritanceType'])) { |
| 91 | $class->setInheritanceType(constant(MappingClassMetadata::class . '::INHERITANCE_TYPE_' . strtoupper($element['inheritanceType']))); |
||
| 92 | } |
||
| 93 | 10 | if (isset($element['discriminatorField'])) { |
|
| 94 | 2 | $class->setDiscriminatorField($this->parseDiscriminatorField($element['discriminatorField'])); |
|
| 95 | } |
||
| 96 | 10 | if (isset($element['discriminatorMap'])) { |
|
| 97 | 2 | $class->setDiscriminatorMap($element['discriminatorMap']); |
|
| 98 | } |
||
| 99 | 10 | if (isset($element['defaultDiscriminatorValue'])) { |
|
| 100 | 2 | $class->setDefaultDiscriminatorValue($element['defaultDiscriminatorValue']); |
|
| 101 | } |
||
| 102 | 10 | View Code Duplication | if (isset($element['changeTrackingPolicy'])) { |
| 103 | $class->setChangeTrackingPolicy(constant(MappingClassMetadata::class . '::CHANGETRACKING_' . strtoupper($element['changeTrackingPolicy']))); |
||
| 104 | } |
||
| 105 | 10 | if (isset($element['requireIndexes'])) { |
|
| 106 | $class->setRequireIndexes($element['requireIndexes']); |
||
| 107 | } |
||
| 108 | 10 | if (isset($element['slaveOkay'])) { |
|
| 109 | $class->setSlaveOkay($element['slaveOkay']); |
||
| 110 | } |
||
| 111 | 10 | if (isset($element['fields'])) { |
|
| 112 | 10 | foreach ($element['fields'] as $fieldName => $mapping) { |
|
| 113 | 10 | if (is_string($mapping)) { |
|
| 114 | 1 | $type = $mapping; |
|
| 115 | 1 | $mapping = array(); |
|
| 116 | 1 | $mapping['type'] = $type; |
|
| 117 | } |
||
| 118 | 10 | if ( ! isset($mapping['fieldName'])) { |
|
| 119 | 8 | $mapping['fieldName'] = $fieldName; |
|
| 120 | } |
||
| 121 | 10 | if (isset($mapping['type']) && ! empty($mapping['embedded'])) { |
|
| 122 | 2 | $this->addMappingFromEmbed($class, $fieldName, $mapping, $mapping['type']); |
|
| 123 | 10 | } elseif (isset($mapping['type']) && ! empty($mapping['reference'])) { |
|
| 124 | 2 | $this->addMappingFromReference($class, $fieldName, $mapping, $mapping['type']); |
|
| 125 | } else { |
||
| 126 | 10 | $this->addFieldMapping($class, $mapping); |
|
| 127 | } |
||
| 128 | } |
||
| 129 | } |
||
| 130 | 10 | View Code Duplication | if (isset($element['embedOne'])) { |
| 131 | 3 | foreach ($element['embedOne'] as $fieldName => $embed) { |
|
| 132 | 3 | $this->addMappingFromEmbed($class, $fieldName, $embed, 'one'); |
|
| 133 | } |
||
| 134 | } |
||
| 135 | 10 | View Code Duplication | if (isset($element['embedMany'])) { |
| 136 | 3 | foreach ($element['embedMany'] as $fieldName => $embed) { |
|
| 137 | 3 | $this->addMappingFromEmbed($class, $fieldName, $embed, 'many'); |
|
| 138 | } |
||
| 139 | } |
||
| 140 | 10 | View Code Duplication | if (isset($element['referenceOne'])) { |
| 141 | 3 | foreach ($element['referenceOne'] as $fieldName => $reference) { |
|
| 142 | 3 | $this->addMappingFromReference($class, $fieldName, $reference, 'one'); |
|
| 143 | } |
||
| 144 | } |
||
| 145 | 10 | View Code Duplication | if (isset($element['referenceMany'])) { |
| 146 | 4 | foreach ($element['referenceMany'] as $fieldName => $reference) { |
|
| 147 | 4 | $this->addMappingFromReference($class, $fieldName, $reference, 'many'); |
|
| 148 | } |
||
| 149 | } |
||
| 150 | 10 | if (isset($element['lifecycleCallbacks'])) { |
|
| 151 | 3 | foreach ($element['lifecycleCallbacks'] as $type => $methods) { |
|
| 152 | 3 | foreach ($methods as $method) { |
|
| 153 | 3 | $class->addLifecycleCallback($method, constant('Doctrine\ODM\MongoDB\Events::' . $type)); |
|
| 154 | } |
||
| 155 | } |
||
| 156 | } |
||
| 157 | 10 | if (isset($element['alsoLoadMethods'])) { |
|
| 158 | 1 | foreach ($element['alsoLoadMethods'] as $methodName => $fieldName) { |
|
| 159 | 1 | $class->registerAlsoLoadMethod($methodName, $fieldName); |
|
| 160 | } |
||
| 161 | } |
||
| 162 | 10 | } |
|
| 163 | |||
| 164 | 10 | private function addFieldMapping(ClassMetadataInfo $class, $mapping) |
|
| 242 | |||
| 243 | 5 | private function addMappingFromEmbed(ClassMetadataInfo $class, $fieldName, $embed, $type) |
|
| 268 | |||
| 269 | 6 | private function addMappingFromReference(ClassMetadataInfo $class, $fieldName, $reference, $type) |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Parses the class or field-level "discriminatorField" option. |
||
| 313 | * |
||
| 314 | * If the value is an array, check the "name" option before falling back to |
||
| 315 | * the deprecated "fieldName" option (for BC). Otherwise, the value must be |
||
| 316 | * a string. |
||
| 317 | * |
||
| 318 | * @param array|string $discriminatorField |
||
| 319 | * @return string |
||
| 320 | * @throws \InvalidArgumentException if the value is neither a string nor an |
||
| 321 | * array with a "name" or "fieldName" key. |
||
| 322 | */ |
||
| 323 | 2 | private function parseDiscriminatorField($discriminatorField) |
|
| 343 | |||
| 344 | /** |
||
| 345 | * {@inheritDoc} |
||
| 346 | */ |
||
| 347 | 10 | protected function loadMappingFile($file) |
|
| 357 | |||
| 358 | 2 | private function setShardKey(ClassMetadataInfo $class, array $shardKey) |
|
| 375 | } |
||
| 376 |
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.