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 |
||
| 18 | View Code Duplication | class Z0005 extends Element implements ElementInterface |
|
|
|
|||
| 19 | { |
||
| 20 | const REG = '0005'; |
||
| 21 | const LEVEL = 2; |
||
| 22 | const PARENT = '0001'; |
||
| 23 | |||
| 24 | protected $parameters = [ |
||
| 25 | 'FANTASIA' => [ |
||
| 26 | 'type' => 'string', |
||
| 27 | 'regex' => '^.{3,60}$', |
||
| 28 | 'required' => true, |
||
| 29 | 'info' => 'Nome de fantasia associado ao nome empresarial.', |
||
| 30 | 'format' => '' |
||
| 31 | ], |
||
| 32 | 'CEP' => [ |
||
| 33 | 'type' => 'string', |
||
| 34 | 'regex' => '^[0-9]{8}$', |
||
| 35 | 'required' => true, |
||
| 36 | 'info' => 'Código de Endereçamento Postal.', |
||
| 37 | 'format' => '' |
||
| 38 | ], |
||
| 39 | 'END' => [ |
||
| 40 | 'type' => 'string', |
||
| 41 | 'regex' => '^.{3,60}$', |
||
| 42 | 'required' => true, |
||
| 43 | 'info' => 'Logradouro e endereço do imóvel.', |
||
| 44 | 'format' => '' |
||
| 45 | ], |
||
| 46 | 'NUM' => [ |
||
| 47 | 'type' => 'string', |
||
| 48 | 'regex' => '^.{1,10}$', |
||
| 49 | 'required' => false, |
||
| 50 | 'info' => 'Número do imóvel.', |
||
| 51 | 'format' => '' |
||
| 52 | ], |
||
| 53 | 'COMPL' => [ |
||
| 54 | 'type' => 'string', |
||
| 55 | 'regex' => '^.{3,60}$', |
||
| 56 | 'required' => false, |
||
| 57 | 'info' => 'Dados complementares do endereço.', |
||
| 58 | 'format' => '' |
||
| 59 | ], |
||
| 60 | 'BAIRRO' => [ |
||
| 61 | 'type' => 'string', |
||
| 62 | 'regex' => '^.{3,60}$', |
||
| 63 | 'required' => true, |
||
| 64 | 'info' => 'Bairro em que o imóvel está situado.', |
||
| 65 | 'format' => '' |
||
| 66 | ], |
||
| 67 | 'FONE' => [ |
||
| 68 | 'type' => 'string', |
||
| 69 | 'regex' => '^[0-9]{8,11}$', |
||
| 70 | 'required' => false, |
||
| 71 | 'info' => 'Número do telefone (DDD+FONE).', |
||
| 72 | 'format' => '' |
||
| 73 | ], |
||
| 74 | 'FAX' => [ |
||
| 75 | 'type' => 'string', |
||
| 76 | 'regex' => '^[0-9]{8,11}$', |
||
| 77 | 'required' => false, |
||
| 78 | 'info' => 'Número do fax.', |
||
| 79 | 'format' => '' |
||
| 80 | ], |
||
| 81 | 'EMAIL' => [ |
||
| 82 | 'type' => 'string', |
||
| 83 | 'regex' => 'email', |
||
| 84 | 'required' => false, |
||
| 85 | 'info' => 'Endereço do correio eletrônico.', |
||
| 86 | 'format' => '' |
||
| 87 | ] |
||
| 88 | ]; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Constructor |
||
| 92 | * @param \stdClass $std |
||
| 93 | */ |
||
| 94 | public function __construct(\stdClass $std) |
||
| 99 | } |
||
| 100 |
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.