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 |
||
21 | View Code Duplication | class PageController extends AdminModuleController { |
|
|
|||
22 | |||
23 | /** |
||
24 | * Constructor |
||
25 | * |
||
26 | * @var Request $request |
||
27 | */ |
||
28 | public function __construct(Request $request) { |
||
43 | |||
44 | /** |
||
45 | * Validation rules |
||
46 | * |
||
47 | * @var array |
||
48 | */ |
||
49 | protected $arValidationArray = [ |
||
50 | 'name' => 'required|max:255|unique:page,name', |
||
51 | 'meta_title' => 'max:255', |
||
52 | 'meta_description' => 'max:255', |
||
53 | 'meta_keywords' => 'max:255', |
||
54 | 'text' => 'max:1000000', |
||
55 | 'url' => 'required|max:255|unique:page,url', |
||
56 | 'author_name' => 'max:255', |
||
57 | 'published_at' => 'date', |
||
58 | ]; |
||
59 | |||
60 | /** |
||
61 | * Associate relationships to other table |
||
62 | * |
||
63 | * @param object $object |
||
64 | * @param Request $request |
||
65 | */ |
||
66 | public function associateRelationships($object, Request $request) { |
||
115 | |||
116 | /** |
||
117 | * Associate relationships to other table, where ID if object must be present |
||
118 | * |
||
119 | * @param object $object |
||
120 | * @param Request $request |
||
121 | */ |
||
122 | public function associateRelationshipsWithID($object, Request $request) { |
||
163 | } |
||
164 |
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.