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; |
||
29 | View Code Duplication | class SingleValidator extends BaseValidator |
|
|
|||
30 | { |
||
31 | use SingleValidation; |
||
32 | |||
33 | /** |
||
34 | * @param RuleInterface $rule |
||
35 | */ |
||
36 | 4 | public function __construct(RuleInterface $rule) |
|
42 | |||
43 | /** |
||
44 | * @param RuleInterface $rule |
||
45 | * |
||
46 | * @return ValidatorInterface |
||
47 | */ |
||
48 | 4 | public static function validator(RuleInterface $rule): ValidatorInterface |
|
54 | |||
55 | /** |
||
56 | * @inheritdoc |
||
57 | * |
||
58 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
59 | */ |
||
60 | 4 | public function validate($input): bool |
|
73 | |||
74 | /** |
||
75 | * During validation you can pass to rules your custom context which might have any additional |
||
76 | * resources needed by your rules (extra properties, database connection settings, container, and etc). |
||
77 | * |
||
78 | * @param array $blocks |
||
79 | * |
||
80 | * @return ContextStorageInterface |
||
81 | */ |
||
82 | 4 | protected function createContextStorageFromBlocks(array $blocks): ContextStorageInterface |
|
86 | } |
||
87 |
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.