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 |
||
14 | class Material extends \Model { |
||
15 | |||
16 | public static $objectName = 'Материал'; |
||
17 | public static $categoryModel = 'Materials\Category'; |
||
18 | public static $labels = [ |
||
19 | 'name' => 'Заголовок', |
||
20 | 'category_id' => 'Раздел', |
||
21 | 'preview' => 'Краткое превью', |
||
22 | 'text' => 'Текст страницы', |
||
23 | 'alias' => 'Алиас страницы', |
||
24 | 'template' => 'Шаблон сайта', |
||
25 | 'viewer' => 'Тип страницы', |
||
26 | 'image_file_id' => 'Фото материала', |
||
27 | 'description' => 'Описание для поисковиков', |
||
28 | 'keywords' => 'Ключевые слова', |
||
29 | 'user_id' => 'Создатель', |
||
30 | 'date_create' => 'Дата создания', |
||
31 | 'date_publish' => 'Дата публикации', |
||
32 | 'tag_list' => 'Теги', |
||
33 | 'default' => 'По умолчанию' |
||
34 | ]; |
||
35 | public static $dataManagers = [ |
||
36 | 'manager' => [ |
||
37 | 'cols' => [ |
||
38 | 'name', |
||
39 | 'alias', |
||
40 | 'default', |
||
41 | 'category_id', |
||
42 | 'tag_list', |
||
43 | 'date_publish', |
||
44 | 'date_create' |
||
45 | ], |
||
46 | 'sortable' => [ |
||
47 | 'name', |
||
48 | 'alias', |
||
49 | 'category_id', |
||
50 | 'date_create' |
||
51 | ], |
||
52 | 'filters' => [ |
||
53 | 'name', |
||
54 | 'preview', |
||
55 | 'text', |
||
56 | 'default', |
||
57 | 'alias', |
||
58 | 'template', |
||
59 | 'viewer', |
||
60 | 'description', |
||
61 | 'keywords', |
||
62 | 'user_id', |
||
63 | 'date_create' |
||
64 | ], |
||
65 | 'categorys' => [ |
||
66 | 'model' => 'Materials\Category', |
||
67 | ], |
||
68 | 'sortMode' => true |
||
69 | ] |
||
70 | ]; |
||
71 | public static $cols = [ |
||
72 | 'name' => ['type' => 'text'], |
||
73 | 'alias' => ['type' => 'text'], |
||
74 | 'preview' => ['type' => 'html'], |
||
75 | 'text' => ['type' => 'html'], |
||
76 | 'keywords' => ['type' => 'text'], |
||
77 | 'description' => ['type' => 'text'], |
||
78 | 'category_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'category'], |
||
79 | 'user_id' => ['type' => 'select', 'source' => 'relation', 'relation' => 'user'], |
||
80 | 'template' => ['type' => 'select', 'source' => 'method', 'method' => 'templatesList', 'module' => 'Materials'], |
||
81 | 'viewer' => ['type' => 'select', 'source' => 'method', 'method' => 'viewsList', 'module' => 'Materials'], |
||
82 | 'default' => ['type' => 'bool'], |
||
83 | 'hidden' => ['type' => 'bool'], |
||
84 | 'image_file_id' => ['type' => 'image'], |
||
85 | 'link' => ['type' => 'dataManager', 'relation' => 'links'], |
||
86 | 'tree_path' => ['type' => 'text'], |
||
87 | 'weight' => ['type' => 'number'], |
||
88 | 'tag_list' => ['type' => 'text'], |
||
89 | 'date_publish' => ['type' => 'dateTime', 'null' => true, 'emptyValue' => null], |
||
90 | 'date_create' => ['type' => 'dateTime'], |
||
91 | ]; |
||
92 | public static $forms = [ |
||
93 | 'manager' => [ |
||
94 | 'options' => [ |
||
95 | 'access' => [ |
||
96 | 'groups' => [ |
||
97 | 3 |
||
98 | ] |
||
99 | ] |
||
100 | ], |
||
101 | 'map' => [ |
||
102 | ['name', 'category_id'], |
||
103 | ['alias', 'image_file_id', 'date_publish'], |
||
104 | ['template', 'viewer', 'default'], |
||
105 | ['keywords', 'description'], |
||
106 | ['tag_list'], |
||
107 | ['preview'], |
||
108 | ['text'], |
||
109 | ['link'], |
||
110 | ] |
||
111 | ] |
||
112 | ]; |
||
113 | |||
114 | View Code Duplication | public static function relations() { |
|
135 | |||
136 | public function getHref() { |
||
152 | |||
153 | View Code Duplication | public function resolveTemplate() { |
|
162 | |||
163 | public function resolveViewer() { |
||
172 | |||
173 | public function beforeSave() { |
||
178 | } |
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.