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 namespace Limoncello\Validation\Rules\Generic; |
||
29 | final class Enum extends BaseRule |
||
30 | { |
||
31 | /** |
||
32 | * Property key. |
||
33 | */ |
||
34 | const PROPERTY_VALUES = self::PROPERTY_LAST + 1; |
||
35 | |||
36 | /** |
||
37 | * @var array |
||
38 | */ |
||
39 | private $values; |
||
40 | |||
41 | /** |
||
42 | * @param array $values |
||
43 | */ |
||
44 | 1 | public function __construct(array $values) |
|
50 | |||
51 | /** |
||
52 | * @inheritdoc |
||
53 | */ |
||
54 | 1 | View Code Duplication | public function toBlock(): ExecutionBlockInterface |
62 | |||
63 | /** |
||
64 | * @param mixed $value |
||
65 | * @param ContextInterface $context |
||
66 | * |
||
67 | * @return array |
||
68 | * |
||
69 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
70 | */ |
||
71 | 1 | public static function execute($value, ContextInterface $context): array |
|
82 | |||
83 | /** |
||
84 | * @return array |
||
85 | */ |
||
86 | 1 | public function getValues(): array |
|
90 | } |
||
91 |
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.