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 D400 extends Element implements ElementInterface |
|
|
|
|||
| 10 | { |
||
| 11 | const REG = 'D400'; |
||
| 12 | const LEVEL = 2; |
||
| 13 | const PARENT = ''; |
||
| 14 | |||
| 15 | protected $parameters = [ |
||
| 16 | 'COD_PART' => [ |
||
| 17 | 'type' => 'string', |
||
| 18 | 'regex' => '^[0-9]{60}$', |
||
| 19 | 'required' => true, |
||
| 20 | 'info' => 'Código do participante (campo 02 do Registro 0150):' |
||
| 21 | . '- agência, filial ou posto', |
||
| 22 | 'format' => '' |
||
| 23 | ], |
||
| 24 | 'COD_MOD' => [ |
||
| 25 | 'type' => 'string', |
||
| 26 | 'regex' => '^[0-9]{2}$', |
||
| 27 | 'required' => true, |
||
| 28 | 'info' => 'Código do modelo do documento fiscal, conforme a tabela 4.1.1', |
||
| 29 | 'format' => '' |
||
| 30 | ], |
||
| 31 | 'COD_SIT' => [ |
||
| 32 | 'type' => 'numeric', |
||
| 33 | 'regex' => '', |
||
| 34 | 'required' => true, |
||
| 35 | 'info' => 'Código da situação do documento fiscal, conforme a tabela 4.1.2', |
||
| 36 | 'format' => '' |
||
| 37 | ], |
||
| 38 | 'SER' => [ |
||
| 39 | 'type' => 'string', |
||
| 40 | 'regex' => '^[0-9]{4}$', |
||
| 41 | 'required' => true, |
||
| 42 | 'info' => 'Série do documento fiscal', |
||
| 43 | 'format' => '' |
||
| 44 | ], |
||
| 45 | 'SUB' => [ |
||
| 46 | 'type' => 'numeric', |
||
| 47 | 'regex' => '^[0-9]{3}$', |
||
| 48 | 'required' => true, |
||
| 49 | 'info' => 'Subsérie do documento fiscal', |
||
| 50 | 'format' => '' |
||
| 51 | ], |
||
| 52 | 'NUM_DOC' => [ |
||
| 53 | 'type' => 'numeric', |
||
| 54 | 'regex' => '^[0-9]{6}$', |
||
| 55 | 'required' => true, |
||
| 56 | 'info' => 'Número do documento fiscal resumo.', |
||
| 57 | 'format' => '' |
||
| 58 | ], |
||
| 59 | 'DT_DOC' => [ |
||
| 60 | 'type' => 'numeric', |
||
| 61 | 'regex' => '^(0[1-9]|[1-2][0-9]|31(?!(?:0[2469]|11))|30(?!02))(0[1-9]|1[0-2])([12]\d{3})$', |
||
| 62 | 'required' => true, |
||
| 63 | 'info' => 'Data da emissão do documento fiscal', |
||
| 64 | 'format' => '' |
||
| 65 | ], |
||
| 66 | 'VL_DOC' => [ |
||
| 67 | 'type' => 'numeric', |
||
| 68 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
| 69 | 'required' => true, |
||
| 70 | 'info' => 'Valor total do documento fiscal', |
||
| 71 | 'format' => '15v2' |
||
| 72 | ], |
||
| 73 | 'VL_DESC' => [ |
||
| 74 | 'type' => 'numeric', |
||
| 75 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
| 76 | 'required' => true, |
||
| 77 | 'info' => 'Valor acumulado dos descontos', |
||
| 78 | 'format' => '15v2' |
||
| 79 | ], |
||
| 80 | 'VL_SERV' => [ |
||
| 81 | 'type' => 'numeric', |
||
| 82 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
| 83 | 'required' => true, |
||
| 84 | 'info' => 'Valor acumulado da prestação de serviço', |
||
| 85 | 'format' => '15v2' |
||
| 86 | ], |
||
| 87 | 'VL_BC_ICMS' => [ |
||
| 88 | 'type' => 'numeric', |
||
| 89 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
| 90 | 'required' => true, |
||
| 91 | 'info' => 'Valor total da base de cálculo do ICMS', |
||
| 92 | 'format' => '15v2' |
||
| 93 | ], |
||
| 94 | 'VL_ICMS' => [ |
||
| 95 | 'type' => 'numeric', |
||
| 96 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
| 97 | 'required' => true, |
||
| 98 | 'info' => 'Valor total do ICMS', |
||
| 99 | 'format' => '15v2' |
||
| 100 | ], |
||
| 101 | 'VL_PIS' => [ |
||
| 102 | 'type' => 'numeric', |
||
| 103 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
| 104 | 'required' => true, |
||
| 105 | 'info' => 'Valor do PIS', |
||
| 106 | 'format' => '15v2' |
||
| 107 | ], |
||
| 108 | 'VL_COFINS' => [ |
||
| 109 | 'type' => 'numeric', |
||
| 110 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
| 111 | 'required' => true, |
||
| 112 | 'info' => 'Valor da COFINS', |
||
| 113 | 'format' => '15v2' |
||
| 114 | ], |
||
| 115 | 'COD_CTA' => [ |
||
| 116 | 'type' => 'string', |
||
| 117 | 'regex' => '^[0-9]{0}$', |
||
| 118 | 'required' => true, |
||
| 119 | 'info' => 'Código da conta analítica contábil debitada/creditada', |
||
| 120 | 'format' => '' |
||
| 121 | ] |
||
| 122 | ]; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Constructor |
||
| 126 | * @param \stdClass $std |
||
| 127 | */ |
||
| 128 | public function __construct(\stdClass $std) |
||
| 133 | } |
||
| 134 |
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.