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 |
||
13 | View Code Duplication | class J215 extends Element implements ElementInterface |
|
|
|||
14 | { |
||
15 | const REG = 'J215'; |
||
16 | const LEVEL = 4; |
||
17 | const PARENT = ''; |
||
18 | |||
19 | protected $parameters = [ |
||
20 | 'cod_hist_fat' => [ |
||
21 | 'type' => 'string', |
||
22 | 'regex' => '^[A-Za-z0-9]$', |
||
23 | 'required' => true, |
||
24 | 'info' => 'Código do histórico do fato contábil.', |
||
25 | 'format' => '' |
||
26 | ], |
||
27 | 'desc_fat' => [ |
||
28 | 'type' => 'string', |
||
29 | 'regex' => '^[A-Za-z0-9]$', |
||
30 | 'required' => true, |
||
31 | 'info' => 'Descrição do Fato Contábil', |
||
32 | 'format' => '' |
||
33 | ], |
||
34 | 'vl_fat_cont' => [ |
||
35 | 'type' => 'numeric', |
||
36 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
37 | 'required' => true, |
||
38 | 'info' => 'Valor do fato contábil.', |
||
39 | 'format' => '19v2' |
||
40 | ], |
||
41 | 'ind_dc_fat' => [ |
||
42 | 'type' => 'string', |
||
43 | 'regex' => '^(D|C|P|N)$', |
||
44 | 'required' => true, |
||
45 | 'info' => 'Indicador de situação do saldo informado no campo anterior:' |
||
46 | .' D – Devedor' |
||
47 | .' C – Credor' |
||
48 | .' P – Subtotal ou total positivo' |
||
49 | .' N – Subtotal ou total negativo', |
||
50 | 'format' => '' |
||
51 | ] |
||
52 | ]; |
||
53 | |||
54 | /** |
||
55 | * Constructor |
||
56 | * @param \stdClass $std |
||
57 | */ |
||
58 | public function __construct(\stdClass $std) |
||
64 | } |
||
65 |
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.