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 |
||
20 | class DynaGrid extends Widget |
||
21 | { |
||
22 | public $title = null; |
||
23 | public $modelClass = null; |
||
24 | /** |
||
25 | * @var BaseDataProvider |
||
26 | */ |
||
27 | public $dataProvider = null; |
||
28 | |||
29 | /** |
||
30 | * @var ActiveRecord |
||
31 | */ |
||
32 | public $filter = null; |
||
33 | public $uniqueId = null; |
||
34 | public $urlAttributes = null; |
||
35 | public $isAllowedAdding = true; |
||
36 | public $isAllowedMassEdit = false; |
||
37 | public $refreshAttributes = []; |
||
38 | public $handleButtons = []; |
||
39 | public $isRenderFlashes = true; |
||
40 | public $urlAttributesExcluded = []; |
||
41 | public $defaultHandleButtons = [ |
||
42 | 'visible' => [ |
||
43 | 'icon' => 'eye-open', |
||
44 | 'label' => 'Mark visible', |
||
45 | 'confirmMessage' => 'You sure want mark # records as visible?', |
||
46 | 'enable' => false, |
||
47 | ], |
||
48 | 'unvisible' => [ |
||
49 | 'icon' => 'eye-close', |
||
50 | 'label' => 'Mark unvisible', |
||
51 | 'confirmMessage' => 'You sure want mark # records as unvisible?', |
||
52 | 'enable' => false, |
||
53 | ], |
||
54 | 'delete' => [ |
||
55 | 'icon' => 'trash', |
||
56 | 'label' => 'Delete', |
||
57 | 'button' => 'danger', |
||
58 | 'confirmMessage' => 'You sure want delete # records?', |
||
59 | 'enable' => false, |
||
60 | ], |
||
61 | ]; |
||
62 | public function getDefaultWidgetOptions() |
||
63 | { |
||
64 | return [ |
||
65 | 'class' => \execut\actions\widgets\DynaGrid::class, |
||
66 | 'filter' => $this->filter, |
||
67 | 'dataProvider' => $this->dataProvider, |
||
68 | 'gridOptions' => [ |
||
69 | 'updateUrl' => $this->getUpdateUrlParams(), |
||
70 | 'addButtonUrl' => $this->getAddUrlParams(), |
||
71 | 'layout' => '{alertBlock}<div class="dyna-grid-footer">{summary}{pager}<div class="dyna-grid-toolbar">{toolbar}</div></div>{items}', |
||
72 | 'toolbar' => $this->getToolbarConfig(), |
||
73 | // 'panel' => [ |
||
|
|||
74 | // 'heading' => '<h3 class="panel-title"><i class="glyphicon glyphicon-cog"></i> ' . \yii::t('execut.actions', 'List') . ' ' . $this->lcfirst($this->title) . '</h3>', |
||
75 | // 'before' => $alertBlock, |
||
76 | // ], |
||
77 | 'options' => [ |
||
78 | 'id' => $this->getGridId(), |
||
79 | ], |
||
80 | ], |
||
81 | 'options' => [ |
||
82 | 'id' => $this->getDynaGridId(), |
||
83 | ], |
||
84 | ]; |
||
85 | } |
||
86 | |||
87 | public function getWidgetOptions() |
||
88 | { |
||
89 | $options = parent::getWidgetOptions(); |
||
90 | if (!empty($options['gridOptions']['layout'])) { |
||
91 | $options['gridOptions']['layout'] = strtr($options['gridOptions']['layout'], [ |
||
92 | '{alertBlock}' => $this->renderAlertBlock(), |
||
93 | ]); |
||
94 | } |
||
95 | |||
96 | return $options; |
||
97 | } |
||
98 | |||
99 | protected function getDynaGridId() { |
||
100 | $m = $this->modelClass; |
||
101 | $tableName = str_replace('\\', '_', $m) . '1'; |
||
102 | $userId = ''; |
||
103 | if (\yii::$app->user) { |
||
104 | $userId .= \yii::$app->user->id; |
||
105 | } |
||
106 | |||
107 | return 'dynagrid-' . $tableName . '-' . $userId; |
||
108 | } |
||
109 | |||
110 | protected function getGridId() { |
||
111 | return 'grid-' . $this->getDynaGridId(); |
||
112 | } |
||
113 | |||
114 | View Code Duplication | protected function renderAlertBlock() |
|
115 | { |
||
116 | if (!$this->isRenderFlashes) { |
||
117 | return ''; |
||
118 | } |
||
119 | |||
120 | $session = \Yii::$app->session; |
||
121 | $flashes = $session->getAllFlashes(); |
||
122 | $alertContainerOptions = [ |
||
123 | 'style' => 'max-width:400px' |
||
124 | ]; |
||
125 | if (count($flashes) === 0) { |
||
126 | Html::addCssStyle($alertContainerOptions, 'display:none;'); |
||
127 | } |
||
128 | $out = Html::beginTag('div', $alertContainerOptions); |
||
129 | foreach ($flashes as $type => $message) { |
||
130 | if (is_array($message)) { |
||
131 | $message = implode('<br>', $message); |
||
132 | } |
||
133 | |||
134 | $alertWidgetOptions = []; |
||
135 | $alertWidgetOptions['body'] = $message; |
||
136 | $alertWidgetOptions['options'] = [ |
||
137 | 'class' => ['alert', 'alert-success'], |
||
138 | 'style' => 'padding-left:10px;padding-right:10px;' |
||
139 | ]; |
||
140 | $out .= "\n" . Alert::widget($alertWidgetOptions); |
||
141 | $session->removeFlash($type); |
||
142 | } |
||
143 | |||
144 | $out .= "\n</div>"; |
||
145 | |||
146 | return $out; |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * @param $refreshUrlParams |
||
151 | * @return array |
||
152 | */ |
||
153 | public function getToolbarConfig(): array |
||
154 | { |
||
155 | $refreshUrlParams = [ |
||
156 | $this->adapter->uniqueId, |
||
157 | ]; |
||
158 | |||
159 | foreach ($this->refreshAttributes as $key) { |
||
160 | if (!empty($this->adapter->actionParams->get[$key])) { |
||
161 | $refreshUrlParams[$key] = $this->adapter->actionParams->get[$key]; |
||
162 | } |
||
163 | } |
||
164 | |||
165 | return [ |
||
166 | 'massEdit' => ['content' => $this->renderMassEditButton()], |
||
167 | 'massVisible' => ['content' => $this->renderVisibleButtons()], |
||
168 | 'add' => ['content' => $this->renderAddButton()], |
||
169 | 'refresh' => [ |
||
170 | 'content' => Html::a('<i class="glyphicon glyphicon-repeat"></i>', $refreshUrlParams, ['data-pjax' => 0, 'class' => 'btn btn-default', 'title' => 'Reset Grid']), |
||
171 | ], |
||
172 | 'dynaParams' => ['content' => '{dynagridFilter}{dynagridSort}{dynagrid}'], |
||
173 | 'toggleData' => '{toggleData}', |
||
174 | 'export' => '{export}', |
||
175 | ]; |
||
176 | } |
||
177 | |||
178 | protected function lcfirst($string, $encoding = "UTF-8") |
||
179 | { |
||
180 | $first = mb_convert_case(mb_substr($string, 0, 1, $encoding), MB_CASE_LOWER, $encoding); |
||
181 | |||
182 | return $first . mb_substr($string, 1, null, $encoding); |
||
183 | } |
||
184 | |||
185 | public function getUniqueId() { |
||
186 | if ($this->uniqueId) { |
||
187 | return $this->uniqueId; |
||
188 | } else { |
||
189 | return $this->adapter->actionParams->getUniqueId(['module', 'controller']); |
||
190 | } |
||
191 | } |
||
192 | |||
193 | protected function getUrlAttributes() { |
||
194 | |||
195 | if ($this->urlAttributes === null) { |
||
196 | $filterAttributes = $this->filter->attributes; |
||
197 | foreach ($this->filter->getRelatedRecords() as $relation => $records) { |
||
198 | if (empty($records)) { |
||
199 | continue; |
||
200 | } |
||
201 | |||
202 | if (!is_array($records)) { |
||
203 | $filterAttributes[$relation] = $records; |
||
204 | continue; |
||
205 | } |
||
206 | |||
207 | $relationAttributes = []; |
||
208 | foreach ($records as $key => $record) { |
||
209 | $recordAttributes = array_filter($record->attributes); |
||
210 | if (!empty($recordAttributes)) { |
||
211 | $relationAttributes[$key] = $recordAttributes; |
||
212 | } |
||
213 | } |
||
214 | |||
215 | if (!empty($relationAttributes)) { |
||
216 | $filterAttributes[$relation] = $relationAttributes; |
||
217 | } |
||
218 | } |
||
219 | |||
220 | $formName = $this->filter->formName(); |
||
221 | $result = [$formName => []]; |
||
222 | foreach ($filterAttributes as $attribute => $value) { |
||
223 | if (in_array($attribute, $this->urlAttributesExcluded)) { |
||
224 | continue; |
||
225 | } |
||
226 | if (!empty($value)) { |
||
227 | $result[$formName][$attribute] = $value; |
||
228 | } |
||
229 | } |
||
230 | |||
231 | return $result; |
||
232 | } |
||
233 | |||
234 | return $this->urlAttributes; |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * @return string |
||
239 | */ |
||
240 | protected function renderAddButton() |
||
241 | { |
||
242 | if ($this->isAllowedAdding) { |
||
243 | $lcfirstTitle = $this->title; |
||
244 | // var_dump($lcfirstTitle); |
||
245 | // exit; |
||
246 | return Html::a(\yii::t('execut.actions', 'Add') . ' ' . $lcfirstTitle, Url::to($this->getUpdateUrlParams()), [ |
||
247 | 'type' => 'button', |
||
248 | 'data-pjax' => 0, |
||
249 | 'title' => \yii::t('execut.actions', 'Add') . ' ' . $lcfirstTitle, |
||
250 | 'class' => 'btn btn-success' |
||
251 | ]) . ' '; |
||
252 | } |
||
253 | } |
||
254 | |||
255 | /** |
||
256 | * @return string |
||
257 | */ |
||
258 | protected function renderMassEditButton() |
||
288 | |||
289 | /** |
||
290 | * @return string |
||
291 | */ |
||
292 | protected function renderVisibleButtons(): string |
||
326 | |||
327 | /** |
||
328 | * @return array |
||
329 | */ |
||
330 | protected function getUpdateUrlParams(): array |
||
331 | { |
||
332 | return [ |
||
333 | '/' . trim($this->getUniqueId(), '/') . '/update', |
||
334 | ]; |
||
335 | } |
||
336 | |||
337 | /** |
||
338 | * @return array |
||
339 | */ |
||
340 | protected function getAddUrlParams(): array |
||
344 | } |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.