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 | 13 | public function __construct($locator, $fileExtension = self::DEFAULT_FILE_EXTENSION) |
|
| 45 | |||
| 46 | /** |
||
| 47 | * {@inheritDoc} |
||
| 48 | */ |
||
| 49 | 9 | public function loadMetadataForClass($className, ClassMetadata $class) |
|
| 160 | |||
| 161 | 9 | private function addFieldMapping(ClassMetadataInfo $class, $mapping) |
|
| 239 | |||
| 240 | 4 | private function addMappingFromEmbed(ClassMetadataInfo $class, $fieldName, $embed, $type) |
|
| 241 | { |
||
| 242 | 4 | $defaultStrategy = $type == 'one' ? ClassMetadataInfo::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY; |
|
| 243 | $mapping = array( |
||
| 244 | 4 | 'type' => $type, |
|
| 245 | 'embedded' => true, |
||
| 246 | 4 | 'targetDocument' => isset($embed['targetDocument']) ? $embed['targetDocument'] : null, |
|
| 247 | 4 | 'fieldName' => $fieldName, |
|
| 248 | 4 | 'strategy' => isset($embed['strategy']) ? (string) $embed['strategy'] : $defaultStrategy, |
|
| 249 | ); |
||
| 250 | 4 | if (isset($embed['name'])) { |
|
| 251 | 1 | $mapping['name'] = $embed['name']; |
|
| 252 | } |
||
| 253 | 4 | if (isset($embed['discriminatorField'])) { |
|
| 254 | 1 | $mapping['discriminatorField'] = $this->parseDiscriminatorField($embed['discriminatorField']); |
|
| 255 | } |
||
| 256 | 4 | if (isset($embed['discriminatorMap'])) { |
|
| 257 | 1 | $mapping['discriminatorMap'] = $embed['discriminatorMap']; |
|
| 258 | } |
||
| 259 | 4 | if (isset($embed['defaultDiscriminatorValue'])) { |
|
| 260 | 1 | $mapping['defaultDiscriminatorValue'] = $embed['defaultDiscriminatorValue']; |
|
| 261 | } |
||
| 262 | 4 | $this->addFieldMapping($class, $mapping); |
|
| 263 | 4 | } |
|
| 264 | |||
| 265 | 4 | private function addMappingFromReference(ClassMetadataInfo $class, $fieldName, $reference, $type) |
|
| 266 | { |
||
| 267 | 4 | $defaultStrategy = $type == 'one' ? ClassMetadataInfo::STORAGE_STRATEGY_SET : CollectionHelper::DEFAULT_STRATEGY; |
|
| 268 | $mapping = array( |
||
| 269 | 4 | 'cascade' => isset($reference['cascade']) ? $reference['cascade'] : null, |
|
| 270 | 4 | 'orphanRemoval' => isset($reference['orphanRemoval']) ? $reference['orphanRemoval'] : false, |
|
| 271 | 4 | 'type' => $type, |
|
| 272 | 'reference' => true, |
||
| 273 | 4 | 'simple' => isset($reference['simple']) ? (boolean) $reference['simple'] : false, // deprecated |
|
| 274 | 4 | 'storeAs' => isset($reference['storeAs']) ? (string) $reference['storeAs'] : ClassMetadataInfo::REFERENCE_STORE_AS_DB_REF_WITH_DB, |
|
| 275 | 4 | 'targetDocument' => isset($reference['targetDocument']) ? $reference['targetDocument'] : null, |
|
| 276 | 4 | 'fieldName' => $fieldName, |
|
| 277 | 4 | 'strategy' => isset($reference['strategy']) ? (string) $reference['strategy'] : $defaultStrategy, |
|
| 278 | 4 | 'inversedBy' => isset($reference['inversedBy']) ? (string) $reference['inversedBy'] : null, |
|
| 279 | 4 | 'mappedBy' => isset($reference['mappedBy']) ? (string) $reference['mappedBy'] : null, |
|
| 280 | 4 | 'repositoryMethod' => isset($reference['repositoryMethod']) ? (string) $reference['repositoryMethod'] : null, |
|
| 281 | 4 | 'limit' => isset($reference['limit']) ? (integer) $reference['limit'] : null, |
|
| 282 | 4 | 'skip' => isset($reference['skip']) ? (integer) $reference['skip'] : null, |
|
| 283 | ); |
||
| 284 | 4 | if (isset($reference['name'])) { |
|
| 285 | 1 | $mapping['name'] = $reference['name']; |
|
| 286 | } |
||
| 287 | 4 | if (isset($reference['discriminatorField'])) { |
|
| 288 | 1 | $mapping['discriminatorField'] = $this->parseDiscriminatorField($reference['discriminatorField']); |
|
| 289 | } |
||
| 290 | 4 | if (isset($reference['discriminatorMap'])) { |
|
| 291 | 1 | $mapping['discriminatorMap'] = $reference['discriminatorMap']; |
|
| 292 | } |
||
| 293 | 4 | if (isset($reference['defaultDiscriminatorValue'])) { |
|
| 294 | 1 | $mapping['defaultDiscriminatorValue'] = $reference['defaultDiscriminatorValue']; |
|
| 295 | } |
||
| 296 | 4 | if (isset($reference['sort'])) { |
|
| 297 | $mapping['sort'] = $reference['sort']; |
||
| 298 | } |
||
| 299 | 4 | if (isset($reference['criteria'])) { |
|
| 300 | $mapping['criteria'] = $reference['criteria']; |
||
| 301 | } |
||
| 302 | 4 | $this->addFieldMapping($class, $mapping); |
|
| 303 | 4 | } |
|
| 304 | |||
| 305 | /** |
||
| 306 | * Parses the class or field-level "discriminatorField" option. |
||
| 307 | * |
||
| 308 | * If the value is an array, check the "name" option before falling back to |
||
| 309 | * the deprecated "fieldName" option (for BC). Otherwise, the value must be |
||
| 310 | * a string. |
||
| 311 | * |
||
| 312 | * @param array|string $discriminatorField |
||
| 313 | * @return string |
||
| 314 | * @throws \InvalidArgumentException if the value is neither a string nor an |
||
| 315 | * array with a "name" or "fieldName" key. |
||
| 316 | */ |
||
| 317 | 1 | private function parseDiscriminatorField($discriminatorField) |
|
| 318 | { |
||
| 319 | 1 | if (is_string($discriminatorField)) { |
|
| 320 | 1 | return $discriminatorField; |
|
| 321 | } |
||
| 322 | |||
| 323 | 1 | if ( ! is_array($discriminatorField)) { |
|
| 324 | throw new \InvalidArgumentException('Expected array or string for discriminatorField; found: ' . gettype($discriminatorField)); |
||
| 325 | } |
||
| 326 | |||
| 327 | 1 | if (isset($discriminatorField['name'])) { |
|
| 328 | return (string) $discriminatorField['name']; |
||
| 329 | } |
||
| 330 | |||
| 331 | 1 | if (isset($discriminatorField['fieldName'])) { |
|
| 332 | 1 | return (string) $discriminatorField['fieldName']; |
|
| 333 | } |
||
| 334 | |||
| 335 | throw new \InvalidArgumentException('Expected "name" or "fieldName" key in discriminatorField array; found neither.'); |
||
| 336 | } |
||
| 337 | |||
| 338 | /** |
||
| 339 | * {@inheritDoc} |
||
| 340 | */ |
||
| 341 | 9 | protected function loadMappingFile($file) |
|
| 345 | |||
| 346 | 1 | private function setShardKey(ClassMetadataInfo $class, array $shardKey) |
|
| 347 | { |
||
| 348 | 1 | $keys = $shardKey['keys']; |
|
| 363 | } |
||
| 364 |
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.