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 DocumentMap 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 DocumentMap, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 17 | class DocumentMap |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var array |
||
| 21 | */ |
||
| 22 | private $mappings = []; |
||
| 23 | /** |
||
| 24 | * @var Document[] |
||
| 25 | */ |
||
| 26 | private $documents = []; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * Constructor |
||
| 30 | * |
||
| 31 | * @param Finder $doctrineFinder Doctrine mapping finder |
||
| 32 | * @param Finder $serializerFinder Serializer mapping finder |
||
| 33 | * @param Finder $validationFinder Validation mapping finder |
||
| 34 | */ |
||
| 35 | 14 | public function __construct(Finder $doctrineFinder, Finder $serializerFinder, Finder $validationFinder) |
|
| 36 | { |
||
| 37 | 14 | $doctrineMap = $this->loadDoctrineClassMap($doctrineFinder); |
|
| 38 | 14 | $serializerMap = $this->loadSerializerClassMap($serializerFinder); |
|
| 39 | 14 | $validationMap = $this->loadValidationClassMap($validationFinder); |
|
| 40 | |||
| 41 | 14 | foreach ($doctrineMap as $className => $doctrineMapping) { |
|
| 42 | 14 | $this->mappings[$className] = [ |
|
| 43 | 14 | 'doctrine' => $doctrineMap[$className], |
|
| 44 | 14 | 'serializer' => isset($serializerMap[$className]) ? $serializerMap[$className] : null, |
|
| 45 | 14 | 'validation' => isset($validationMap[$className]) ? $validationMap[$className] : null, |
|
| 46 | ]; |
||
| 47 | 7 | } |
|
| 48 | 14 | } |
|
| 49 | |||
| 50 | /** |
||
| 51 | * Get document |
||
| 52 | * |
||
| 53 | * @param string $className Document class |
||
| 54 | * @return Document |
||
| 55 | */ |
||
| 56 | 14 | public function getDocument($className) |
|
| 57 | { |
||
| 58 | 14 | if (isset($this->documents[$className])) { |
|
| 59 | 14 | return $this->documents[$className]; |
|
| 60 | } |
||
| 61 | 14 | if (!isset($this->mappings[$className])) { |
|
| 62 | throw new \InvalidArgumentException(sprintf('No XML mapping found for document "%s"', $className)); |
||
| 63 | } |
||
| 64 | |||
| 65 | 14 | return $this->documents[$className] = $this->processDocument( |
|
| 66 | 7 | $className, |
|
| 67 | 14 | $this->mappings[$className]['doctrine'], |
|
| 68 | 14 | $this->mappings[$className]['serializer'], |
|
| 69 | 14 | $this->mappings[$className]['validation'] |
|
| 70 | 7 | ); |
|
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Get all documents |
||
| 75 | * |
||
| 76 | * @return Document[] |
||
| 77 | */ |
||
| 78 | 10 | public function getDocuments() |
|
| 79 | { |
||
| 80 | 10 | return array_map([$this, 'getDocument'], array_keys($this->mappings)); |
|
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Process document |
||
| 85 | * |
||
| 86 | * @param string $className Class name |
||
| 87 | * @param \DOMElement $doctrineMapping Doctrine XML mapping |
||
| 88 | * @param \DOMElement $serializerMapping Serializer XML mapping |
||
|
|
|||
| 89 | * @param \DOMElement $validationMapping Validation XML mapping |
||
| 90 | * @return Document |
||
| 91 | */ |
||
| 92 | 14 | private function processDocument( |
|
| 93 | $className, |
||
| 94 | \DOMElement $doctrineMapping, |
||
| 95 | \DOMElement $serializerMapping = null, |
||
| 96 | \DOMElement $validationMapping = null |
||
| 97 | ) { |
||
| 98 | 14 | if ($serializerMapping === null) { |
|
| 99 | 2 | $serializerFields = []; |
|
| 100 | 1 | } else { |
|
| 101 | 14 | $serializerFields = array_reduce( |
|
| 102 | 14 | $this->getSerializerFields($serializerMapping), |
|
| 103 | function (array $fields, array $field) { |
||
| 104 | 14 | $fields[$field['fieldName']] = $field; |
|
| 105 | 14 | return $fields; |
|
| 106 | 14 | }, |
|
| 107 | 14 | [] |
|
| 108 | 7 | ); |
|
| 109 | } |
||
| 110 | |||
| 111 | 14 | if ($validationMapping === null) { |
|
| 112 | 2 | $validationFields = []; |
|
| 113 | 1 | } else { |
|
| 114 | 14 | $validationFields = array_reduce( |
|
| 115 | 14 | $this->getValidationFields($validationMapping), |
|
| 116 | function (array $fields, array $field) { |
||
| 117 | 8 | $fields[$field['fieldName']] = $field; |
|
| 118 | 8 | return $fields; |
|
| 119 | 14 | }, |
|
| 120 | 14 | [] |
|
| 121 | 7 | ); |
|
| 122 | } |
||
| 123 | |||
| 124 | 14 | $fields = []; |
|
| 125 | 14 | foreach ($this->getDoctrineFields($doctrineMapping) as $doctrineField) { |
|
| 126 | 14 | $serializerField = isset($serializerFields[$doctrineField['name']]) ? |
|
| 127 | 14 | $serializerFields[$doctrineField['name']] : |
|
| 128 | 14 | null; |
|
| 129 | 14 | $validationField = isset($validationFields[$doctrineField['name']]) ? |
|
| 130 | 11 | $validationFields[$doctrineField['name']] : |
|
| 131 | 14 | null; |
|
| 132 | |||
| 133 | 14 | if ($doctrineField['type'] === 'collection') { |
|
| 134 | 2 | $fields[] = new ArrayField( |
|
| 135 | 2 | $serializerField === null ? 'array<string>' : $serializerField['fieldType'], |
|
| 136 | 2 | $doctrineField['name'], |
|
| 137 | 2 | $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'], |
|
| 138 | 2 | $serializerField === null ? false : $serializerField['readOnly'], |
|
| 139 | 2 | $validationField === null ? false : $validationField['required'], |
|
| 140 | 2 | $serializerField === null ? false : $serializerField['searchable'] |
|
| 141 | 1 | ); |
|
| 142 | 1 | } else { |
|
| 143 | 14 | $fields[] = new Field( |
|
| 144 | 14 | $doctrineField['type'], |
|
| 145 | 14 | $doctrineField['name'], |
|
| 146 | 14 | $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'], |
|
| 147 | 14 | $serializerField === null ? false : $serializerField['readOnly'], |
|
| 148 | 14 | $validationField === null ? false : $validationField['required'], |
|
| 149 | 14 | $serializerField === null ? false : $serializerField['searchable'] |
|
| 150 | 7 | ); |
|
| 151 | } |
||
| 152 | 7 | } |
|
| 153 | 14 | foreach ($this->getDoctrineEmbedOneFields($doctrineMapping) as $doctrineField) { |
|
| 154 | 14 | $serializerField = isset($serializerFields[$doctrineField['name']]) ? |
|
| 155 | 14 | $serializerFields[$doctrineField['name']] : |
|
| 156 | 14 | null; |
|
| 157 | 14 | $validationField = isset($validationFields[$doctrineField['name']]) ? |
|
| 158 | 11 | $validationFields[$doctrineField['name']] : |
|
| 159 | 14 | null; |
|
| 160 | |||
| 161 | 14 | $fields[] = new EmbedOne( |
|
| 162 | 14 | $this->getDocument($doctrineField['type']), |
|
| 163 | 14 | $doctrineField['name'], |
|
| 164 | 14 | $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'], |
|
| 165 | 14 | $serializerField === null ? false : $serializerField['readOnly'], |
|
| 166 | 14 | $validationField === null ? false : $validationField['required'], |
|
| 167 | 14 | $serializerField === null ? false : $serializerField['searchable'] |
|
| 168 | 7 | ); |
|
| 169 | 7 | } |
|
| 170 | 14 | foreach ($this->getDoctrineEmbedManyFields($doctrineMapping) as $doctrineField) { |
|
| 171 | 14 | $serializerField = isset($serializerFields[$doctrineField['name']]) ? |
|
| 172 | 14 | $serializerFields[$doctrineField['name']] : |
|
| 173 | 14 | null; |
|
| 174 | 14 | $validationField = isset($validationFields[$doctrineField['name']]) ? |
|
| 175 | 8 | $validationFields[$doctrineField['name']] : |
|
| 176 | 14 | null; |
|
| 177 | |||
| 178 | 14 | $fields[] = new EmbedMany( |
|
| 179 | 14 | $this->getDocument($doctrineField['type']), |
|
| 180 | 14 | $doctrineField['name'], |
|
| 181 | 14 | $serializerField === null ? $doctrineField['name'] : $serializerField['exposedName'], |
|
| 182 | 14 | $serializerField === null ? false : $serializerField['readOnly'], |
|
| 183 | 14 | $validationField === null ? false : $validationField['required'] |
|
| 184 | 7 | ); |
|
| 185 | 7 | } |
|
| 186 | |||
| 187 | 14 | return new Document($className, $fields); |
|
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Load doctrine class map |
||
| 192 | * |
||
| 193 | * @param Finder $finder Mapping finder |
||
| 194 | * @return array |
||
| 195 | */ |
||
| 196 | 14 | View Code Duplication | private function loadDoctrineClassMap(Finder $finder) |
| 197 | { |
||
| 198 | 14 | $classMap = []; |
|
| 199 | 14 | foreach ($finder as $file) { |
|
| 200 | 14 | $document = new \DOMDocument(); |
|
| 201 | 14 | $document->load($file); |
|
| 202 | |||
| 203 | 14 | $xpath = new \DOMXPath($document); |
|
| 204 | 14 | $xpath->registerNamespace('doctrine', 'http://doctrine-project.org/schemas/odm/doctrine-mongo-mapping'); |
|
| 205 | |||
| 206 | 14 | $classMap = array_reduce( |
|
| 207 | 14 | iterator_to_array($xpath->query('//*[self::doctrine:document or self::doctrine:embedded-document]')), |
|
| 208 | function (array $classMap, \DOMElement $element) { |
||
| 209 | 14 | $classMap[$element->getAttribute('name')] = $element; |
|
| 210 | 14 | return $classMap; |
|
| 211 | 14 | }, |
|
| 212 | $classMap |
||
| 213 | 7 | ); |
|
| 214 | 7 | } |
|
| 215 | |||
| 216 | 14 | return $classMap; |
|
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Load serializer class map |
||
| 221 | * |
||
| 222 | * @param Finder $finder Mapping finder |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | 14 | View Code Duplication | private function loadSerializerClassMap(Finder $finder) |
| 226 | { |
||
| 227 | 14 | $classMap = []; |
|
| 228 | 14 | foreach ($finder as $file) { |
|
| 229 | 14 | $document = new \DOMDocument(); |
|
| 230 | 14 | $document->load($file); |
|
| 231 | |||
| 232 | 14 | $xpath = new \DOMXPath($document); |
|
| 233 | |||
| 234 | 14 | $classMap = array_reduce( |
|
| 235 | 14 | iterator_to_array($xpath->query('//class')), |
|
| 236 | function (array $classMap, \DOMElement $element) { |
||
| 237 | 14 | $classMap[$element->getAttribute('name')] = $element; |
|
| 238 | 14 | return $classMap; |
|
| 239 | 14 | }, |
|
| 240 | $classMap |
||
| 241 | 7 | ); |
|
| 242 | 7 | } |
|
| 243 | |||
| 244 | 14 | return $classMap; |
|
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Load validation class map |
||
| 249 | * |
||
| 250 | * @param Finder $finder Mapping finder |
||
| 251 | * @return array |
||
| 252 | */ |
||
| 253 | 14 | View Code Duplication | private function loadValidationClassMap(Finder $finder) |
| 254 | { |
||
| 255 | 14 | $classMap = []; |
|
| 256 | 14 | foreach ($finder as $file) { |
|
| 257 | 14 | $document = new \DOMDocument(); |
|
| 258 | 14 | $document->load($file); |
|
| 259 | |||
| 260 | 14 | $xpath = new \DOMXPath($document); |
|
| 261 | 14 | $xpath->registerNamespace('constraint', 'http://symfony.com/schema/dic/constraint-mapping'); |
|
| 262 | |||
| 263 | 14 | $classMap = array_reduce( |
|
| 264 | 14 | iterator_to_array($xpath->query('//constraint:class')), |
|
| 265 | function (array $classMap, \DOMElement $element) { |
||
| 266 | 14 | $classMap[$element->getAttribute('name')] = $element; |
|
| 267 | 14 | return $classMap; |
|
| 268 | 14 | }, |
|
| 269 | $classMap |
||
| 270 | 7 | ); |
|
| 271 | 7 | } |
|
| 272 | |||
| 273 | 14 | return $classMap; |
|
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get serializer fields |
||
| 278 | * |
||
| 279 | * @param \DOMElement $mapping Serializer XML mapping |
||
| 280 | * @return array |
||
| 281 | */ |
||
| 282 | 14 | private function getSerializerFields(\DOMElement $mapping) |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Get serializer field type |
||
| 302 | * |
||
| 303 | * @param \DOMElement $field Field node |
||
| 304 | * @return string|null |
||
| 305 | */ |
||
| 306 | 14 | private function getSerializerFieldType(\DOMElement $field) |
|
| 317 | |||
| 318 | /** |
||
| 319 | * Get validation fields |
||
| 320 | * |
||
| 321 | * @param \DOMElement $mapping Validation XML mapping |
||
| 322 | * @return array |
||
| 323 | */ |
||
| 324 | 14 | private function getValidationFields(\DOMElement $mapping) |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Get doctrine document fields |
||
| 343 | * |
||
| 344 | * @param \DOMElement $mapping Doctrine XML mapping |
||
| 345 | * @return array |
||
| 346 | */ |
||
| 347 | 14 | View Code Duplication | private function getDoctrineFields(\DOMElement $mapping) |
| 362 | |||
| 363 | /** |
||
| 364 | * Get doctrine document embed-one fields |
||
| 365 | * |
||
| 366 | * @param \DOMElement $mapping Doctrine XML mapping |
||
| 367 | * @return array |
||
| 368 | */ |
||
| 369 | 14 | View Code Duplication | private function getDoctrineEmbedOneFields(\DOMElement $mapping) |
| 384 | |||
| 385 | /** |
||
| 386 | * Get doctrine document embed-many fields |
||
| 387 | * |
||
| 388 | * @param \DOMElement $mapping Doctrine XML mapping |
||
| 389 | * @return array |
||
| 390 | */ |
||
| 391 | 14 | View Code Duplication | private function getDoctrineEmbedManyFields(\DOMElement $mapping) |
| 406 | } |
||
| 407 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.