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 |
||
24 | class D100 extends Element implements ElementInterface |
||
25 | { |
||
26 | const REG = 'D100'; |
||
27 | const LEVEL = 2; |
||
28 | const PARENT = 'D001'; |
||
29 | protected $parameters = [ |
||
30 | 'IND_OPER' => [ |
||
31 | 'type' => 'string', |
||
32 | 'regex' => '^[0-1]{1}$', |
||
33 | 'required' => true, |
||
34 | 'info' => 'Indicador do tipo de operação', |
||
35 | 'format' => '' |
||
36 | ], |
||
37 | 'IND_EMIT' => [ |
||
38 | 'type' => 'string', |
||
39 | 'regex' => '^[0-1]{1}$', |
||
40 | 'required' => true, |
||
41 | 'info' => 'Indicador do emitente do documento fiscal', |
||
42 | 'format' => '' |
||
43 | ], |
||
44 | 'COD_PART' => [ |
||
45 | 'type' => 'string', |
||
46 | 'regex' => '^.{1,60}$', |
||
47 | 'required' => false, |
||
48 | 'info' => 'Código do participante (campo 02 do Registro 0150):', |
||
49 | 'format' => '' |
||
50 | ], |
||
51 | 'COD_MOD' => [ |
||
52 | 'type' => 'string', |
||
53 | 'regex' => '^(07|08|8B|09|10|11|26|27|57|63|67)$', |
||
54 | 'required' => true, |
||
55 | 'info' => 'Código do modelo do documento fiscalValor total do estoque', |
||
56 | 'format' => '' |
||
57 | ], |
||
58 | 'COD_SIT' => [ |
||
59 | 'type' => 'numeric', |
||
60 | 'regex' => '^(00|01|02|03|04|05|06|07|08)$', |
||
61 | 'required' => true, |
||
62 | 'info' => 'Código da situação do documento fiscal', |
||
63 | 'format' => '' |
||
64 | ], |
||
65 | 'SER' => [ |
||
66 | 'type' => 'string', |
||
67 | 'regex' => '^.{0,4}$', |
||
68 | 'required' => false, |
||
69 | 'info' => 'Série do documento fiscal', |
||
70 | 'format' => '' |
||
71 | ], |
||
72 | 'SUB' => [ |
||
73 | 'type' => 'string', |
||
74 | 'regex' => '^.{1,3}$', |
||
75 | 'required' => false, |
||
76 | 'info' => 'Subsérie do documento fiscal ', |
||
77 | 'format' => '' |
||
78 | ], |
||
79 | 'NUM_DOC' => [ |
||
80 | 'type' => 'numeric', |
||
81 | 'regex' => '^([0-9]{1,9})?$', |
||
82 | 'required' => true, |
||
83 | 'info' => 'Número do documento fiscal', |
||
84 | 'format' => '' |
||
85 | ], |
||
86 | 'CHV_CTE' => [ |
||
87 | 'type' => 'numeric', |
||
88 | 'regex' => '^[0-9]{44}$', |
||
89 | 'required' => false, |
||
90 | 'info' => 'Chave do Conhecimento de Transporte Eletrônico ou do Bilhete de Passagem Eletrônico', |
||
91 | 'format' => '' |
||
92 | ], |
||
93 | 'DT_DOC' => [ |
||
94 | 'type' => 'string', |
||
95 | 'regex' => '^(0[1-9]|[1-2][0-9]|31(?!(?:0[2469]|11))|30(?!02))(0[1-9]|1[0-2])([12]\d{3})$', |
||
96 | 'required' => false, |
||
97 | 'info' => 'Data da emissão do documento fiscal', |
||
98 | 'format' => '' |
||
99 | ], |
||
100 | 'DT_A_P' => [ |
||
101 | 'type' => 'string', |
||
102 | 'regex' => '^(0[1-9]|[1-2][0-9]|31(?!(?:0[2469]|11))|30(?!02))(0[1-9]|1[0-2])([12]\d{3})$', |
||
103 | 'required' => false, |
||
104 | 'info' => 'Data da aquisição ou da prestação do serviço', |
||
105 | 'format' => '' |
||
106 | ], |
||
107 | 'TP_CT_E' => [ |
||
108 | 'type' => 'numeric', |
||
109 | 'regex' => '^[0-1]{1}$', |
||
110 | 'required' => false, |
||
111 | 'info' => 'Tipo de Conhecimento de Transporte Eletrônico', |
||
112 | 'format' => '' |
||
113 | ], |
||
114 | 'CHV_CTE_REF' => [ |
||
115 | 'type' => 'numeric', |
||
116 | 'regex' => '^[0-9]{44}$', |
||
117 | 'required' => false, |
||
118 | 'info' => 'Chave do CT-e de referência', |
||
119 | 'format' => '' |
||
120 | ], |
||
121 | 'VL_DOC' => [ |
||
122 | 'type' => 'numeric', |
||
123 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
124 | 'required' => true, |
||
125 | 'info' => 'Valor total do documento fiscal', |
||
126 | 'format' => '15v2' |
||
127 | ], |
||
128 | 'VL_DESC' => [ |
||
129 | 'type' => 'numeric', |
||
130 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
131 | 'required' => false, |
||
132 | 'info' => 'Valor total do desconto', |
||
133 | 'format' => '15v2' |
||
134 | ], |
||
135 | 'IND_FRT' => [ |
||
136 | 'type' => 'numeric', |
||
137 | 'regex' => '^(0|1|2|9)$', |
||
138 | 'required' => false, |
||
139 | 'info' => 'Indicador do tipo do frete', |
||
140 | 'format' => '' |
||
141 | ], |
||
142 | 'VL_SERV' => [ |
||
143 | 'type' => 'numeric', |
||
144 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
145 | 'required' => true, |
||
146 | 'info' => 'Valor total da prestação de serviço', |
||
147 | 'format' => '15v2' |
||
148 | ], |
||
149 | 'VL_BC_ICMS' => [ |
||
150 | 'type' => 'numeric', |
||
151 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
152 | 'required' => false, |
||
153 | 'info' => 'Valor da base de cálculo do ICMS', |
||
154 | 'format' => '15v2' |
||
155 | ], |
||
156 | 'VL_ICMS' => [ |
||
157 | 'type' => 'numeric', |
||
158 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
159 | 'required' => false, |
||
160 | 'info' => 'Valor do ICMS', |
||
161 | 'format' => '15v2' |
||
162 | ], |
||
163 | 'VL_NT' => [ |
||
164 | 'type' => 'numeric', |
||
165 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
166 | 'required' => false, |
||
167 | 'info' => 'Valor não-tributado', |
||
168 | 'format' => '15v2' |
||
169 | ], |
||
170 | 'COD_INF' => [ |
||
171 | 'type' => 'string', |
||
172 | 'regex' => '^.{1,6}$', |
||
173 | 'required' => false, |
||
174 | 'info' => 'Código da informação complementar do documento fiscal', |
||
175 | 'format' => '' |
||
176 | ], |
||
177 | 'COD_CTA' => [ |
||
178 | 'type' => 'string', |
||
179 | 'regex' => '^.{1,255}$', |
||
180 | 'required' => false, |
||
181 | 'info' => 'Código da conta analítica contábil debitada/creditada', |
||
182 | 'format' => '' |
||
183 | ], |
||
184 | 'COD_MUN_ORIG' => [ |
||
185 | 'type' => 'numeric', |
||
186 | 'regex' => '^[0-9]{7}$', |
||
187 | 'required' => false, |
||
188 | 'info' => 'Código do município de origem do serviço, conforme a tabela IBGE', |
||
189 | 'format' => '' |
||
190 | ], |
||
191 | 'COD_MUN_DEST' => [ |
||
192 | 'type' => 'numeric', |
||
193 | 'regex' => '^[0-9]{7}$', |
||
194 | 'required' => false, |
||
195 | 'info' => 'Código do município de destino, conforme a tabela IBGE', |
||
196 | 'format' => '' |
||
197 | ], |
||
198 | ]; |
||
199 | |||
200 | |||
201 | /** |
||
202 | * Constructor |
||
203 | * @param \stdClass $std |
||
204 | */ |
||
205 | public function __construct(\stdClass $std) |
||
211 | |||
212 | |||
213 | /** |
||
214 | * Aqui são colocadas validações adicionais que requerem mais logica |
||
215 | * e processamento |
||
216 | * Deve ser usado apenas quando necessário |
||
217 | * @throws \InvalidArgumentException |
||
218 | */ |
||
219 | public function postValidation() |
||
286 | } |
||
287 |
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.