Conditions | 11 |
Total Lines | 52 |
Code Lines | 36 |
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:
Complex classes like cacheflag.js ➔ submitForm often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | /** global: Craft */ |
||
17 | function submitForm() { |
||
18 | |||
19 | if ($submitRequest) { |
||
20 | $submitRequest.abort(); |
||
21 | } |
||
22 | |||
23 | $form.addClass('js-submitting'); |
||
24 | $form.find('.spinner').removeClass('hidden'); |
||
25 | $form.find('input[type="submit"]').prop('disabled', true).addClass('disabled'); |
||
26 | |||
27 | $submitRequest = $.ajax($form.attr('action'), { |
||
28 | data: $form.serialize(), |
||
29 | type: 'POST', |
||
30 | dataType: 'json', |
||
31 | success: function (response) { |
||
32 | if (response.success) { |
||
33 | Craft.cp.displayNotice(response.message); |
||
34 | $form.find('input[type="text"][name^="cacheflags"]').each(function () { |
||
35 | var $input = $(this); |
||
36 | var source = $input.attr('name').replace('cacheflags[', '').replace(']', '').split(':'); |
||
37 | var sourceColumn = source[0] || null; |
||
38 | var sourceValue = (source[1] || '').toString(); |
||
39 | if (!sourceColumn || !sourceValue) { |
||
40 | return; |
||
41 | } |
||
42 | var flags = ''; |
||
43 | for (var i = 0; i < response.flags.length; ++i) { |
||
44 | if ((response.flags[i][sourceColumn] || '').toString() === sourceValue) { |
||
45 | flags = response.flags[i].flags || ''; |
||
46 | break; |
||
47 | } |
||
48 | } |
||
49 | $input.val(flags); |
||
50 | }); |
||
51 | } else { |
||
52 | Craft.cp.displayError(response.message); |
||
53 | } |
||
54 | }, |
||
55 | error: function (response) { |
||
56 | if (response.statusText !== 'abort') { |
||
57 | Craft.cp.displayError(response.statusText); |
||
58 | } |
||
59 | }, |
||
60 | complete: function () { |
||
61 | $submitRequest = null; |
||
62 | $form.removeClass('js-submitting'); |
||
63 | $form.find('.spinner').addClass('hidden'); |
||
64 | $form.find('input[type="submit"]').prop('disabled', false).removeClass('disabled'); |
||
65 | } |
||
66 | }); |
||
67 | |||
68 | } |
||
69 | |||
113 |