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 |
||
22 | final class ArrayMapping implements MappingInterface |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var array<MappingInterface> |
||
27 | */ |
||
28 | private $entryMappings = array(); |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $origin; |
||
34 | |||
35 | 11 | public function __construct(array $entryMappings, string $origin = "unknown") |
|
47 | |||
48 | 1 | public function getEntryMappings(): array |
|
52 | |||
53 | 1 | public function describeOrigin(): string |
|
57 | |||
58 | 2 | public function collectDBALColumns(): array |
|
74 | |||
75 | 1 | public function resolveValue( |
|
93 | |||
94 | 2 | public function revertValue( |
|
124 | |||
125 | 1 | View Code Duplication | public function assertValue( |
|
|||
126 | HydrationContextInterface $context, |
||
127 | array $dataFromAdditionalColumns, |
||
128 | $actualValue |
||
129 | ): void { |
||
130 | 1 | if (!is_array($actualValue) && !is_null($actualValue)) { |
|
131 | 1 | throw FailedRDMAssertionException::expectedArray( |
|
132 | 1 | $actualValue, |
|
133 | 1 | $this->origin |
|
134 | ); |
||
135 | } |
||
136 | } |
||
137 | |||
138 | 1 | public function wakeUpMapping(ContainerInterface $container): void |
|
146 | } |
||
147 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.