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 | View Code Duplication | class C120 extends Element implements ElementInterface |
|
|
|||
17 | { |
||
18 | const REG = 'C120'; |
||
19 | const LEVEL = 3; |
||
20 | const PARENT = 'C100'; |
||
21 | |||
22 | protected $parameters = [ |
||
23 | 'COD_DOC_IMP' => [ |
||
24 | 'type' => 'string', |
||
25 | 'regex' => '^(0|1)+$', |
||
26 | 'required' => true, |
||
27 | 'info' => 'Documento de importação', |
||
28 | 'format' => '' |
||
29 | ], |
||
30 | 'NUM_DOC_IMP' => [ |
||
31 | 'type' => 'string', |
||
32 | 'regex' => '^([0-9]{10,12})+$', |
||
33 | 'required' => true, |
||
34 | 'info' => 'Número do documento de Importação', |
||
35 | 'format' => '' |
||
36 | ], |
||
37 | 'PIS_IMP' => [ |
||
38 | 'type' => 'numeric', |
||
39 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
40 | 'required' => false, |
||
41 | 'info' => 'Valor pago de PIS na importação', |
||
42 | 'format' => '15v2' |
||
43 | ], |
||
44 | 'COFINS_IMP' => [ |
||
45 | 'type' => 'numeric', |
||
46 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
47 | 'required' => false, |
||
48 | 'info' => 'Valor pago de COFINS na importação', |
||
49 | 'format' => '15v2' |
||
50 | ], |
||
51 | 'NUM_ACDRAW' => [ |
||
52 | 'type' => 'string', |
||
53 | 'regex' => '^([0-9]{1,20})$', |
||
54 | 'required' => false, |
||
55 | 'info' => 'Número do Ato Concessório do regime Drawback', |
||
56 | 'format' => '' |
||
57 | ], |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * Constructor |
||
62 | * @param \stdClass $std |
||
63 | */ |
||
64 | public function __construct(\stdClass $std) |
||
69 | } |
||
70 |
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.