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 |
||
| 35 | class Z0150 extends Element implements ElementInterface |
||
| 36 | { |
||
| 37 | const REG = '0150'; |
||
| 38 | const LEVEL = 2; |
||
| 39 | const PARENT = '0100'; |
||
| 40 | |||
| 41 | protected $parameters = [ |
||
| 42 | 'COD_PART' => [ |
||
| 43 | 'type' => 'string', |
||
| 44 | 'regex' => '^.{1,60}$', |
||
| 45 | 'required' => true, |
||
| 46 | 'info' => 'Código de identificação do participante no arquivo.', |
||
| 47 | 'format' => '' |
||
| 48 | ], |
||
| 49 | 'NOME' => [ |
||
| 50 | 'type' => 'string', |
||
| 51 | 'regex' => '^.{1,100}$', |
||
| 52 | 'required' => true, |
||
| 53 | 'info' => 'Nome pessoal ou empresarial do participante.', |
||
| 54 | 'format' => '' |
||
| 55 | ], |
||
| 56 | 'COD_PAIS' => [ |
||
| 57 | 'type' => 'integer', |
||
| 58 | 'regex' => '^[0-9]{4,5}$', |
||
| 59 | 'required' => true, |
||
| 60 | 'info' => 'Código do país do participante, conforme a tabela ' |
||
| 61 | . 'indicada no item 3.2.1', |
||
| 62 | 'format' => '' |
||
| 63 | ], |
||
| 64 | 'CNPJ' => [ |
||
| 65 | 'type' => 'string', |
||
| 66 | 'regex' => '^[0-9]{14}$', |
||
| 67 | 'required' => false, |
||
| 68 | 'info' => 'CNPJ do participante.', |
||
| 69 | 'format' => '' |
||
| 70 | ], |
||
| 71 | 'CPF' => [ |
||
| 72 | 'type' => 'string', |
||
| 73 | 'regex' => '^[0-9]{11}$', |
||
| 74 | 'required' => false, |
||
| 75 | 'info' => 'CPF do participante.', |
||
| 76 | 'format' => '' |
||
| 77 | ], |
||
| 78 | 'IE' => [ |
||
| 79 | 'type' => 'string', |
||
| 80 | 'regex' => '^[0-9]{2,14}$', |
||
| 81 | 'required' => false, |
||
| 82 | 'info' => 'Inscrição Estadual do participante.', |
||
| 83 | 'format' => '' |
||
| 84 | ], |
||
| 85 | 'COD_MUN' => [ |
||
| 86 | 'type' => 'integer', |
||
| 87 | 'regex' => '^[0-9]{7}$', |
||
| 88 | 'required' => false, |
||
| 89 | 'info' => 'Código do município, conforme a tabela IBGE', |
||
| 90 | 'format' => '' |
||
| 91 | ], |
||
| 92 | 'SUFRAMA' => [ |
||
| 93 | 'type' => 'string', |
||
| 94 | 'regex' => '^[0-9]{8,9}$', |
||
| 95 | 'required' => false, |
||
| 96 | 'info' => 'Número de inscrição do participante na SUFRAMA.', |
||
| 97 | 'format' => '' |
||
| 98 | ], |
||
| 99 | 'END' => [ |
||
| 100 | 'type' => 'string', |
||
| 101 | 'regex' => '^.{1,60}$', |
||
| 102 | 'required' => true, |
||
| 103 | 'info' => 'Logradouro e endereço do imóvel', |
||
| 104 | 'format' => '' |
||
| 105 | ], |
||
| 106 | 'NUM' => [ |
||
| 107 | 'type' => 'string', |
||
| 108 | 'regex' => '^.{1,10}$', |
||
| 109 | 'required' => false, |
||
| 110 | 'info' => 'Número do imóvel', |
||
| 111 | 'format' => '' |
||
| 112 | ], |
||
| 113 | 'COMPL' => [ |
||
| 114 | 'type' => 'string', |
||
| 115 | 'regex' => '^.{1,60}$', |
||
| 116 | 'required' => false, |
||
| 117 | 'info' => 'Dados complementares do endereço', |
||
| 118 | 'format' => '' |
||
| 119 | ], |
||
| 120 | 'BAIRRO' => [ |
||
| 121 | 'type' => 'string', |
||
| 122 | 'regex' => '^.{1,60}$', |
||
| 123 | 'required' => false, |
||
| 124 | 'info' => 'Bairro em que o imóvel está situado', |
||
| 125 | 'format' => '' |
||
| 126 | ], |
||
| 127 | ]; |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Constructor |
||
| 131 | * @param \stdClass $std |
||
| 132 | */ |
||
| 133 | public function __construct(\stdClass $std) |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Aqui são colocadas validações adicionais que requerem mais logica |
||
| 142 | * e processamento |
||
| 143 | * Deve ser usado apenas quando necessário |
||
| 144 | * @throws \InvalidArgumentException |
||
| 145 | */ |
||
| 146 | public function postValidation() |
||
| 154 | } |
||
| 155 |
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.