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 |
||
9 | View Code Duplication | class E313 extends Element implements ElementInterface |
|
|
|||
10 | { |
||
11 | const REG = 'E313'; |
||
12 | const LEVEL = 5; |
||
13 | const PARENT = 'E311'; |
||
14 | |||
15 | protected $parameters = [ |
||
16 | 'COD_PART' => [ |
||
17 | 'type' => 'string', |
||
18 | 'regex' => '^\d{1,60}+$', |
||
19 | 'required' => true, |
||
20 | 'info' => 'Código do participante (campo 02 do Registro 0150)', |
||
21 | 'format' => '' |
||
22 | ], |
||
23 | 'COD_MOD' => [ |
||
24 | 'type' => 'string', |
||
25 | 'regex' => '^\d{2}$', |
||
26 | 'required' => true, |
||
27 | 'info' => 'Código do modelo do documento fiscal, conforme a Tabela 4.1.1', |
||
28 | 'format' => '' |
||
29 | ], |
||
30 | 'SER' => [ |
||
31 | 'type' => 'string', |
||
32 | 'regex' => '^\d{1,4}+$', |
||
33 | 'required' => false, |
||
34 | 'info' => 'Série do documento fiscal', |
||
35 | 'format' => '' |
||
36 | ], |
||
37 | 'SUB' => [ |
||
38 | 'type' => 'integer', |
||
39 | 'regex' => '^\d{1,3}+$', |
||
40 | 'required' => false, |
||
41 | 'info' => 'Subsérie do documento fiscal', |
||
42 | 'format' => '' |
||
43 | ], |
||
44 | 'NUM_DOC' => [ |
||
45 | 'type' => 'integer', |
||
46 | 'regex' => '^([1-9])([0-9]{1,8}|)$', |
||
47 | 'required' => true, |
||
48 | 'info' => 'Número do documento fiscal', |
||
49 | 'format' => '' |
||
50 | ], |
||
51 | 'DT_DOC' => [ |
||
52 | 'type' => 'integer', |
||
53 | 'regex' => '^(0[1-9]|[1-2][0-9]|31(?!(?:0[2469]|11))|30(?!02))(0[1-9]|1[0-2])([12]\d{3})$', |
||
54 | 'required' => true, |
||
55 | 'info' => 'Data da emissão do documento fiscal' , |
||
56 | 'format' => '' |
||
57 | ], |
||
58 | 'COD_ITEM' => [ |
||
59 | 'type' => 'string', |
||
60 | 'regex' => '^.{1,60}$', |
||
61 | 'required' => false, |
||
62 | 'info' => 'Código do item (campo 02 do Registro 0200)', |
||
63 | 'format' => '' |
||
64 | ], |
||
65 | 'VL_AJ_ITEM' => [ |
||
66 | 'type' => 'numeric', |
||
67 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
68 | 'required' => true, |
||
69 | 'info' => 'Valor do ajuste para a operação/item', |
||
70 | 'format' => '15v2' |
||
71 | ], |
||
72 | 'CHV_DOCE' => [ |
||
73 | 'type' => 'string', |
||
74 | 'regex' => '^\d{44}+$', |
||
75 | 'required' => false, |
||
76 | 'info' => 'Chave do Documento Eletrônico', |
||
77 | 'format' => '' |
||
78 | ] |
||
79 | ]; |
||
80 | |||
81 | /** |
||
82 | * Constructor |
||
83 | * @param \stdClass $std |
||
84 | */ |
||
85 | public function __construct(\stdClass $std) |
||
91 | |||
92 | public function postValidation() |
||
102 | } |
||
103 |
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.