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 |
||
22 | class ActionBox extends Box |
||
23 | { |
||
24 | public $model; |
||
25 | |||
26 | public $dataProvider; |
||
27 | |||
28 | public $bulk = true; |
||
29 | |||
30 | public $options = [ |
||
31 | 'class' => 'box-info', |
||
32 | ]; |
||
33 | |||
34 | public function run() |
||
35 | { |
||
36 | parent::run(); |
||
37 | $this->registerClientScript(); |
||
38 | } |
||
39 | |||
40 | private function registerClientScript() |
||
41 | { |
||
42 | $searchFormId = Json::htmlEncode("#{$this->getBulkFormId()}"); |
||
43 | $view = $this->getView(); |
||
44 | $view->registerJs(<<<JS |
||
45 | // Checkbox |
||
46 | var checkboxes = $('table input[type="checkbox"]'); |
||
47 | var bulkcontainer = $('.box-bulk-actions fieldset'); |
||
48 | checkboxes.on('ifChecked ifUnchecked', function(event) { |
||
49 | if (event.type == 'ifChecked' && $('input.icheck').filter(':checked').length > 0) { |
||
50 | bulkcontainer.prop('disabled', false); |
||
51 | } else if ($('input.icheck').filter(':checked').length == 0) { |
||
52 | bulkcontainer.prop('disabled', true); |
||
53 | } |
||
54 | }); |
||
55 | // On/Off Actions TODO: reduce scope |
||
56 | $(document).on('click', '.box-bulk-actions a', function (event) { |
||
57 | var link = $(this); |
||
58 | var action = link.data('action'); |
||
59 | var form = $($searchFormId); |
||
60 | if (action) { |
||
61 | form.attr({'action': action, method: 'POST'}).submit(); |
||
62 | } |
||
63 | }); |
||
64 | JS |
||
65 | , $view::POS_READY); |
||
66 | } |
||
67 | |||
68 | public function beginActions() |
||
69 | { |
||
70 | echo Html::beginTag('div', ['class' => 'row']) . PHP_EOL; |
||
71 | echo Html::beginTag('div', ['class' => 'box-actions col-md-6']) . PHP_EOL; |
||
72 | } |
||
73 | |||
74 | public function endActions() |
||
75 | { |
||
76 | echo PHP_EOL . Html::endTag('div'); |
||
77 | if ($this->bulk === false) { |
||
78 | echo PHP_EOL . Html::endTag('div'); |
||
79 | } |
||
80 | } |
||
81 | |||
82 | public function beginBulkActions() |
||
83 | { |
||
84 | if ($this->bulk === false) { |
||
85 | throw new InvalidConfigException("'bulk' property is false, turn this on ('true' statement), if you want use bulk actions."); |
||
86 | } |
||
87 | |||
88 | echo Html::beginTag('div', ['class' => 'box-bulk-actions col-md-6 text-right']) . "\n"; |
||
89 | echo Html::beginTag('fieldset', ['disabled' => 'disabled']) . "\n"; |
||
90 | } |
||
91 | |||
92 | public function endBulkActions() |
||
93 | { |
||
94 | echo "\n" . Html::endTag('fieldset'); |
||
95 | echo "\n" . Html::endTag('div'); |
||
96 | echo "\n" . Html::endTag('div'); |
||
97 | |||
98 | // print Html::tag('div', '', ['class' => 'clearfix']); |
||
|
|||
99 | } |
||
100 | |||
101 | public function renderCreateButton($text) |
||
105 | |||
106 | public function renderSearchButton() |
||
107 | { |
||
108 | return AdvancedSearch::renderButton() . "\n"; |
||
109 | } |
||
110 | |||
111 | public function beginSearchForm($options = []) |
||
112 | { |
||
113 | return AdvancedSearch::begin(array_merge(['model' => $this->model], $options)); |
||
114 | } |
||
115 | |||
116 | public function renderSearchForm(array $data = [], $advancedSearchOptions = []) |
||
117 | { |
||
118 | ob_start(); |
||
119 | ob_implicit_flush(false); |
||
120 | try { |
||
121 | $search = $this->beginSearchForm($advancedSearchOptions); |
||
122 | foreach (['per_page', 'representation'] as $key) { |
||
123 | echo Html::hiddenInput($key, Yii::$app->request->get($key)); |
||
124 | } |
||
125 | echo Yii::$app->view->render('_search', array_merge(compact('search'), $data)); |
||
126 | $search->end(); |
||
127 | } catch (\Exception $e) { |
||
128 | ob_end_clean(); |
||
129 | throw $e; |
||
130 | } |
||
131 | |||
132 | return ob_get_clean(); |
||
133 | } |
||
134 | |||
135 | View Code Duplication | public function renderPerPage() |
|
151 | |||
152 | View Code Duplication | public function renderSorter(array $options) |
|
153 | { |
||
159 | |||
160 | public function renderBulkActions(array $options) |
||
168 | |||
169 | View Code Duplication | public function renderBulkButton($text, $action, $color = 'default') |
|
178 | |||
179 | public function renderDeleteButton($text = null) |
||
184 | |||
185 | public function getBulkFormId() |
||
189 | |||
190 | public function beginBulkForm($action = '') |
||
194 | |||
195 | public function endBulkForm() |
||
199 | } |
||
200 |
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.