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 |
||
17 | class Updates extends \App\BaseModel |
||
18 | { |
||
19 | /** @var string Widget type. */ |
||
20 | protected $type = 'Updates'; |
||
21 | |||
22 | /** @var string Module name. */ |
||
23 | protected $moduleName; |
||
24 | |||
25 | /** @var int Source record ID. */ |
||
26 | protected $recordId; |
||
27 | |||
28 | /** @var int Page number. */ |
||
29 | protected $page = 1; |
||
30 | |||
31 | /** @var bool More pages. */ |
||
32 | protected $isMorePages; |
||
33 | |||
34 | /** @var array Scripts. */ |
||
35 | public $scripts = []; |
||
36 | |||
37 | /** @var int Limit. */ |
||
38 | public $limit = 5; |
||
39 | |||
40 | /** @var \YF\Modules\Base\Model\RecordHistory Model of history record. */ |
||
41 | protected $historyModel; |
||
42 | |||
43 | /** |
||
44 | * Constructor. |
||
45 | * |
||
46 | * @param string $moduleName |
||
47 | */ |
||
48 | public function __construct(string $moduleName) |
||
52 | |||
53 | /** |
||
54 | * Gets widget ID. |
||
55 | * |
||
56 | * @return int |
||
57 | */ |
||
58 | public function getId() |
||
62 | |||
63 | /** |
||
64 | * Gets widget type. |
||
65 | * |
||
66 | * @return string |
||
67 | */ |
||
68 | public function getType(): string |
||
72 | |||
73 | /** |
||
74 | * Gets module name. |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | public function getModuleName(): string |
||
82 | |||
83 | /** |
||
84 | * Gets widget name. |
||
85 | * |
||
86 | * @return string |
||
87 | */ |
||
88 | public function getTitle(): string |
||
92 | |||
93 | /** |
||
94 | * Sets record ID. |
||
95 | * |
||
96 | * @param int $recordId |
||
97 | * |
||
98 | * @return self |
||
99 | */ |
||
100 | public function setRecordId(int $recordId): self |
||
106 | |||
107 | /** |
||
108 | * Get URL address. |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | public function getUrl(): string |
||
116 | |||
117 | /** |
||
118 | * Set page number. |
||
119 | * |
||
120 | * @param int $page |
||
121 | * |
||
122 | * @return $this |
||
123 | */ |
||
124 | public function setPage(int $page): self |
||
129 | |||
130 | /** |
||
131 | * Get page number. |
||
132 | * |
||
133 | * @return int |
||
134 | */ |
||
135 | public function getPage(): int |
||
139 | |||
140 | /** |
||
141 | * Gets history model. |
||
142 | * |
||
143 | * @return \YF\Modules\Base\Model\RecordHistory |
||
144 | */ |
||
145 | public function getHistoryModel(): \YF\Modules\Base\Model\RecordHistory |
||
152 | |||
153 | /** |
||
154 | * Check if is more pages. |
||
155 | * |
||
156 | * @return bool |
||
157 | */ |
||
158 | public function isMorePages(): bool |
||
162 | |||
163 | /** |
||
164 | * Gets template path for widget. |
||
165 | * |
||
166 | * @return string |
||
167 | */ |
||
168 | public function getTemplatePath(): string |
||
172 | |||
173 | /** |
||
174 | * Gets template path for widget content. |
||
175 | * |
||
176 | * @return string |
||
177 | */ |
||
178 | public function getTemplateContentPath(): string |
||
182 | |||
183 | /** |
||
184 | * Set scripts. |
||
185 | * |
||
186 | * @param array $scripts |
||
187 | */ |
||
188 | public function setScriptsObject($scripts) |
||
192 | |||
193 | /** |
||
194 | * Gets scripts. |
||
195 | * |
||
196 | * @return array |
||
197 | */ |
||
198 | View Code Duplication | public function getScripts(): array |
|
205 | } |
||
206 |
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.