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 |
||
50 | View Code Duplication | class Z0200 extends Element implements ElementInterface |
|
|
|||
51 | { |
||
52 | const REG = '0200'; |
||
53 | const LEVEL = 2; |
||
54 | const PARENT = ''; |
||
55 | |||
56 | protected $parameters = [ |
||
57 | 'COD_ITEM' => [ |
||
58 | 'type' => 'string', |
||
59 | 'regex' => '^.{1,60}$', |
||
60 | 'required' => true, |
||
61 | 'info' => 'Código do item', |
||
62 | 'format' => '' |
||
63 | ], |
||
64 | 'DESCR_ITEM' => [ |
||
65 | 'type' => 'string', |
||
66 | 'regex' => '^.{3,255}$', |
||
67 | 'required' => true, |
||
68 | 'info' => 'Descrição do item', |
||
69 | 'format' => '' |
||
70 | ], |
||
71 | 'COD_BARRA' => [ |
||
72 | 'type' => 'string', |
||
73 | 'regex' => '^(SEM GTIN)|([0-9]{8,14}|\-)$', |
||
74 | 'required' => false, |
||
75 | 'info' => 'Representação alfanumérico do código de barra do produto, se houver', |
||
76 | 'format' => '' |
||
77 | ], |
||
78 | 'COD_ANT_ITEM' => [ |
||
79 | 'type' => 'string', |
||
80 | 'regex' => '^.{1,6}$', |
||
81 | 'required' => false, |
||
82 | 'info' => 'Código anterior do item com relação à última informação anterior', |
||
83 | 'format' => '' |
||
84 | ], |
||
85 | 'UNID_INV' => [ |
||
86 | 'type' => 'string', |
||
87 | 'regex' => '^.{1,6}$', |
||
88 | 'required' => true, |
||
89 | 'info' => 'Unidade de medida utilizada na quantificação de estoques.', |
||
90 | 'format' => '' |
||
91 | ], |
||
92 | 'TIPO_ITEM' => [ |
||
93 | 'type' => 'string', |
||
94 | 'regex' => '^[0-9]{2}$', |
||
95 | 'required' => true, |
||
96 | 'info' => 'Tipo do item – Atividades Industriais, Comerciais e Serviços:' |
||
97 | . '00 – Mercadoria para Revenda;' |
||
98 | . '01 – Matéria-prima;' |
||
99 | . '02 – Embalagem;' |
||
100 | . '03 – Produto em Processo;' |
||
101 | . '04 – Produto Acabado;' |
||
102 | . '05 – Subproduto;' |
||
103 | . '06 – Produto Intermediário;' |
||
104 | . '07 – Material de Uso e Consumo;' |
||
105 | . '08 – Ativo Imobilizado;' |
||
106 | . '09 – Serviços;' |
||
107 | . '10 – Outros insumos;' |
||
108 | . '99 – Outras', |
||
109 | 'format' => '' |
||
110 | ], |
||
111 | 'COD_NCM' => [ |
||
112 | 'type' => 'string', |
||
113 | 'regex' => '^([0-9]{8})|([0-9]{2})$', |
||
114 | 'required' => false, |
||
115 | 'info' => 'Código da Nomenclatura Comum do Mercosul', |
||
116 | 'format' => '' |
||
117 | ], |
||
118 | 'EX_IPI' => [ |
||
119 | 'type' => 'string', |
||
120 | 'regex' => '^[0-9]{1,3}$', |
||
121 | 'required' => false, |
||
122 | 'info' => 'Código EX, conforme a TIPI', |
||
123 | 'format' => '' |
||
124 | ], |
||
125 | 'COD_GEN' => [ |
||
126 | 'type' => 'string', |
||
127 | 'regex' => '^[0-9]{2}$', |
||
128 | 'required' => false, |
||
129 | 'info' => 'Código do gênero do item, conforme a Tabela 4.2.1', |
||
130 | 'format' => '' |
||
131 | ], |
||
132 | 'COD_LST' => [ |
||
133 | 'type' => 'string', |
||
134 | 'regex' => '^([0-9]{2}\.[0-9]{2})$', |
||
135 | 'required' => false, |
||
136 | 'info' => 'Código do serviço conforme lista do Anexo I da ' |
||
137 | . 'Lei Complementar Federal nº 116/03.', |
||
138 | 'format' => '' |
||
139 | ], |
||
140 | 'ALIQ_ICMS' => [ |
||
141 | 'type' => 'numeric', |
||
142 | 'regex' => '^(\d*\.)?\d+$', |
||
143 | 'required' => false, |
||
144 | 'info' => 'Alíquota de ICMS aplicável ao item nas operações internas', |
||
145 | 'format' => '6v2' |
||
146 | ], |
||
147 | 'CEST' => [ |
||
148 | 'type' => 'string', |
||
149 | 'regex' => '^[0-9]{7}$', |
||
150 | 'required' => false, |
||
151 | 'info' => 'Código Especificador da Substituição Tributária', |
||
152 | 'format' => '' |
||
153 | ] |
||
154 | ]; |
||
155 | |||
156 | /** |
||
157 | * Constructor |
||
158 | * @param stdClass $std |
||
159 | */ |
||
160 | public function __construct(stdClass $std) |
||
165 | } |
||
166 |
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.