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 |
||
31 | class AjaxModal extends \yii\bootstrap\Modal |
||
32 | { |
||
33 | /** |
||
34 | * @var bool |
||
35 | */ |
||
36 | public $bulkPage = false; |
||
37 | |||
38 | /** |
||
39 | * @var bool |
||
40 | */ |
||
41 | public $usePost = false; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | public $scenario; |
||
47 | |||
48 | /** |
||
49 | * @var string URL |
||
50 | */ |
||
51 | protected $_actionUrl; |
||
52 | |||
53 | /** |
||
54 | * @var string ID for modal form |
||
55 | */ |
||
56 | protected $_modalFormId; |
||
57 | |||
58 | /** |
||
59 | * @var string The error text for PNotify generation. |
||
60 | * On set will be processed through [[Yii::t()]] |
||
61 | */ |
||
62 | protected $_errorText; |
||
63 | |||
64 | /** |
||
65 | * @var string The success text for PNotify generation. |
||
66 | * On set will be processed through [[Yii::t()]] |
||
67 | */ |
||
68 | protected $_successText; |
||
69 | |||
70 | /** |
||
71 | * @var string The text that will be displayed on submit button after press. |
||
72 | * On set will be processed through [[Yii::t()]] |
||
73 | */ |
||
74 | protected $_loadingText; |
||
75 | |||
76 | /** |
||
77 | * @var boolean whether to handle the submit of the form in the modal with special AJAX script |
||
78 | * @see registerClientScript |
||
79 | */ |
||
80 | public $handleSubmit = true; |
||
81 | |||
82 | public function getActionUrl() |
||
86 | |||
87 | public function setActionUrl($url) |
||
91 | |||
92 | public function getModalFormId() |
||
96 | |||
97 | public function setModalFormId($id) |
||
101 | |||
102 | public function getErrorText() |
||
103 | { |
||
104 | return $this->_errorText ?: Yii::t('app', 'An error occurred. Try again later.'); |
||
105 | } |
||
106 | |||
107 | public function setErrorText($text) |
||
111 | |||
112 | public function getSuccessText() |
||
113 | { |
||
114 | return $this->_successText ?: Yii::t('app', 'Settings saved'); |
||
115 | } |
||
116 | |||
117 | public function setSuccessText($text) |
||
121 | |||
122 | public function getLoadingText() |
||
126 | |||
127 | public function setLoadingText($text) |
||
131 | |||
132 | public function init() |
||
133 | { |
||
134 | if (!$this->scenario) { |
||
135 | throw new InvalidConfigException("Attribute 'scenario' is required"); |
||
136 | } |
||
137 | |||
138 | Html::addCssClass($this->options['class'], 'text-left'); |
||
139 | |||
140 | $this->initAdditionalOptions(); |
||
141 | if ($this->handleSubmit !== false) { |
||
142 | $this->registerClientScript(); |
||
143 | } |
||
144 | parent::init(); |
||
145 | } |
||
146 | |||
147 | protected function initAdditionalOptions() |
||
148 | { |
||
149 | $quotedHtml = Json::htmlEncode($this->loadingHtml); |
||
|
|||
150 | if (!isset($this->clientEvents['show.bs.modal'])) { |
||
151 | if ($this->bulkPage) { |
||
152 | View Code Duplication | if ($this->usePost) { |
|
153 | $this->clientEvents['show.bs.modal'] = new JsExpression("function() { |
||
154 | var selection = jQuery('div[role=\"grid\"]').yiiGridView('getSelectedRows'); |
||
155 | $.post('{$this->actionUrl}', {selection: selection}).done(function (data) { |
||
156 | $('#{$this->id} .modal-body').html(data); |
||
157 | }); |
||
158 | }"); |
||
159 | } else { |
||
160 | $this->clientEvents['show.bs.modal'] = new JsExpression("function() { |
||
161 | var selection = jQuery('div[role=\"grid\"]').yiiGridView('getSelectedRows'); |
||
162 | $.get('{$this->actionUrl}', {selection: selection}).done(function (data) { |
||
163 | $('#{$this->id} .modal-body').html(data); |
||
164 | }); |
||
165 | }"); |
||
166 | } |
||
167 | View Code Duplication | } else { |
|
168 | if ($this->usePost) { |
||
169 | $this->clientEvents['show.bs.modal'] = new JsExpression("function() { |
||
170 | $.post('{$this->actionUrl}').done(function (data) { |
||
171 | $('#{$this->id} .modal-body').html(data); |
||
172 | }); |
||
173 | }"); |
||
174 | } else { |
||
175 | $this->clientEvents['show.bs.modal'] = new JsExpression("function() { |
||
176 | $.get('{$this->actionUrl}').done(function (data) { |
||
177 | $('#{$this->id} .modal-body').html(data); |
||
178 | }); |
||
179 | }"); |
||
180 | } |
||
181 | } |
||
182 | } |
||
183 | if (!isset($this->clientEvents['hidden.bs.modal'])) { |
||
184 | $this->clientEvents['hidden.bs.modal'] = new JsExpression("function() { |
||
185 | jQuery('#{$this->id} .modal-body').html({$quotedHtml}); |
||
186 | }"); |
||
187 | } |
||
188 | } |
||
189 | |||
190 | protected function registerClientScript() |
||
191 | { |
||
192 | $url = is_string($this->handleSubmit) ? $this->handleSubmit : $this->actionUrl; |
||
193 | Yii::$app->view->registerJs(<<<JS |
||
194 | jQuery(document).on('submit', '#{$this->modalFormId}', function(event) { |
||
195 | event.preventDefault(); |
||
196 | var form = jQuery(this); |
||
197 | var btn = jQuery('form[data-pjax] button').button('{$this->loadingText}'); |
||
198 | jQuery.ajax({ |
||
199 | url: '$url', |
||
200 | type: 'POST', |
||
201 | timeout: 0, |
||
202 | data: form.serialize(), |
||
203 | error: function() { |
||
204 | new PNotify({ |
||
205 | text: "{$this->errorText}", |
||
206 | type: 'error', |
||
207 | buttons: { |
||
208 | sticker: false |
||
209 | }, |
||
210 | icon: false |
||
211 | }); |
||
212 | }, |
||
213 | success: function() { |
||
214 | jQuery('#$this->id').modal('hide'); |
||
215 | new PNotify({ |
||
216 | text: "{$this->successText}", |
||
217 | type: 'info', |
||
218 | buttons: { |
||
219 | sticker: false |
||
220 | }, |
||
221 | icon: false |
||
222 | }); |
||
223 | btn.button('reset'); |
||
224 | } |
||
225 | }); |
||
226 | }); |
||
227 | JS |
||
228 | ); |
||
229 | } |
||
230 | |||
231 | public function getLoadingHtml() |
||
232 | { |
||
233 | return <<<HTML |
||
234 | <div class="progress"> |
||
235 | <div class="progress-bar progress-bar-striped active" role="progressbar" aria-valuenow="100" aria-valuemin="0" aria-valuemax="100" style="width: 100%"> |
||
236 | <span class="sr-only">{$this->loadingText}</span> |
||
237 | </div> |
||
238 | </div> |
||
239 | HTML; |
||
240 | } |
||
241 | |||
242 | /** |
||
243 | * {@inheritdoc} |
||
244 | */ |
||
245 | protected function renderBodyBegin() |
||
249 | |||
250 | /** |
||
251 | * {@inheritdoc} |
||
252 | */ |
||
253 | protected function registerClientEvents() |
||
265 | } |
||
266 |
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.