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 Filter extends BaseRule |
||
30 | { |
||
31 | /** |
||
32 | * Property key. |
||
33 | */ |
||
34 | const PROPERTY_FILTER_ID = self::PROPERTY_LAST + 1; |
||
35 | |||
36 | /** |
||
37 | * Property key. |
||
38 | */ |
||
39 | const PROPERTY_FILTER_OPTIONS = self::PROPERTY_FILTER_ID + 1; |
||
40 | |||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | private $filterId; |
||
45 | |||
46 | /** |
||
47 | * @var mixed |
||
48 | */ |
||
49 | private $filterOptions; |
||
50 | |||
51 | /** |
||
52 | * For filter ID and options see @link http://php.net/manual/en/filter.filters.php |
||
53 | * |
||
54 | * @param int $filterId |
||
55 | * @param null $options |
||
56 | */ |
||
57 | 1 | public function __construct(int $filterId, $options = null) |
|
62 | |||
63 | /** |
||
64 | * @inheritdoc |
||
65 | */ |
||
66 | 1 | View Code Duplication | public function toBlock(): ExecutionBlockInterface |
75 | |||
76 | /** |
||
77 | * @param mixed $value |
||
78 | * @param ContextInterface $context |
||
79 | * |
||
80 | * @return array |
||
81 | * |
||
82 | * @SuppressWarnings(PHPMD.StaticAccess) |
||
83 | */ |
||
84 | 1 | public static function execute($value, ContextInterface $context): array |
|
97 | |||
98 | /** |
||
99 | * @return int |
||
100 | */ |
||
101 | 1 | private function getFilterId(): int |
|
105 | |||
106 | /** |
||
107 | * @return mixed |
||
108 | */ |
||
109 | 1 | private function getFilterOptions() |
|
113 | } |
||
114 |
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.