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 E311 extends Element implements ElementInterface |
|
|
|
|||
| 10 | { |
||
| 11 | const REG = 'E311'; |
||
| 12 | const LEVEL = 4; |
||
| 13 | const PARENT = 'E310'; |
||
| 14 | |||
| 15 | protected $parameters = [ |
||
| 16 | 'COD_AJ_APUR' => [ |
||
| 17 | 'type' => 'string', |
||
| 18 | 'regex' => '^.{1,8}$', |
||
| 19 | 'required' => true, |
||
| 20 | 'info' => 'Código do ajuste da apuração e dedução, conforme a Tabela indicada no item 5.1.1', |
||
| 21 | 'format' => '' |
||
| 22 | ], |
||
| 23 | 'DESCR_COMPL_AJ' => [ |
||
| 24 | 'type' => 'string', |
||
| 25 | 'regex' => '^.*$', |
||
| 26 | 'required' => false, |
||
| 27 | 'info' => 'Descrição complementar do ajuste da apuração', |
||
| 28 | 'format' => '' |
||
| 29 | ], |
||
| 30 | 'VL_AJ_APUR' => [ |
||
| 31 | 'type' => 'numeric', |
||
| 32 | 'regex' => '^\d+(\.\d*)?|\.\d+$', |
||
| 33 | 'required' => true, |
||
| 34 | 'info' => 'Valor do ajuste da apuração', |
||
| 35 | 'format' => '15v2' |
||
| 36 | ] |
||
| 37 | ]; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * Constructor |
||
| 41 | * @param \stdClass $std |
||
| 42 | */ |
||
| 43 | public function __construct(\stdClass $std) |
||
| 49 | |||
| 50 | public function postValidation() |
||
| 60 | } |
||
| 61 |
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.