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 | class A170 extends Element implements ElementInterface |
||
10 | { |
||
11 | const REG = 'A170'; |
||
12 | const LEVEL = 4; |
||
13 | const PARENT = 'A100'; |
||
14 | |||
15 | protected $parameters = [ |
||
16 | 'NUM_ITEM' => [ |
||
17 | 'type' => 'numeric', |
||
18 | 'regex' => '^([1-9]{1})(\d{0,3})$', |
||
19 | 'required' => false, |
||
20 | 'info' => 'Número seqüencial do item no documento fiscal', |
||
21 | 'format' => '' |
||
22 | ], |
||
23 | 'COD_ITEM' => [ |
||
24 | 'type' => 'string', |
||
25 | 'regex' => '^.{0,60}$', |
||
26 | 'required' => false, |
||
27 | 'info' => 'Código do item (campo 02 do Registro 0200)', |
||
28 | 'format' => '' |
||
29 | ], |
||
30 | 'DESCR_COMPL' => [ |
||
31 | 'type' => 'string', |
||
32 | 'regex' => '^(.*)$', |
||
33 | 'required' => false, |
||
34 | 'info' => 'Descrição complementar do item como adotado no documento fiscal', |
||
35 | 'format' => '' |
||
36 | ], |
||
37 | 'VL_ITEM' => [ |
||
38 | 'type' => 'numeric', |
||
39 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
40 | 'required' => false, |
||
41 | 'info' => 'Valor total do item (mercadorias ou serviços)', |
||
42 | 'format' => '15v2' |
||
43 | ], |
||
44 | 'VL_DESC' => [ |
||
45 | 'type' => 'numeric', |
||
46 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
47 | 'required' => false, |
||
48 | 'info' => 'Valor do desconto comercial / exclusão da base de cálculo do PIS/PASEP e da COFINS', |
||
49 | 'format' => '15v2' |
||
50 | ], |
||
51 | 'NAT_BC_CRED' => [ |
||
52 | 'type' => 'string', |
||
53 | 'regex' => '^.{2}$', |
||
54 | 'required' => false, |
||
55 | 'info' => 'Código da base de cálculo do crédito, conforme a Tabela indicada no item 4.3.7, |
||
56 | caso seja informado código representativo de crédito no Campo 09 (CST_PIS) |
||
57 | ou no Campo 13 (CST_COFINS).', |
||
58 | 'format' => '' |
||
59 | ], |
||
60 | 'IND_ORIG_CRED' => [ |
||
61 | 'type' => 'string', |
||
62 | 'regex' => '^(0|1)$', |
||
63 | 'required' => false, |
||
64 | 'info' => 'Indicador da origem do crédito: 0 – Operação no Mercado Interno', |
||
65 | 'format' => '' |
||
66 | ], |
||
67 | 'CST_PIS' => [ |
||
68 | 'type' => 'numeric', |
||
69 | 'regex' => '^(\d{2})$', |
||
70 | 'required' => false, |
||
71 | 'info' => 'Código da Situação Tributária referente ao PIS/PASEP – Tabela 4.3.3.', |
||
72 | 'format' => '' |
||
73 | ], |
||
74 | 'VL_BC_PIS' => [ |
||
75 | 'type' => 'numeric', |
||
76 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
77 | 'required' => false, |
||
78 | 'info' => 'Valor da base de cálculo do PIS/PASEP.', |
||
79 | 'format' => '15v2' |
||
80 | ], |
||
81 | 'ALIQ_PIS' => [ |
||
82 | 'type' => 'numeric', |
||
83 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
84 | 'required' => false, |
||
85 | 'info' => 'Alíquota do PIS/PASEP (em percentual)', |
||
86 | 'format' => '15v2' |
||
87 | ], |
||
88 | 'VL_PIS' => [ |
||
89 | 'type' => 'numeric', |
||
90 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
91 | 'required' => false, |
||
92 | 'info' => 'Valor do PIS/PASEP', |
||
93 | 'format' => '15v2' |
||
94 | ], |
||
95 | 'CST_COFINS' => [ |
||
96 | 'type' => 'numeric', |
||
97 | 'regex' => '^(\d{2})$', |
||
98 | 'required' => false, |
||
99 | 'info' => 'Código da Situação Tributária referente ao COFINS – Tabela 4.3.4.', |
||
100 | 'format' => '' |
||
101 | ], |
||
102 | 'VL_BC_COFINS' => [ |
||
103 | 'type' => 'numeric', |
||
104 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
105 | 'required' => false, |
||
106 | 'info' => 'Valor da base de cálculo da COFINS', |
||
107 | 'format' => '15v2' |
||
108 | ], |
||
109 | 'ALIQ_COFINS' => [ |
||
110 | 'type' => 'numeric', |
||
111 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
112 | 'required' => false, |
||
113 | 'info' => 'Alíquota do COFINS (em percentual)', |
||
114 | 'format' => '6v2' |
||
115 | ], |
||
116 | 'VL_COFINS' => [ |
||
117 | 'type' => 'numeric', |
||
118 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
119 | 'required' => false, |
||
120 | 'info' => 'Valor da COFINS', |
||
121 | 'format' => '15v2' |
||
122 | ], |
||
123 | 'COD_CTA' => [ |
||
124 | 'type' => 'string', |
||
125 | 'regex' => '^.{0,255}$', |
||
126 | 'required' => false, |
||
127 | 'info' => 'Código da conta analítica contábil debitada/creditada', |
||
128 | 'format' => '' |
||
129 | ], |
||
130 | 'COD_CCUS' => [ |
||
131 | 'type' => 'string', |
||
132 | 'regex' => '^.{0,255}$', |
||
133 | 'required' => false, |
||
134 | 'info' => 'Código do centro de custos', |
||
135 | 'format' => '' |
||
136 | ], |
||
137 | |||
138 | ]; |
||
139 | |||
140 | /** |
||
141 | * Constructor |
||
142 | * @param \stdClass $std |
||
143 | */ |
||
144 | public function __construct(\stdClass $std) |
||
150 | |||
151 | /** |
||
152 | * Arre |
||
153 | * @param float $valor |
||
154 | * @return float|int |
||
155 | */ |
||
156 | private function roundFloat($valor) |
||
160 | |||
161 | public function postValidation() |
||
179 | } |
||
180 |
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.