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 |
||
37 | class YamlDriver extends FileDriver |
||
38 | { |
||
39 | const DEFAULT_FILE_EXTENSION = '.dcm.yml'; |
||
40 | |||
41 | /** |
||
42 | * {@inheritDoc} |
||
43 | */ |
||
44 | public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) |
||
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | public function loadMetadataForClass($className, ClassMetadata $class) |
||
53 | { |
||
54 | /** @var $class \Doctrine\ODM\CouchDB\Mapping\ClassMetadata */ |
||
55 | try { |
||
56 | $element = $this->getElement($className); |
||
57 | } catch (DoctrineMappingException $e) { |
||
58 | // Convert Exception type for consistency with other drivers |
||
59 | throw new MappingException($e->getMessage(), $e->getCode(), $e); |
||
60 | } |
||
61 | |||
62 | if (!$element) { |
||
|
|||
63 | return; |
||
64 | } |
||
65 | |||
66 | if ($element['type'] == 'document') { |
||
67 | $class->setCustomRepositoryClass( |
||
68 | (isset($element['repositoryClass'])) ? $element['repositoryClass'] : null |
||
69 | ); |
||
70 | |||
71 | if (isset($element['indexed']) && $element['indexed'] == true) { |
||
72 | $class->indexed = true; |
||
73 | } |
||
74 | |||
75 | if (isset($element['inheritanceRoot']) && $element['inheritanceRoot']) { |
||
76 | $class->markInheritanceRoot(); |
||
77 | } |
||
78 | } else if ($element['type'] == 'embedded') { |
||
79 | $class->isEmbeddedDocument = true; |
||
80 | |||
81 | if (isset($element['inheritanceRoot']) && $element['inheritanceRoot']) { |
||
82 | $class->markInheritanceRoot(); |
||
83 | } |
||
84 | } else if (strtolower($element['type']) == "mappedsuperclass") { |
||
85 | $class->isMappedSuperclass = true; |
||
86 | } else { |
||
87 | throw MappingException::classIsNotAValidDocument($className); |
||
88 | } |
||
89 | |||
90 | View Code Duplication | if (isset($element['id'])) { |
|
91 | foreach ($element['id'] AS $fieldName => $idElement) { |
||
92 | $class->mapField(array( |
||
93 | 'fieldName' => $fieldName, |
||
94 | 'indexed' => (isset($idElement['index'])) ? (bool)$idElement['index'] : false, |
||
95 | 'type' => (isset($idElement['type'])) ? $idElement['type'] : null, |
||
96 | 'id' => true, |
||
97 | 'strategy' => (isset($idElement['strategy'])) ? $idElement['strategy'] : null, |
||
98 | )); |
||
99 | } |
||
100 | } |
||
101 | |||
102 | View Code Duplication | if (isset($element['fields'])) { |
|
103 | foreach ($element['fields'] AS $fieldName => $fieldElement) { |
||
104 | $class->mapField(array( |
||
105 | 'fieldName' => $fieldName, |
||
106 | 'jsonName' => (isset($fieldElement['jsonName'])) ? $fieldElement['jsonName'] : null, |
||
107 | 'indexed' => (isset($fieldElement['index'])) ? (bool)$fieldElement['index'] : false, |
||
108 | 'type' => (isset($fieldElement['type'])) ? $fieldElement['type'] : null, |
||
109 | 'isVersionField' => (isset($fieldElement['version'])) ? true : null, |
||
110 | )); |
||
111 | } |
||
112 | } |
||
113 | |||
114 | |||
115 | View Code Duplication | if (isset($element['referenceOne'])) { |
|
116 | foreach ($element['referenceOne'] AS $field => $referenceOneElement) { |
||
117 | $class->mapManyToOne(array( |
||
118 | 'cascade' => (isset($referenceOneElement['cascade'])) ? $this->getCascadeMode($referenceOneElement['cascade']) : 0, |
||
119 | 'targetDocument' => (string)$referenceOneElement['targetDocument'], |
||
120 | 'fieldName' => $field, |
||
121 | 'jsonName' => (isset($referenceOneElement['jsonName'])) ? (string)$referenceOneElement['jsonName'] : null, |
||
122 | 'indexed' => (isset($referenceOneElement['index'])) ? (bool)$referenceOneElement['index'] : false, |
||
123 | )); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | View Code Duplication | if (isset($element['referenceMany'])) { |
|
128 | foreach ($element['referenceMany'] AS $field => $referenceManyElement) { |
||
129 | $class->mapManyToMany(array( |
||
130 | 'cascade' => (isset($referenceManyElement['cascade'])) ? $this->getCascadeMode($referenceManyElement['cascade']) : 0, |
||
131 | 'targetDocument' => (string)$referenceManyElement['targetDocument'], |
||
132 | 'fieldName' => $field, |
||
133 | 'jsonName' => (isset($referenceManyElement['jsonName'])) ? (string)$referenceManyElement['jsonName'] : null, |
||
134 | 'mappedBy' => (isset($referenceManyElement['mappedBy'])) ? (string)$referenceManyElement['mappedBy'] : null, |
||
135 | )); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | if (isset($element['attachments'])) { |
||
140 | $class->mapAttachments($element['attachments']); |
||
141 | } |
||
142 | |||
143 | View Code Duplication | if (isset($element['embedOne'])) { |
|
144 | foreach ($element['embedOne'] AS $field => $embedOneElement) { |
||
145 | $class->mapEmbedded(array( |
||
146 | 'targetDocument' => (string)$embedOneElement['targetDocument'], |
||
147 | 'fieldName' => $field, |
||
148 | 'jsonName' => (isset($embedOneElement['jsonName'])) ? (string)$embedOneElement['jsonName'] : null, |
||
149 | 'embedded' => 'one', |
||
150 | )); |
||
151 | } |
||
152 | } |
||
153 | |||
154 | View Code Duplication | if (isset($element['embedMany'])) { |
|
155 | foreach ($element['embedMany'] AS $field => $embedManyElement) { |
||
156 | $class->mapEmbedded(array( |
||
157 | 'targetDocument' => (string)$embedManyElement['targetDocument'], |
||
158 | 'fieldName' => $field, |
||
159 | 'jsonName' => (isset($embedManyElement['jsonName'])) ? (string)$embedManyElement['jsonName'] : null, |
||
160 | 'embedded' => 'many', |
||
161 | )); |
||
162 | } |
||
163 | } |
||
164 | } |
||
165 | |||
166 | protected function loadMappingFile($file) |
||
170 | |||
171 | /** |
||
172 | * Gathers a list of cascade options found in the given cascade element. |
||
173 | * |
||
174 | * @param array $cascadeElement The cascade element. |
||
175 | * @return integer a bitmask of cascade options. |
||
176 | * @throws MappingException |
||
177 | */ |
||
178 | View Code Duplication | private function getCascadeMode(array $cascadeElement) |
|
191 | } |
||
192 |
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.