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 MappingException 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 MappingException, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class MappingException extends \Doctrine\ORM\ORMException |
||
13 | { |
||
14 | /** |
||
15 | * @return MappingException |
||
16 | */ |
||
17 | public static function pathRequired() |
||
22 | |||
23 | /** |
||
24 | * @param string $entityName |
||
25 | * |
||
26 | * @return MappingException |
||
27 | */ |
||
28 | View Code Duplication | public static function identifierRequired($entityName) |
|
43 | 8 | ||
44 | /** |
||
45 | 8 | * @param string $entityName |
|
46 | 4 | * @param string $type |
|
47 | 4 | * |
|
48 | * @return MappingException |
||
49 | */ |
||
50 | public static function invalidInheritanceType($entityName, $type) |
||
54 | |||
55 | /** |
||
56 | * @return MappingException |
||
57 | */ |
||
58 | public static function generatorNotAllowedWithCompositeId() |
||
62 | |||
63 | /** |
||
64 | * @param string $entity |
||
65 | * |
||
66 | * @return MappingException |
||
67 | */ |
||
68 | public static function missingFieldName($entity) |
||
72 | |||
73 | /** |
||
74 | * @param string $fieldName |
||
75 | * |
||
76 | * @return MappingException |
||
77 | */ |
||
78 | public static function missingTargetEntity($fieldName) |
||
82 | |||
83 | 1 | /** |
|
84 | * @param string $fieldName |
||
85 | 1 | * |
|
86 | * @return MappingException |
||
87 | */ |
||
88 | public static function missingSourceEntity($fieldName) |
||
92 | |||
93 | /** |
||
94 | * @param string $fieldName |
||
95 | * |
||
96 | * @return MappingException |
||
97 | */ |
||
98 | public static function missingEmbeddedClass($fieldName) |
||
102 | |||
103 | /** |
||
104 | * @param string $entityName |
||
105 | * @param string $fileName |
||
106 | * |
||
107 | * @return MappingException |
||
108 | */ |
||
109 | public static function mappingFileNotFound($entityName, $fileName) |
||
113 | |||
114 | /** |
||
115 | * Exception for invalid property name override. |
||
116 | * |
||
117 | * @param string $className The entity's name. |
||
118 | * @param string $fieldName |
||
119 | * |
||
120 | * @return MappingException |
||
121 | */ |
||
122 | public static function invalidOverrideFieldName($className, $fieldName) |
||
126 | |||
127 | /** |
||
128 | * Exception for invalid property type override. |
||
129 | * |
||
130 | * @param string $className The entity's name. |
||
131 | * @param string $fieldName |
||
132 | * |
||
133 | * @return MappingException |
||
134 | */ |
||
135 | public static function invalidOverridePropertyType($className, $fieldName) |
||
139 | 2 | ||
140 | /** |
||
141 | * Exception for invalid version property override. |
||
142 | * |
||
143 | * @param string $className The entity's name. |
||
144 | * @param string $fieldName |
||
145 | * |
||
146 | * @return MappingException |
||
147 | */ |
||
148 | public static function invalidOverrideVersionField($className, $fieldName) |
||
152 | |||
153 | /** |
||
154 | * Exception for invalid property type override. |
||
155 | * |
||
156 | * @param string $className The entity's name. |
||
157 | * @param string $fieldName |
||
158 | * |
||
159 | * @return MappingException |
||
160 | */ |
||
161 | public static function invalidOverrideFieldType($className, $fieldName) |
||
165 | |||
166 | /** |
||
167 | * @param string $className |
||
168 | * @param string $fieldName |
||
169 | * |
||
170 | * @return MappingException |
||
171 | */ |
||
172 | 1 | public static function mappingNotFound($className, $fieldName) |
|
176 | |||
177 | /** |
||
178 | * @param string $className |
||
179 | * @param string $queryName |
||
180 | * |
||
181 | * @return MappingException |
||
182 | */ |
||
183 | public static function queryNotFound($className, $queryName) |
||
187 | |||
188 | /** |
||
189 | * @param string $className |
||
190 | * @param string $resultName |
||
191 | * |
||
192 | * @return MappingException |
||
193 | */ |
||
194 | public static function resultMappingNotFound($className, $resultName) |
||
198 | |||
199 | /** |
||
200 | * @param string $entity |
||
201 | * @param string $queryName |
||
202 | * |
||
203 | * @return MappingException |
||
204 | 2 | */ |
|
205 | public static function missingQueryMapping($entity, $queryName) |
||
209 | |||
210 | /** |
||
211 | * @param string $entity |
||
212 | * @param string $resultName |
||
213 | * |
||
214 | * @return MappingException |
||
215 | */ |
||
216 | public static function missingResultSetMappingEntity($entity, $resultName) |
||
220 | |||
221 | /** |
||
222 | * @param string $entity |
||
223 | * @param string $resultName |
||
224 | * |
||
225 | * @return MappingException |
||
226 | 1 | */ |
|
227 | public static function missingResultSetMappingFieldName($entity, $resultName) |
||
231 | |||
232 | /** |
||
233 | * @param string $className |
||
234 | * |
||
235 | * @return MappingException |
||
236 | */ |
||
237 | public static function nameIsMandatoryForSqlResultSetMapping($className) |
||
241 | |||
242 | /** |
||
243 | * @param string $fieldName |
||
244 | * |
||
245 | * @return MappingException |
||
246 | */ |
||
247 | public static function oneToManyRequiresMappedBy($fieldName) |
||
251 | |||
252 | /** |
||
253 | * @param string $fieldName |
||
254 | * |
||
255 | * @return MappingException |
||
256 | */ |
||
257 | public static function joinTableRequired($fieldName) |
||
261 | |||
262 | /** |
||
263 | * Called if a required option was not found but is required |
||
264 | * |
||
265 | * @param string $field Which field cannot be processed? |
||
266 | * @param string $expectedOption Which option is required |
||
267 | * @param string $hint Can optionally be used to supply a tip for common mistakes, |
||
268 | * e.g. "Did you think of the plural s?" |
||
269 | * |
||
270 | * @return MappingException |
||
271 | */ |
||
272 | public static function missingRequiredOption($field, $expectedOption, $hint = '') |
||
282 | |||
283 | /** |
||
284 | * Generic exception for invalid mappings. |
||
285 | * |
||
286 | * @param string $fieldName |
||
287 | * |
||
288 | * @return MappingException |
||
289 | */ |
||
290 | public static function invalidMapping($fieldName) |
||
294 | |||
295 | /** |
||
296 | * Exception for reflection exceptions - adds the entity name, |
||
297 | * because there might be long classnames that will be shortened |
||
298 | * within the stacktrace |
||
299 | * |
||
300 | * @param string $entity The entity's name |
||
301 | * @param \ReflectionException $previousException |
||
302 | * |
||
303 | * @return MappingException |
||
304 | */ |
||
305 | public static function reflectionFailure($entity, \ReflectionException $previousException) |
||
309 | |||
310 | /** |
||
311 | * @param string $className |
||
312 | * @param string $joinColumn |
||
313 | * |
||
314 | * @return MappingException |
||
315 | */ |
||
316 | public static function joinColumnMustPointToMappedField($className, $joinColumn) |
||
321 | |||
322 | /** |
||
323 | * @param string $className |
||
324 | * |
||
325 | * @return MappingException |
||
326 | */ |
||
327 | View Code Duplication | public static function classIsNotAValidEntityOrMappedSuperClass($className) |
|
341 | 1 | ||
342 | /** |
||
343 | * @param string $className |
||
344 | * @param string $propertyName |
||
345 | * |
||
346 | 2 | * @return MappingException |
|
347 | 2 | */ |
|
348 | public static function propertyTypeIsRequired($className, $propertyName) |
||
352 | |||
353 | /** |
||
354 | * @param string $className |
||
355 | * |
||
356 | * @return MappingException |
||
357 | */ |
||
358 | public static function tableIdGeneratorNotImplemented($className) |
||
362 | |||
363 | /** |
||
364 | * @param string $className |
||
365 | * @param Property $property |
||
366 | * |
||
367 | * @return MappingException |
||
368 | */ |
||
369 | public static function duplicateProperty($className, Property $property) |
||
378 | 1 | ||
379 | /** |
||
380 | 1 | * @param string $entity |
|
381 | 1 | * @param string $queryName |
|
382 | 1 | * |
|
383 | 1 | * @return MappingException |
|
384 | */ |
||
385 | public static function duplicateQueryMapping($entity, $queryName) |
||
389 | |||
390 | /** |
||
391 | * @param string $entity |
||
392 | * @param string $resultName |
||
393 | 1 | * |
|
394 | * @return MappingException |
||
395 | 1 | */ |
|
396 | public static function duplicateResultSetMapping($entity, $resultName) |
||
400 | |||
401 | /** |
||
402 | * @param string $entity |
||
403 | * |
||
404 | 1 | * @return MappingException |
|
405 | */ |
||
406 | 1 | public static function singleIdNotAllowedOnCompositePrimaryKey($entity) |
|
410 | |||
411 | /** |
||
412 | * @param string $entity |
||
413 | * |
||
414 | * @return MappingException |
||
415 | 2 | */ |
|
416 | public static function noIdDefined($entity) |
||
420 | |||
421 | /** |
||
422 | * @param string $unsupportedType |
||
423 | * |
||
424 | * @return MappingException |
||
425 | */ |
||
426 | 1 | public static function unsupportedOptimisticLockingType($unsupportedType) |
|
430 | |||
431 | /** |
||
432 | * @param string|null $path |
||
433 | * |
||
434 | * @return MappingException |
||
435 | */ |
||
436 | 1 | public static function fileMappingDriversRequireConfiguredDirectoryPath($path = null) |
|
447 | |||
448 | 1 | /** |
|
449 | * Returns an exception that indicates that a class used in a discriminator map does not exist. |
||
450 | 1 | * An example would be an outdated (maybe renamed) classname. |
|
451 | 1 | * |
|
452 | * @param string $className The class that could not be found |
||
453 | * @param string $owningClass The class that declares the discriminator map. |
||
454 | * |
||
455 | * @return MappingException |
||
456 | */ |
||
457 | public static function invalidClassInDiscriminatorMap($className, $owningClass) |
||
464 | |||
465 | /** |
||
466 | * @param string $className |
||
467 | * @param array $entries |
||
468 | * @param array $map |
||
469 | * |
||
470 | * @return MappingException |
||
471 | */ |
||
472 | public static function duplicateDiscriminatorEntry($className, array $entries, array $map) |
||
482 | |||
483 | /** |
||
484 | * @param string $className |
||
485 | * |
||
486 | * @return MappingException |
||
487 | */ |
||
488 | public static function missingDiscriminatorMap($className) |
||
492 | |||
493 | /** |
||
494 | * @param string $className |
||
495 | * |
||
496 | * @return MappingException |
||
497 | */ |
||
498 | public static function missingDiscriminatorColumn($className) |
||
502 | |||
503 | /** |
||
504 | * @param string $type |
||
505 | * |
||
506 | * @return MappingException |
||
507 | */ |
||
508 | public static function invalidDiscriminatorColumnType($type) |
||
512 | |||
513 | /** |
||
514 | * @param string $className |
||
515 | * |
||
516 | * @return MappingException |
||
517 | */ |
||
518 | public static function nameIsMandatoryForDiscriminatorColumns($className) |
||
522 | |||
523 | /** |
||
524 | * @param string $className |
||
525 | * @param string $fieldName |
||
526 | * |
||
527 | * @return MappingException |
||
528 | */ |
||
529 | public static function cannotVersionIdField($className, $fieldName) |
||
533 | |||
534 | /** |
||
535 | * @param string $className |
||
536 | * @param Property $property |
||
537 | * |
||
538 | * @return MappingException |
||
539 | */ |
||
540 | public static function sqlConversionNotAllowedForPrimaryKeyProperties($className, Property $property) |
||
551 | |||
552 | /** |
||
553 | * @param string $className |
||
554 | * @param string $fieldName |
||
555 | * @param string $type |
||
556 | * |
||
557 | * @return MappingException |
||
558 | */ |
||
559 | public static function sqlConversionNotAllowedForIdentifiers($className, $fieldName, $type) |
||
563 | |||
564 | 437 | /** |
|
565 | * @param string $className |
||
566 | 437 | * @param string $columnName |
|
567 | * |
||
568 | 437 | * @return MappingException |
|
569 | 437 | */ |
|
570 | 437 | public static function duplicateColumnName($className, $columnName) |
|
574 | |||
575 | /** |
||
576 | * @param string $className |
||
577 | * @param string $field |
||
578 | * |
||
579 | * @return MappingException |
||
580 | */ |
||
581 | public static function illegalToManyAssociationOnMappedSuperclass($className, $field) |
||
585 | |||
586 | /** |
||
587 | * @param string $className |
||
588 | * @param string $targetEntity |
||
589 | * @param string $targetField |
||
590 | * |
||
591 | * @return MappingException |
||
592 | */ |
||
593 | public static function cannotMapCompositePrimaryKeyEntitiesAsForeignId($className, $targetEntity, $targetField) |
||
598 | |||
599 | /** |
||
600 | * @param string $className |
||
601 | * @param string $field |
||
602 | * |
||
603 | * @return MappingException |
||
604 | */ |
||
605 | 1 | public static function noSingleAssociationJoinColumnFound($className, $field) |
|
609 | |||
610 | /** |
||
611 | * @param string $className |
||
612 | * @param string $column |
||
613 | * |
||
614 | * @return MappingException |
||
615 | */ |
||
616 | public static function noFieldNameFoundForColumn($className, $column) |
||
621 | |||
622 | /** |
||
623 | * @param string $className |
||
624 | * @param string $field |
||
625 | * |
||
626 | * @return MappingException |
||
627 | */ |
||
628 | public static function illegalOrphanRemovalOnIdentifierAssociation($className, $field) |
||
633 | |||
634 | /** |
||
635 | * @param string $className |
||
636 | * @param string $field |
||
637 | * |
||
638 | * @return MappingException |
||
639 | */ |
||
640 | public static function illegalOrphanRemoval($className, $field) |
||
645 | |||
646 | /** |
||
647 | * @param string $className |
||
648 | * @param string $field |
||
649 | * |
||
650 | * @return MappingException |
||
651 | */ |
||
652 | 1 | public static function illegalInverseIdentifierAssociation($className, $field) |
|
656 | |||
657 | /** |
||
658 | * @param string $className |
||
659 | * @param string $field |
||
660 | * |
||
661 | * @return MappingException |
||
662 | */ |
||
663 | public static function illegalToManyIdentifierAssociation($className, $field) |
||
667 | 1 | ||
668 | /** |
||
669 | * @param string $className |
||
670 | * |
||
671 | * @return MappingException |
||
672 | */ |
||
673 | public static function noInheritanceOnMappedSuperClass($className) |
||
677 | |||
678 | 2 | /** |
|
679 | * @param string $className |
||
680 | * @param string $rootClassName |
||
681 | * |
||
682 | * @return MappingException |
||
683 | */ |
||
684 | public static function mappedClassNotPartOfDiscriminatorMap($className, $rootClassName) |
||
692 | |||
693 | /** |
||
694 | * @param string $className |
||
695 | * @param string $methodName |
||
696 | * |
||
697 | 1 | * @return MappingException |
|
698 | */ |
||
699 | 1 | public static function lifecycleCallbackMethodNotFound($className, $methodName) |
|
703 | |||
704 | /** |
||
705 | * @param string $listenerName |
||
706 | * @param string $className |
||
707 | * |
||
708 | 1 | * @return \Doctrine\ORM\Mapping\MappingException |
|
709 | */ |
||
710 | 1 | public static function entityListenerClassNotFound($listenerName, $className) |
|
714 | |||
715 | /** |
||
716 | * @param string $listenerName |
||
717 | * @param string $methodName |
||
718 | * @param string $className |
||
719 | * |
||
720 | * @return \Doctrine\ORM\Mapping\MappingException |
||
721 | */ |
||
722 | public static function entityListenerMethodNotFound($listenerName, $methodName, $className) |
||
726 | |||
727 | /** |
||
728 | * @param string $listenerName |
||
729 | * @param string $methodName |
||
730 | * @param string $className |
||
731 | * |
||
732 | * @return \Doctrine\ORM\Mapping\MappingException |
||
733 | */ |
||
734 | 1 | public static function duplicateEntityListener($listenerName, $methodName, $className) |
|
738 | |||
739 | /** |
||
740 | * @param string $className |
||
741 | * @param string $annotation |
||
742 | * |
||
743 | * @return MappingException |
||
744 | */ |
||
745 | public static function invalidFetchMode($className, $annotation) |
||
749 | |||
750 | /** |
||
751 | * @param string $className |
||
752 | * |
||
753 | * @return MappingException |
||
754 | */ |
||
755 | public static function compositeKeyAssignedIdGeneratorRequired($className) |
||
759 | |||
760 | 1 | /** |
|
761 | * @param string $targetEntity |
||
762 | * @param string $sourceEntity |
||
763 | * @param string $associationName |
||
764 | * |
||
765 | * @return MappingException |
||
766 | */ |
||
767 | public static function invalidTargetEntityClass($targetEntity, $sourceEntity, $associationName) |
||
771 | |||
772 | /** |
||
773 | * @param array $cascades |
||
774 | * @param string $className |
||
775 | * @param string $propertyName |
||
776 | * |
||
777 | * @return MappingException |
||
778 | */ |
||
779 | public static function invalidCascadeOption(array $cascades, $className, $propertyName) |
||
790 | |||
791 | 1 | /** |
|
792 | * @param string $className |
||
793 | 1 | * |
|
794 | * @return MappingException |
||
795 | */ |
||
796 | public static function missingSequenceName($className) |
||
802 | |||
803 | /** |
||
804 | * @param string $className |
||
805 | * @param string $propertyName |
||
806 | * |
||
807 | 1 | * @return MappingException |
|
808 | 1 | */ |
|
809 | public static function infiniteEmbeddableNesting($className, $propertyName) |
||
820 | 1 | ||
821 | /** |
||
822 | 1 | * @param LibXMLError[] $errors |
|
823 | 1 | */ |
|
824 | public static function fromLibXmlErrors(array $errors) : self |
||
835 | } |
||
836 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: