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 | class B510 extends Element implements ElementInterface |
||
10 | { |
||
11 | const REG = 'B510'; |
||
12 | const LEVEL = 3; |
||
13 | const PARENT = 'B500'; |
||
14 | |||
15 | protected $parameters = [ |
||
16 | 'IND_PROF' => [ |
||
17 | 'type' => 'string', |
||
18 | 'regex' => '^[0|1]$', |
||
19 | 'required' => true, |
||
20 | 'info' => 'Indicador de habilitação: ' |
||
21 | .'0- Profissional habilitado ' |
||
22 | .'1- Profissional não habilitado', |
||
23 | 'format' => '' |
||
24 | ], |
||
25 | 'IND_ESC' => [ |
||
26 | 'type' => 'string', |
||
27 | 'regex' => '^[0|1]$', |
||
28 | 'required' => true, |
||
29 | 'info' => 'Indicador de escolaridade: ' |
||
30 | .'0- Nível superior ' |
||
31 | .'1- Nível médio', |
||
32 | 'format' => '' |
||
33 | ], |
||
34 | 'IND_SOC' => [ |
||
35 | 'type' => 'string', |
||
36 | 'regex' => '^[0|1]$', |
||
37 | 'required' => true, |
||
38 | 'info' => 'Indicador de participação societária: ' |
||
39 | .'0- Sócio ' |
||
40 | .'1- Não sócio', |
||
41 | 'format' => '' |
||
42 | ], |
||
43 | 'CPF' => [ |
||
44 | 'type' => 'string', |
||
45 | 'regex' => '^\d{11}$', |
||
46 | 'required' => true, |
||
47 | 'info' => 'Número de inscrição do profissional no CPF.', |
||
48 | 'format' => '' |
||
49 | ], |
||
50 | 'NOME' => [ |
||
51 | 'type' => 'string', |
||
52 | 'regex' => '^.{1,100}$', |
||
53 | 'required' => true, |
||
54 | 'info' => 'Nome do profissional', |
||
55 | 'format' => '' |
||
56 | ] |
||
57 | ]; |
||
58 | |||
59 | /** |
||
60 | * Constructor |
||
61 | * @param \stdClass $std |
||
62 | */ |
||
63 | public function __construct(\stdClass $std) |
||
69 | |||
70 | public function postValidation() |
||
89 | |||
90 | private function validaCPF($cpf) |
||
161 | } |
||
162 |
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.