| Conditions | 11 |
| Paths | 11 |
| Total Lines | 105 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 2 |
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 gaaf.js ➔ ... ➔ fillInAddress 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 | jQuery(function ($) { |
||
| 7 | function fillInAddress() { |
||
| 8 | var place = autocomplete.getPlace(); |
||
| 9 | console.log(place); |
||
| 10 | var temp_state = ''; |
||
| 11 | |||
| 12 | for (var i = 0; i < place.address_components.length; i++) { |
||
| 13 | |||
| 14 | var address_type = place.address_components[i].types[0]; |
||
| 15 | |||
| 16 | switch (address_type) { |
||
| 17 | case 'route': |
||
| 18 | var val = place.address_components[i]['short_name']; |
||
| 19 | document.getElementById('wpinv_address').value = val; |
||
| 20 | break; |
||
| 21 | case 'postal_town': |
||
| 22 | var val = place.address_components[i]['short_name']; |
||
| 23 | document.getElementById('wpinv_city').value = val; |
||
| 24 | break; |
||
| 25 | case 'locality': |
||
| 26 | var val = place.address_components[i]['long_name']; |
||
| 27 | document.getElementById('wpinv_city').value = val; |
||
| 28 | break; |
||
| 29 | case 'administrative_area_level_1': |
||
| 30 | var val = place.address_components[i]['short_name']; |
||
| 31 | temp_state = val; |
||
| 32 | break; |
||
| 33 | case 'country': |
||
| 34 | var val = place.address_components[i]['short_name']; |
||
| 35 | |||
| 36 | $('#wpinv_country').val(val); |
||
| 37 | |||
| 38 | var elB = $('#wpinv-address'); |
||
| 39 | var elF = $('#wpinv-fields'); |
||
| 40 | data = { |
||
| 41 | action: 'wpinv_get_states_field', |
||
| 42 | country: val, |
||
| 43 | field_name: 'wpinv_state', |
||
| 44 | }; |
||
| 45 | |||
| 46 | if(elB.length){ |
||
| 47 | var $this = $('#wpinv_country', elB); |
||
| 48 | |||
| 49 | $this.closest('.gdmbx-row').find('.wpi-loader').show(); |
||
| 50 | $('#wpinv_state', elB).css({ |
||
| 51 | 'opacity': '.5' |
||
| 52 | }); |
||
| 53 | $.post(ajaxurl, data, function(response) { |
||
| 54 | if ('nostates' === response || '' == temp_state) { |
||
| 55 | var text_field = '<input type="text" value="' + temp_state + '" id="wpinv_state" name="wpinv_state" />'; |
||
| 56 | $('#wpinv_state', elB).replaceWith(text_field); |
||
| 57 | } else { |
||
| 58 | $('#wpinv_state', elB).replaceWith(response); |
||
| 59 | $('#wpinv_state', elB).find('option[value="' + temp_state + '"]').attr('selected', 'selected'); |
||
| 60 | $('#wpinv_state', elB).find('option[value=""]').remove(); |
||
| 61 | } |
||
| 62 | |||
| 63 | $('#wpinv_state', elB).addClass('gdmbx2-text-large'); |
||
| 64 | if (typeof $this.data('change') === '1') { |
||
| 65 | $('#wpinv_state', elB).change(); |
||
| 66 | } else { |
||
| 67 | window.wpiConfirmed = true; |
||
| 68 | $('#wpinv-recalc-totals').click(); |
||
| 69 | window.wpiConfirmed = false; |
||
| 70 | } |
||
| 71 | |||
| 72 | $this.closest('.gdmbx-row').find('.wpi-loader').hide(); |
||
| 73 | $('#wpinv_state', elB).css({ |
||
| 74 | 'opacity': '1' |
||
| 75 | }); |
||
| 76 | }); |
||
| 77 | } else if(elF.length){ |
||
| 78 | $('.wpinv_errors').remove(); |
||
| 79 | wpinvBlock(jQuery('#wpinv_state_box')); |
||
| 80 | var $this = $('#wpinv_country', elF); |
||
| 81 | $.post(ajaxurl, data, function(response) { |
||
| 82 | if ('nostates' === response) { |
||
| 83 | var text_field = '<input type="text" required="required" class="wpi-input required" id="wpinv_state" name="wpinv_state">'; |
||
| 84 | $('#wpinv_state', elF).replaceWith(text_field); |
||
| 85 | } else { |
||
| 86 | $('#wpinv_state', elF).replaceWith(response); |
||
| 87 | $('#wpinv_state', elF).find('option[value="' + temp_state + '"]').attr('selected', 'selected'); |
||
| 88 | var changeState = function() { |
||
| 89 | wpinv_recalculate_taxes(temp_state); |
||
| 90 | }; |
||
| 91 | $("#wpinv_state", elF).unbind("change", changeState); |
||
| 92 | $("#wpinv_state", elF).bind("change", changeState); |
||
| 93 | } |
||
| 94 | $('#wpinv_state', elF).find('option[value=""]').remove(); |
||
| 95 | $('#wpinv_state', elF).addClass('form-control wpi-input required'); |
||
| 96 | }).done(function(data) { |
||
| 97 | jQuery('#wpinv_state_box').unblock(); |
||
| 98 | wpinv_recalculate_taxes(); |
||
| 99 | }); |
||
| 100 | } |
||
| 101 | |||
| 102 | break; |
||
| 103 | case 'postal_code': |
||
| 104 | var val = place.address_components[i]['short_name']; |
||
| 105 | document.getElementById('wpinv_zip').value = val; |
||
| 106 | break; |
||
| 107 | default: |
||
| 108 | break; |
||
| 109 | } |
||
| 110 | } |
||
| 111 | } |
||
| 112 | }); |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.