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 |
||
| 15 | View Code Duplication | class C165 extends Element implements ElementInterface |
|
|
|
|||
| 16 | { |
||
| 17 | const REG = 'C165'; |
||
| 18 | const LEVEL = 3; |
||
| 19 | const PARENT = 'C100'; |
||
| 20 | |||
| 21 | protected $parameters = [ |
||
| 22 | 'COD_PART' => [ |
||
| 23 | 'type' => 'string', |
||
| 24 | 'regex' => '^[A-z0-9]{1,60}$', |
||
| 25 | 'required' => false, |
||
| 26 | 'info' => 'Código do participante', |
||
| 27 | 'format' => '' |
||
| 28 | ], |
||
| 29 | 'VEIC_ID' => [ |
||
| 30 | 'type' => 'string', |
||
| 31 | 'regex' => '^[A-Z]{3}[\d]{1}[\dA-Z]{1}[\d]{2}$', |
||
| 32 | 'required' => false, |
||
| 33 | 'info' => 'Placa de identificação do veículo automotor', |
||
| 34 | 'format' => '' |
||
| 35 | ], |
||
| 36 | 'COD_AUT' => [ |
||
| 37 | 'type' => 'string', |
||
| 38 | 'regex' => '^(.*)$', |
||
| 39 | 'required' => false, |
||
| 40 | 'info' => 'Código da autorização fornecido pela SEFAZ (combustíveis)', |
||
| 41 | 'format' => '' |
||
| 42 | ], |
||
| 43 | 'NR_PASSE' => [ |
||
| 44 | 'type' => 'string', |
||
| 45 | 'regex' => '^(.*)$', |
||
| 46 | 'required' => false, |
||
| 47 | 'info' => 'Código da autorização fornecido pela SEFAZ (combustíveis)', |
||
| 48 | 'format' => '' |
||
| 49 | ], |
||
| 50 | 'HORA' => [ |
||
| 51 | 'type' => 'numeric', |
||
| 52 | 'regex' => '^(?:[01]\d|2[0123])(?:[012345]\d)(?:[012345]\d)$', |
||
| 53 | 'required' => false, |
||
| 54 | 'info' => 'Código da autorização fornecido pela SEFAZ (combustíveis)', |
||
| 55 | 'format' => '' |
||
| 56 | ], |
||
| 57 | 'TEMPER' => [ |
||
| 58 | 'type' => 'numeric', |
||
| 59 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
| 60 | 'required' => false, |
||
| 61 | 'info' => 'Temperatura em graus Celsius utilizada para '. |
||
| 62 | 'quantificação do volume de combustível', |
||
| 63 | 'format' => '3v2' |
||
| 64 | ], |
||
| 65 | 'QTD_VOL' => [ |
||
| 66 | 'type' => 'numeric', |
||
| 67 | 'regex' => '^\d+$', |
||
| 68 | 'required' => false, |
||
| 69 | 'info' => 'Quantidade de volumes transportados', |
||
| 70 | 'format' => '' |
||
| 71 | ], |
||
| 72 | 'PESO_BRT' => [ |
||
| 73 | 'type' => 'numeric', |
||
| 74 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
| 75 | 'required' => false, |
||
| 76 | 'info' => 'Peso bruto dos volumes transportados (em Kg)', |
||
| 77 | 'format' => '10v2' |
||
| 78 | ], |
||
| 79 | 'PESO_LIQ' => [ |
||
| 80 | 'type' => 'numeric', |
||
| 81 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
| 82 | 'required' => false, |
||
| 83 | 'info' => 'Peso líquido dos volumes transportados (em Kg)', |
||
| 84 | 'format' => '10v2' |
||
| 85 | ], |
||
| 86 | 'NOM_MOT' => [ |
||
| 87 | 'type' => 'string', |
||
| 88 | 'regex' => '^.{1,60}$', |
||
| 89 | 'required' => false, |
||
| 90 | 'info' => 'Nome do motorista', |
||
| 91 | 'format' => '' |
||
| 92 | ], |
||
| 93 | 'CPF' => [ |
||
| 94 | 'type' => 'string', |
||
| 95 | 'regex' => '^[0-9]{11}$', |
||
| 96 | 'required' => false, |
||
| 97 | 'info' => 'CPF do motorista', |
||
| 98 | 'format' => '' |
||
| 99 | ], |
||
| 100 | 'UF_ID' => [ |
||
| 101 | 'type' => 'string', |
||
| 102 | 'regex' => '^[A-z]{2}$', |
||
| 103 | 'required' => false, |
||
| 104 | 'info' => 'Sigla da UF da placa do veículo', |
||
| 105 | 'format' => '' |
||
| 106 | ], |
||
| 107 | ]; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Constructor |
||
| 111 | * @param \stdClass $std |
||
| 112 | */ |
||
| 113 | public function __construct(\stdClass $std) |
||
| 118 | } |
||
| 119 |
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.