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 declare(strict_types=1); |
||
32 | abstract class BaseTwoValueComparision extends BaseRule implements ComparisionInterface |
||
33 | { |
||
34 | /** |
||
35 | * Property key. |
||
36 | */ |
||
37 | const PROPERTY_LOWER_VALUE = self::PROPERTY_LAST + 1; |
||
38 | |||
39 | /** |
||
40 | * Property key. |
||
41 | */ |
||
42 | const PROPERTY_UPPER_VALUE = self::PROPERTY_LOWER_VALUE + 1; |
||
43 | |||
44 | /** |
||
45 | * Property key. |
||
46 | */ |
||
47 | const PROPERTY_TWO_VALUE_LAST = self::PROPERTY_UPPER_VALUE; |
||
48 | |||
49 | /** |
||
50 | * @var mixed |
||
51 | */ |
||
52 | private $lowerValue; |
||
53 | |||
54 | /** |
||
55 | * @var mixed |
||
56 | */ |
||
57 | private $upperValue; |
||
58 | |||
59 | /** |
||
60 | * @var int |
||
61 | */ |
||
62 | private $errorCode; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | private $messageTemplate; |
||
68 | |||
69 | /** |
||
70 | * @var array |
||
71 | */ |
||
72 | private $messageParams; |
||
73 | |||
74 | /** |
||
75 | * @param mixed $lowerValue |
||
76 | * @param mixed $upperValue |
||
77 | * @param int $errorCode |
||
78 | * @param string $messageTemplate |
||
79 | * @param array $messageParams |
||
80 | */ |
||
81 | 5 | View Code Duplication | public function __construct($lowerValue, $upperValue, int $errorCode, string $messageTemplate, array $messageParams) |
91 | |||
92 | /** |
||
93 | * @inheritdoc |
||
94 | */ |
||
95 | 5 | View Code Duplication | public function toBlock(): ExecutionBlockInterface |
114 | |||
115 | /** |
||
116 | * @return mixed |
||
117 | */ |
||
118 | 5 | public function getLowerValue() |
|
122 | |||
123 | /** |
||
124 | * @return mixed |
||
125 | */ |
||
126 | 5 | public function getUpperValue() |
|
130 | |||
131 | /** |
||
132 | * @return int |
||
133 | */ |
||
134 | 5 | protected function getErrorCode(): int |
|
138 | |||
139 | /** |
||
140 | * @return string |
||
141 | */ |
||
142 | 5 | protected function getMessageTemplate(): string |
|
146 | |||
147 | /** |
||
148 | * @return array |
||
149 | */ |
||
150 | 5 | public function getMessageParameters(): array |
|
154 | |||
155 | /** |
||
156 | * @param ContextInterface $context |
||
157 | * |
||
158 | * @return mixed |
||
159 | */ |
||
160 | 4 | protected static function readLowerValue(ContextInterface $context) |
|
166 | |||
167 | /** |
||
168 | * @param ContextInterface $context |
||
169 | * |
||
170 | * @return mixed |
||
171 | */ |
||
172 | 4 | protected static function readUpperValue(ContextInterface $context) |
|
178 | } |
||
179 |
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.