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 | class ImageController extends AdminModuleController |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * Constructor |
||
26 | */ |
||
27 | public function __construct() { |
||
32 | |||
33 | /** |
||
34 | * Validation rules |
||
35 | */ |
||
36 | protected $arValidationArray = [ |
||
37 | 'name' => 'required|max:255', |
||
38 | 'description' => 'max:255', |
||
39 | 'alt' => 'max:255', |
||
40 | 'url' => 'max:255|unique:image,url' |
||
41 | ]; |
||
42 | |||
43 | /** |
||
44 | * Binary fields |
||
45 | * |
||
46 | * @var array |
||
47 | */ |
||
48 | protected $binaryFields = []; |
||
49 | |||
50 | /** |
||
51 | * Get number of pagination rows |
||
52 | * |
||
53 | * @return integer |
||
54 | */ |
||
55 | public function getRowsToPaginate(){ |
||
59 | |||
60 | /** |
||
61 | * Associate relationships to other table |
||
62 | * |
||
63 | * @param object $object |
||
64 | * @param Request $request |
||
65 | */ |
||
66 | View Code Duplication | public function associateRelationships($object, Request $request){ |
|
91 | |||
92 | /** |
||
93 | * Save media to storage |
||
94 | * |
||
95 | * @param object $object |
||
96 | * @param Request $request |
||
97 | * @param boolean $update |
||
98 | * @return boolean |
||
99 | */ |
||
100 | public function saveMediaToStorage($object, $request, $update = FALSE) { |
||
126 | |||
127 | } |
||
128 |
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.