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 |
||
7 | class EloquentModel extends Model |
||
8 | { |
||
9 | /** |
||
10 | * The primary key for the model. |
||
11 | * |
||
12 | * @var string |
||
13 | */ |
||
14 | protected $primaryKey = 'ID'; |
||
15 | |||
16 | /** |
||
17 | * The name of the "created at" column. |
||
18 | * |
||
19 | * @var string |
||
20 | */ |
||
21 | const CREATED_AT = 'UF_CREATED_AT'; |
||
22 | |||
23 | /** |
||
24 | * The name of the "updated at" column. |
||
25 | * |
||
26 | * @var string |
||
27 | */ |
||
28 | const UPDATED_AT = 'UF_UPDATED_AT'; |
||
29 | |||
30 | /** |
||
31 | * @var array |
||
32 | */ |
||
33 | public $multipleHighloadBlockFields = []; |
||
34 | |||
35 | /** |
||
36 | * Get an attribute from the model. |
||
37 | * |
||
38 | * @param string $key |
||
39 | * @return mixed |
||
40 | */ |
||
41 | View Code Duplication | public function getAttribute($key) |
|
49 | |||
50 | /** |
||
51 | * Set a given attribute on the model. |
||
52 | * |
||
53 | * @param string $key |
||
54 | * @param mixed $value |
||
55 | * @return $this |
||
56 | */ |
||
57 | View Code Duplication | public function setAttribute($key, $value) |
|
69 | } |
||
70 |
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.