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); |
||
35 | abstract class BaseOneValueComparision extends BaseRule implements ComparisionInterface |
||
36 | { |
||
37 | /** |
||
38 | * Property key. |
||
39 | */ |
||
40 | const PROPERTY_VALUE = self::PROPERTY_LAST + 1; |
||
41 | |||
42 | /** |
||
43 | * Property key. |
||
44 | */ |
||
45 | const PROPERTY_ONE_VALUE_LAST = self::PROPERTY_VALUE; |
||
46 | |||
47 | /** |
||
48 | * @var mixed |
||
49 | */ |
||
50 | private $value; |
||
51 | |||
52 | /** |
||
53 | * @var int |
||
54 | */ |
||
55 | private $errorCode; |
||
56 | |||
57 | /** |
||
58 | * @var string |
||
59 | */ |
||
60 | private $messageTemplate; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | private $messageParams; |
||
66 | |||
67 | /** |
||
68 | * @param mixed $value |
||
69 | * @param int $errorCode |
||
70 | * @param string $messageTemplate |
||
71 | * @param array $messageParams |
||
72 | */ |
||
73 | 6 | View Code Duplication | public function __construct($value, int $errorCode, string $messageTemplate, array $messageParams) |
82 | |||
83 | /** |
||
84 | * @inheritdoc |
||
85 | */ |
||
86 | 6 | View Code Duplication | public function toBlock(): ExecutionBlockInterface |
102 | |||
103 | /** |
||
104 | * @return mixed |
||
105 | */ |
||
106 | 6 | public function getValue() |
|
110 | |||
111 | /** |
||
112 | * @return int |
||
113 | */ |
||
114 | 6 | protected function getErrorCode(): int |
|
118 | |||
119 | /** |
||
120 | * @return string |
||
121 | */ |
||
122 | 6 | protected function getMessageTemplate(): string |
|
126 | |||
127 | /** |
||
128 | * @return mixed |
||
129 | */ |
||
130 | 6 | public function getMessageParameters(): array |
|
134 | |||
135 | /** |
||
136 | * @param ContextInterface $context |
||
137 | * |
||
138 | * @return mixed |
||
139 | */ |
||
140 | 5 | protected static function readValue(ContextInterface $context) |
|
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.