| Conditions | 1 |
| Paths | 1 |
| Total Lines | 118 |
| 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 | ;(function ($, undefined) { |
||
|
|
|||
| 2 | |||
| 3 | /** Plugin constructor */ |
||
| 4 | $.plugin('swagKlarnaCheckout', { |
||
| 5 | init: function () { |
||
| 6 | var me = this; |
||
| 7 | |||
| 8 | me.$klarnaTab = $('.label-klarna-register'); |
||
| 9 | me.$b2bSelect = $('#b2bselect'); |
||
| 10 | me.klarnaLink = me.$klarnaTab.find('a'); |
||
| 11 | me.$otherPayments = $('.select--other-payments'); |
||
| 12 | me.$paymentForm = $('.klarna--select-form'); |
||
| 13 | me.$autoSubmit = $('.klarna--auto-submit'); |
||
| 14 | |||
| 15 | me.registerEvents(); |
||
| 16 | }, |
||
| 17 | |||
| 18 | /** |
||
| 19 | * Helper method to register the needed events |
||
| 20 | */ |
||
| 21 | registerEvents: function () { |
||
| 22 | var me = this; |
||
| 23 | |||
| 24 | me._on(me.$klarnaTab, me.getEventName('click'), $.proxy(me.onChangeTabs, me)); |
||
| 25 | me._on(me.$otherPayments, me.getEventName('click'), $.proxy(me.onChangePayment, me)); |
||
| 26 | me._on(me.$b2bSelect, me.getEventName('change'), $.proxy(me.onSelectB2b, me)); |
||
| 27 | me._on(me.$autoSubmit, me.getEventName('change'), $.proxy(me.onChangeRadio, me)); |
||
| 28 | |||
| 29 | $.subscribe('plugin/swAjaxVariant/onRequestData', $.proxy(me.reloadScript, me)); |
||
| 30 | }, |
||
| 31 | |||
| 32 | /** |
||
| 33 | * Method to change the dispatch. |
||
| 34 | * It simply submits the wrapping form to reload the site. |
||
| 35 | * @param event |
||
| 36 | */ |
||
| 37 | onChangeRadio: function (event) { |
||
| 38 | $(event.currentTarget).parents('form').submit(); |
||
| 39 | }, |
||
| 40 | |||
| 41 | /** |
||
| 42 | * This method is called when the user clicks on 'other payments' in the klarna checkout. |
||
| 43 | */ |
||
| 44 | onChangePayment: function () { |
||
| 45 | var me = this; |
||
| 46 | |||
| 47 | me.changePayment(); |
||
| 48 | }, |
||
| 49 | |||
| 50 | /** |
||
| 51 | * This method is called when the user clicks on 'Company' in the b2b-select in the klarna checkout. |
||
| 52 | * |
||
| 53 | * @param event |
||
| 54 | */ |
||
| 55 | onSelectB2b: function (event) { |
||
| 56 | var me = this, |
||
| 57 | $el = $(event.currentTarget); |
||
| 58 | |||
| 59 | if ($el.find(":selected").hasClass('is--b2b')) { |
||
| 60 | me.changePayment(); |
||
| 61 | } |
||
| 62 | }, |
||
| 63 | |||
| 64 | /** |
||
| 65 | * This method is called when the user clicks on klarna checkout tab in registration page. |
||
| 66 | * |
||
| 67 | * @param event |
||
| 68 | */ |
||
| 69 | onChangeTabs: function (event) { |
||
| 70 | var me = this, |
||
| 71 | form = $('.register--form').serialize(), |
||
| 72 | link = me.klarnaLink.attr('href'); |
||
| 73 | |||
| 74 | event.preventDefault(); |
||
| 75 | |||
| 76 | $.ajax({ |
||
| 77 | type: "POST", |
||
| 78 | url: $(event.currentTarget).attr('data-form-url'), |
||
| 79 | data: form |
||
| 80 | }).done(function () { |
||
| 81 | window.location = link; |
||
| 82 | }); |
||
| 83 | }, |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Helper method to actually submit the "other payments"-form upon either clicking on "other payments" or |
||
| 87 | * clicking on "company" in the b2b-select. |
||
| 88 | */ |
||
| 89 | changePayment: function () { |
||
| 90 | var me = this; |
||
| 91 | |||
| 92 | me.$paymentForm.submit(); |
||
| 93 | }, |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Needed to reload the klarna-widgets on the detail page after changing the ajax-variant. |
||
| 97 | */ |
||
| 98 | reloadScript: function () { |
||
| 99 | var me = this, |
||
| 100 | script = $('.klarna--widget-script'), |
||
| 101 | src = script.attr('src'); |
||
| 102 | |||
| 103 | script.after($('<script>').addClass('klarna--widget-script').attr('src', src)); |
||
| 104 | script.remove(); |
||
| 105 | }, |
||
| 106 | |||
| 107 | /** Destroys the plugin */ |
||
| 108 | destroy: function () { |
||
| 109 | $.unsubscribe('plugin/swAjaxVariant/onRequestData'); |
||
| 110 | |||
| 111 | this._destroy(); |
||
| 112 | } |
||
| 113 | }); |
||
| 114 | |||
| 115 | $(document).ready(function () { |
||
| 116 | $('body').swagKlarnaCheckout(); |
||
| 117 | }); |
||
| 118 | })(jQuery); |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.