Conditions | 1 |
Paths | 1 |
Total Lines | 81 |
Code Lines | 23 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
220 | protected function script() |
||
221 | { |
||
222 | $deleteConfirm = trans('admin.delete_confirm'); |
||
223 | $saveSucceeded = trans('admin.save_succeeded'); |
||
224 | $refreshSucceeded = trans('admin.refresh_succeeded'); |
||
225 | $deleteSucceeded = trans('admin.delete_succeeded'); |
||
226 | $confirm = trans('admin.confirm'); |
||
227 | $cancel = trans('admin.cancel'); |
||
228 | |||
229 | $nestableOptions = json_encode($this->nestableOptions); |
||
230 | |||
231 | return <<<SCRIPT |
||
232 | |||
233 | $('#{$this->elementId}').nestable($nestableOptions); |
||
234 | |||
235 | $('.tree_branch_delete').click(function() { |
||
236 | var id = $(this).data('id'); |
||
237 | swal({ |
||
238 | title: "$deleteConfirm", |
||
239 | type: "warning", |
||
240 | showCancelButton: true, |
||
241 | confirmButtonColor: "#DD6B55", |
||
242 | confirmButtonText: "$confirm", |
||
243 | closeOnConfirm: false, |
||
244 | cancelButtonText: "$cancel" |
||
245 | }, |
||
246 | function(){ |
||
247 | $.ajax({ |
||
248 | method: 'post', |
||
249 | url: '{$this->path}/' + id, |
||
250 | data: { |
||
251 | _method:'delete', |
||
252 | _token:LA.token, |
||
253 | }, |
||
254 | success: function (data) { |
||
255 | $.pjax.reload('#pjax-container'); |
||
256 | |||
257 | if (typeof data === 'object') { |
||
258 | if (data.status) { |
||
259 | swal(data.message, '', 'success'); |
||
260 | } else { |
||
261 | swal(data.message, '', 'error'); |
||
262 | } |
||
263 | } |
||
264 | } |
||
265 | }); |
||
266 | }); |
||
267 | }); |
||
268 | |||
269 | $('.{$this->elementId}-save').click(function () { |
||
270 | var serialize = $('#{$this->elementId}').nestable('serialize'); |
||
271 | |||
272 | $.post('{$this->path}', { |
||
273 | _token: LA.token, |
||
274 | _order: JSON.stringify(serialize) |
||
275 | }, |
||
276 | function(data){ |
||
277 | $.pjax.reload('#pjax-container'); |
||
278 | toastr.success('{$saveSucceeded}'); |
||
279 | }); |
||
280 | }); |
||
281 | |||
282 | $('.{$this->elementId}-refresh').click(function () { |
||
283 | $.pjax.reload('#pjax-container'); |
||
284 | toastr.success('{$refreshSucceeded}'); |
||
285 | }); |
||
286 | |||
287 | $('.{$this->elementId}-tree-tools').on('click', function(e){ |
||
288 | var target = $(e.target), |
||
289 | action = target.data('action'); |
||
290 | if (action === 'expand') { |
||
291 | $('.dd').nestable('expandAll'); |
||
292 | } |
||
293 | if (action === 'collapse') { |
||
294 | $('.dd').nestable('collapseAll'); |
||
295 | } |
||
296 | }); |
||
297 | |||
298 | |||
299 | SCRIPT; |
||
300 | } |
||
301 | |||
380 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: