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 |
||
13 | class InRule extends OrRule |
||
14 | { |
||
15 | use Trait_RuleWithField; |
||
16 | |||
17 | /** @var string operator */ |
||
18 | const operator = 'in'; |
||
19 | |||
20 | /** @var array $native_possibilities */ |
||
21 | protected $native_possibilities = []; |
||
22 | |||
23 | /** |
||
24 | * @param string $field The field to apply the rule on. |
||
25 | * @param mixed $possibilities The values the field can belong to. |
||
26 | */ |
||
27 | 50 | public function __construct($field, $possibilities, array $options=[]) |
|
36 | |||
37 | /** |
||
38 | * @return array |
||
39 | */ |
||
40 | 54 | public function getPossibilities() |
|
44 | |||
45 | /** |
||
46 | * @param mixed possibilities |
||
47 | * |
||
48 | * @return InRule $this |
||
49 | */ |
||
50 | 51 | public function addPossibilities($possibilities) |
|
86 | |||
87 | /** |
||
88 | * @param array possibilities |
||
89 | * |
||
90 | * @return InRule $this |
||
91 | */ |
||
92 | public function addOperand( AbstractRule $operand ) |
||
98 | |||
99 | /** |
||
100 | * @param mixed possibilities |
||
101 | * |
||
102 | * @return InRule $this |
||
103 | */ |
||
104 | 14 | public function setPossibilities($possibilities) |
|
112 | |||
113 | /** |
||
114 | * @param array possibilities |
||
115 | * |
||
116 | * @return InRule $this |
||
117 | */ |
||
118 | 5 | public function setOperands(array $operands) |
|
124 | |||
125 | /** |
||
126 | * |
||
127 | */ |
||
128 | 49 | protected function checkOperandAndExtractValue($operand) |
|
143 | |||
144 | /** |
||
145 | * @return InRule $this |
||
146 | */ |
||
147 | 22 | public function getOperands() |
|
160 | |||
161 | /** |
||
162 | * @return array |
||
163 | */ |
||
164 | 51 | public function getValues() |
|
168 | |||
169 | /** |
||
170 | * @param array $options + show_instance=false Display the operator of the rule or its instance id |
||
171 | * |
||
172 | * @return array |
||
173 | */ |
||
174 | 55 | View Code Duplication | public function toArray(array $options=[]) |
204 | |||
205 | /** |
||
206 | */ |
||
207 | 1 | public function toString(array $options=[]) |
|
221 | |||
222 | /** |
||
223 | */ |
||
224 | 36 | public function isNormalizationAllowed(array $contextual_options) |
|
232 | |||
233 | /** |
||
234 | * @return bool If the InRule can have a solution or not |
||
235 | */ |
||
236 | 2 | public function hasSolution(array $contextual_options=[]) |
|
240 | |||
241 | /** |
||
242 | * There is no negations into an InRule |
||
243 | */ |
||
244 | 20 | public function removeNegations(array $contextual_options) |
|
248 | |||
249 | /**/ |
||
250 | } |
||
251 |
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.