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 | 8 | public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) |
|
| 43 | { |
||
| 44 | 8 | parent::__construct($locator, $fileExtension); |
|
| 45 | 8 | } |
|
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritDoc} |
||
| 49 | */ |
||
| 50 | 4 | public function loadMetadataForClass($className, ClassMetadata $class) |
|
| 51 | { |
||
| 52 | /* @var $class ClassMetadataInfo */ |
||
| 53 | 4 | $element = $this->getElement($className); |
|
| 54 | 4 | if ( ! $element) { |
|
|
|
|||
| 55 | return; |
||
| 56 | } |
||
| 57 | 4 | $element['type'] = isset($element['type']) ? $element['type'] : 'document'; |
|
| 58 | |||
| 59 | 4 | if (isset($element['db'])) { |
|
| 60 | 1 | $class->setDatabase($element['db']); |
|
| 61 | 1 | } |
|
| 62 | 4 | if (isset($element['collection'])) { |
|
| 63 | 4 | $class->setCollection($element['collection']); |
|
| 64 | 4 | } |
|
| 65 | 4 | if ($element['type'] == 'document') { |
|
| 66 | 4 | if (isset($element['repositoryClass'])) { |
|
| 67 | $class->setCustomRepositoryClass($element['repositoryClass']); |
||
| 68 | } |
||
| 69 | 4 | View Code Duplication | } elseif ($element['type'] === 'mappedSuperclass') { |
| 70 | $class->setCustomRepositoryClass( |
||
| 71 | isset($element['repositoryClass']) ? $element['repositoryClass'] : null |
||
| 72 | ); |
||
| 73 | $class->isMappedSuperclass = true; |
||
| 74 | 1 | } elseif ($element['type'] === 'embeddedDocument') { |
|
| 75 | 1 | $class->isEmbeddedDocument = true; |
|
| 76 | 1 | } |
|
| 77 | 4 | if (isset($element['indexes'])) { |
|
| 78 | 1 | foreach($element['indexes'] as $index) { |
|
| 79 | 1 | $class->addIndex($index['keys'], isset($index['options']) ? $index['options'] : array()); |
|
| 80 | 1 | } |
|
| 81 | 1 | } |
|
| 82 | 4 | if (isset($element['inheritanceType'])) { |
|
| 83 | $class->setInheritanceType(constant('Doctrine\ODM\MongoDB\Mapping\ClassMetadata::INHERITANCE_TYPE_' . strtoupper($element['inheritanceType']))); |
||
| 84 | } |
||
| 85 | 4 | if (isset($element['discriminatorField'])) { |
|
| 86 | 1 | $class->setDiscriminatorField($this->parseDiscriminatorField($element['discriminatorField'])); |
|
| 87 | 1 | } |
|
| 88 | 4 | if (isset($element['discriminatorMap'])) { |
|
| 89 | 1 | $class->setDiscriminatorMap($element['discriminatorMap']); |
|
| 90 | 1 | } |
|
| 91 | 4 | if (isset($element['defaultDiscriminatorValue'])) { |
|
| 92 | 1 | $class->setDefaultDiscriminatorValue($element['defaultDiscriminatorValue']); |
|
| 93 | 1 | } |
|
| 94 | 4 | View Code Duplication | if (isset($element['changeTrackingPolicy'])) { |
| 95 | $class->setChangeTrackingPolicy(constant('Doctrine\ODM\MongoDB\Mapping\ClassMetadata::CHANGETRACKING_' |
||
| 96 | . strtoupper($element['changeTrackingPolicy']))); |
||
| 97 | } |
||
| 98 | 4 | if (isset($element['requireIndexes'])) { |
|
| 99 | $class->setRequireIndexes($element['requireIndexes']); |
||
| 100 | } |
||
| 101 | 4 | if (isset($element['slaveOkay'])) { |
|
| 102 | $class->setSlaveOkay($element['slaveOkay']); |
||
| 103 | } |
||
| 104 | 4 | if (isset($element['fields'])) { |
|
| 105 | 4 | foreach ($element['fields'] as $fieldName => $mapping) { |
|
| 106 | 4 | if (is_string($mapping)) { |
|
| 107 | 1 | $type = $mapping; |
|
| 108 | 1 | $mapping = array(); |
|
| 109 | 1 | $mapping['type'] = $type; |
|
| 110 | 1 | } |
|
| 111 | 4 | if ( ! isset($mapping['fieldName'])) { |
|
| 112 | 4 | $mapping['fieldName'] = $fieldName; |
|
| 113 | 4 | } |
|
| 114 | 4 | if (isset($mapping['type']) && ! empty($mapping['embedded'])) { |
|
| 115 | 2 | $this->addMappingFromEmbed($class, $fieldName, $mapping, $mapping['type']); |
|
| 116 | 4 | } elseif (isset($mapping['type']) && ! empty($mapping['reference'])) { |
|
| 117 | 2 | $this->addMappingFromReference($class, $fieldName, $mapping, $mapping['type']); |
|
| 118 | 2 | } else { |
|
| 119 | 4 | $this->addFieldMapping($class, $mapping); |
|
| 120 | } |
||
| 121 | 4 | } |
|
| 122 | 4 | } |
|
| 123 | 4 | View Code Duplication | if (isset($element['embedOne'])) { |
| 124 | 2 | foreach ($element['embedOne'] as $fieldName => $embed) { |
|
| 125 | 2 | $this->addMappingFromEmbed($class, $fieldName, $embed, 'one'); |
|
| 126 | 2 | } |
|
| 127 | 2 | } |
|
| 128 | 4 | View Code Duplication | if (isset($element['embedMany'])) { |
| 129 | 2 | foreach ($element['embedMany'] as $fieldName => $embed) { |
|
| 130 | 2 | $this->addMappingFromEmbed($class, $fieldName, $embed, 'many'); |
|
| 131 | 2 | } |
|
| 132 | 2 | } |
|
| 133 | 4 | View Code Duplication | if (isset($element['referenceOne'])) { |
| 134 | 2 | foreach ($element['referenceOne'] as $fieldName => $reference) { |
|
| 135 | 2 | $this->addMappingFromReference($class, $fieldName, $reference, 'one'); |
|
| 136 | 2 | } |
|
| 137 | 2 | } |
|
| 138 | 4 | View Code Duplication | if (isset($element['referenceMany'])) { |
| 139 | 2 | foreach ($element['referenceMany'] as $fieldName => $reference) { |
|
| 140 | 2 | $this->addMappingFromReference($class, $fieldName, $reference, 'many'); |
|
| 141 | 2 | } |
|
| 142 | 2 | } |
|
| 143 | 4 | if (isset($element['lifecycleCallbacks'])) { |
|
| 144 | 2 | foreach ($element['lifecycleCallbacks'] as $type => $methods) { |
|
| 145 | 2 | foreach ($methods as $method) { |
|
| 146 | 2 | $class->addLifecycleCallback($method, constant('Doctrine\ODM\MongoDB\Events::' . $type)); |
|
| 147 | 2 | } |
|
| 148 | 2 | } |
|
| 149 | 2 | } |
|
| 150 | 4 | if (isset($element['alsoLoadMethods'])) { |
|
| 151 | 1 | foreach ($element['alsoLoadMethods'] as $methodName => $fieldName) { |
|
| 152 | 1 | $class->registerAlsoLoadMethod($methodName, $fieldName); |
|
| 153 | 1 | } |
|
| 154 | 1 | } |
|
| 155 | 4 | } |
|
| 156 | |||
| 157 | 4 | private function addFieldMapping(ClassMetadataInfo $class, $mapping) |
|
| 235 | |||
| 236 | 4 | private function addMappingFromEmbed(ClassMetadataInfo $class, $fieldName, $embed, $type) |
|
| 237 | { |
||
| 259 | |||
| 260 | 4 | private function addMappingFromReference(ClassMetadataInfo $class, $fieldName, $reference, $type) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Parses the class or field-level "discriminatorField" option. |
||
| 300 | * |
||
| 301 | * If the value is an array, check the "name" option before falling back to |
||
| 302 | * the deprecated "fieldName" option (for BC). Otherwise, the value must be |
||
| 303 | * a string. |
||
| 304 | * |
||
| 305 | * @param array|string $discriminatorField |
||
| 306 | * @return string |
||
| 307 | * @throws \InvalidArgumentException if the value is neither a string nor an |
||
| 308 | * array with a "name" or "fieldName" key. |
||
| 309 | */ |
||
| 310 | 1 | private function parseDiscriminatorField($discriminatorField) |
|
| 330 | |||
| 331 | /** |
||
| 332 | * {@inheritDoc} |
||
| 333 | */ |
||
| 334 | 4 | protected function loadMappingFile($file) |
|
| 338 | } |
||
| 339 |
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.