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 |
||
17 | class TextField extends Field |
||
18 | { |
||
19 | /** |
||
20 | * @var int|null |
||
21 | */ |
||
22 | public $min_length; |
||
23 | |||
24 | /** |
||
25 | * @var int|null |
||
26 | */ |
||
27 | public $max_length; |
||
28 | |||
29 | /** |
||
30 | * @var string|null |
||
31 | */ |
||
32 | protected $pattern; |
||
33 | |||
34 | /** |
||
35 | * @var string|null |
||
36 | */ |
||
37 | protected $pattern_error; |
||
38 | |||
39 | /** |
||
40 | * @param string $pattern regular expression pattern (optional; no delimiters, modifiers or anchors) |
||
41 | * @param string $error error message to apply on pattern mismatch |
||
42 | */ |
||
43 | 5 | public function setPattern($pattern, $error) |
|
48 | |||
49 | /** |
||
50 | * {@inheritdoc} |
||
51 | */ |
||
52 | 6 | View Code Duplication | public function renderInput(InputRenderer $renderer, InputModel $model, array $attr) |
67 | |||
68 | /** |
||
69 | * @inheritdoc |
||
70 | */ |
||
71 | 7 | public function createValidators() |
|
91 | } |
||
92 |
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.