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:
| 1 | <?php |
||
| 28 | class EnumDelegate extends TypeDefinitionDelegate |
||
| 29 | { |
||
| 30 | /** |
||
| 31 | * @param DocumentInterface|Document $document |
||
| 32 | * @return Definition |
||
| 33 | */ |
||
| 34 | protected function create(DocumentInterface $document): Definition |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @param Environment $env |
||
| 41 | */ |
||
| 42 | public function boot(Environment $env): void |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param EnumDefinition|TypeDefinition $enum |
||
| 53 | */ |
||
| 54 | private function bootValues(EnumDefinition $enum): void |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @param EnumDefinition $enum |
||
| 67 | * @return iterable |
||
| 68 | */ |
||
| 69 | private function getEnumValues(EnumDefinition $enum): iterable |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @param EnumDefinition $enum |
||
| 90 | * @param LeafInterface|NodeInterface $name |
||
| 91 | * @return EnumValueDefinition |
||
| 92 | */ |
||
| 93 | private function createEnumValue(EnumDefinition $enum, LeafInterface $name): EnumValueDefinition |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param EnumDefinition $enum |
||
| 103 | * @param EnumValueDefinition $value |
||
| 104 | * @throws \Railt\SDL\Exception\CompilerException |
||
| 105 | */ |
||
| 106 | private function verifyDuplication(EnumDefinition $enum, EnumValueDefinition $value): void |
||
| 115 | } |
||
| 116 |
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.