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 E250 extends Element implements ElementInterface |
|
|
|||
10 | { |
||
11 | const REG = 'E250'; |
||
12 | const LEVEL = 4; |
||
13 | const PARENT = 'E210'; |
||
14 | |||
15 | protected $parameters = [ |
||
16 | 'COD_OR' => [ |
||
17 | 'type' => 'string', |
||
18 | 'regex' => '^.[001|002|006|999]+$', |
||
19 | 'required' => true, |
||
20 | 'info' => 'Código da obrigação a recolher, conforme a Tabela 5.4', |
||
21 | 'format' => '' |
||
22 | ], |
||
23 | 'VL_OR' => [ |
||
24 | 'type' => 'numeric', |
||
25 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
26 | 'required' => true, |
||
27 | 'info' => 'Valor da obrigação ICMS ST a recolher', |
||
28 | 'format' => '15v2' |
||
29 | ], |
||
30 | 'DT_VCTO' => [ |
||
31 | 'type' => 'integer', |
||
32 | 'regex' => '^(0[1-9]|[1-2][0-9]|31(?!(?:0[2469]|11))|30(?!02))(0[1-9]|1[0-2])([12]\d{3})$', |
||
33 | 'required' => true, |
||
34 | 'info' => 'Data de vencimento da obrigação', |
||
35 | 'format' => '' |
||
36 | ], |
||
37 | 'COD_REC' => [ |
||
38 | 'type' => 'string', |
||
39 | 'regex' => '^.*$', |
||
40 | 'required' => true, |
||
41 | 'info' => 'Código de receita referente à obrigação, próprio da unidade da ' |
||
42 | .'federação do contribuinte substituído.', |
||
43 | 'format' => '' |
||
44 | ], |
||
45 | 'NUM_PROC' => [ |
||
46 | 'type' => 'string', |
||
47 | 'regex' => '^.{1,15}$', |
||
48 | 'required' => false, |
||
49 | 'info' => 'Número do processo ou auto de infração ao qual a obrigação está vinculada, se houver', |
||
50 | 'format' => '' |
||
51 | ], |
||
52 | 'IND_PROC' => [ |
||
53 | 'type' => 'string', |
||
54 | 'regex' => '^[0|1|2|9]$', |
||
55 | 'required' => false, |
||
56 | 'info' => 'Indicador da origem do processo: ' |
||
57 | .'0- SEFAZ; ' |
||
58 | .'1- Justiça Federal; ' |
||
59 | .'2- Justiça Estadual; ' |
||
60 | .'9- Outros', |
||
61 | 'format' => '' |
||
62 | ], |
||
63 | 'PROC' => [ |
||
64 | 'type' => 'string', |
||
65 | 'regex' => '^.*$', |
||
66 | 'required' => false, |
||
67 | 'info' => 'Descrição resumida do processo que embasou o lançamento', |
||
68 | 'format' => '' |
||
69 | ], |
||
70 | 'TXT_COMPL' => [ |
||
71 | 'type' => 'string', |
||
72 | 'regex' => '^.*$', |
||
73 | 'required' => false, |
||
74 | 'info' => 'Descrição complementar das obrigações a recolher', |
||
75 | 'format' => '' |
||
76 | ], |
||
77 | 'MES_REF' => [ |
||
78 | 'type' => 'integer', |
||
79 | 'regex' => '^((?!(13^))|30(?!02))(0[1-9]|1[0-2])([12]\d{3})$', |
||
80 | 'required' => true, |
||
81 | 'info' => 'Informe o mês de referência no formato “mmaaaa”', |
||
82 | 'format' => '' |
||
83 | ] |
||
84 | ]; |
||
85 | |||
86 | /** |
||
87 | * Constructor |
||
88 | * @param \stdClass $std |
||
89 | */ |
||
90 | public function __construct(\stdClass $std) |
||
96 | |||
97 | public function postValidation() |
||
113 | } |
||
114 |
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.