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 |
||
16 | class IntField extends TextField |
||
17 | { |
||
18 | /** |
||
19 | * @var int|null minimum value |
||
20 | */ |
||
21 | public $min_value; |
||
22 | |||
23 | /** |
||
24 | * var int|null maximum value |
||
25 | */ |
||
26 | public $max_value; |
||
27 | |||
28 | /** |
||
29 | * @param InputModel $model |
||
30 | * |
||
31 | * @return int|null |
||
32 | * |
||
33 | * @throws UnexpectedValueException if unable to parse the input |
||
34 | */ |
||
35 | 1 | View Code Duplication | public function getValue(InputModel $model) |
49 | |||
50 | /** |
||
51 | * @param InputModel $model |
||
52 | * @param int|null $value |
||
53 | * |
||
54 | * @return void |
||
55 | * |
||
56 | * @throws InvalidArgumentException if the given value is unacceptable. |
||
57 | */ |
||
58 | 1 | public function setValue(InputModel $model, $value) |
|
68 | |||
69 | /** |
||
70 | * @inheritdoc |
||
71 | */ |
||
72 | 2 | public function createValidators() |
|
90 | } |
||
91 |
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.