Completed
Push — master ( 1137b9...97c77e )
by
unknown
20s queued 11s
created

paylater.js ➔ define   B

Complexity

Conditions 1
Paths 3

Size

Total Lines 81
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
cc 1
eloc 47
c 4
b 0
f 0
nc 3
nop 12
dl 0
loc 81
rs 8.7345

11 Functions

Rating   Name   Duplication   Size   Complexity  
A paylater.js ➔ ... ➔ Component.extend.dataPmtMaxIns 0 3 1
A paylater.js ➔ ... ➔ Component.extend.loadSimulator 0 16 1
A paylater.js ➔ ... ➔ Component.extend.getSubtitle 0 3 1
A paylater.js ➔ ... ➔ Component.extend.getPmtType 0 3 1
A paylater.js ➔ ... ➔ Component.extend.getPmtNumQuota 0 3 1
A paylater.js ➔ ... ➔ Component.extend.placeOrder 0 10 1
A paylater.js ➔ ... ➔ Component.extend.getPublicKey 0 3 1
A paylater.js ➔ ... ➔ Component.extend.getPmtTotal 0 3 1
A paylater.js ➔ ... ➔ Component.extend.getSecretKey 0 3 1
A paylater.js ➔ ... ➔ Component.extend.selectPaymentMethod 0 5 1
A paylater.js ➔ ... ➔ Component.extend.getDisplayMode 0 3 1

How to fix   Long Method    Many Parameters   

Long Method

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:

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
define(
2
    [
3
        'jquery',
4
        'Magento_Checkout/js/view/payment/default',
5
        'mage/url',
6
        'Magento_Customer/js/customer-data',
7
        'Magento_Checkout/js/model/error-processor',
8
        'Magento_Checkout/js/model/full-screen-loader',
9
        'Magento_Checkout/js/model/quote',
10
        '//cdn.pagamastarde.com/pmt-js-client-sdk/3/js/client-sdk.min.js',
11
        'Magento_Checkout/js/action/select-payment-method',
12
        'Magento_Checkout/js/checkout-data',
13
        'Magento_Checkout/js/model/totals',
14
        'Magento_Catalog/js/price-utils'
15
    ],
16
    function ($, Component, url, customerData, errorProcessor, fullScreenLoader, quote, pmtClient, selectPaymentMethodAction, checkoutData, totals, priceUtils) {
17
        'use strict';
18
19
        window.checkoutConfig.payment.paylater.guestEmail = quote.guestEmail;
20
21
        window.pmtClient = pmtClient;
22
23
        return Component.extend({
24
                defaults: {
25
                    template: 'DigitalOrigin_Pmt/payment/checkout-form'
26
                },
27
28
                redirectAfterPlaceOrder: false,
29
30
                loadSimulator: function ()
31
                {
32
                    setTimeout(function(){
33
                        if (window.checkoutConfig.payment.paylater.pmtType  !='0' &&
0 ignored issues
show
Complexity Best Practice introduced by pgarces
There is no return statement if window.checkoutConfig.pa...aylater.secretKey != "" is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
34
                            window.checkoutConfig.payment.paylater.publicKey!=''  &&
35
                            window.checkoutConfig.payment.paylater.secretKey!='')
36
                        {
37
                            if (typeof window.pmtClient !== 'undefined')
0 ignored issues
show
Complexity Best Practice introduced by pgarces
There is no return statement if typeof window.pmtClient !== "undefined" is false. Are you sure this is correct? If so, consider adding return; explicitly.

This check looks for functions where a return statement is found in some execution paths, but not in all.

Consider this little piece of code

function isBig(a) {
    if (a > 5000) {
        return "yes";
    }
}

console.log(isBig(5001)); //returns yes
console.log(isBig(42)); //returns undefined

The function isBig will only return a specific value when its parameter is bigger than 5000. In any other case, it will implicitly return undefined.

This behaviour may not be what you had intended. In any case, you can add a return undefined to the other execution path to make the return value explicit.

Loading history...
38
                            {
39
                                window.pmtClient.setPublicKey(window.checkoutConfig.payment.paylater.publicKey);
40
                                window.pmtClient.simulator.reload();
41
                                return true;
42
                            }
43
                        }
44
                    }, 3000);
45
                },
46
47
                getSubtitle: function () {
48
                    return window.checkoutConfig.payment.paylater.subtitle
49
                },
50
51
                getPmtNumQuota: function () {
52
                    return window.checkoutConfig.payment.paylater.pmtNumQuota
53
                },
54
55
                dataPmtMaxIns: function () {
56
                    return window.checkoutConfig.payment.paylater.pmtMaxIns
57
                },
58
59
                getPmtType: function () {
60
                    return window.checkoutConfig.payment.paylater.pmtType
61
                },
62
63
                getPmtTotal: function () {
64
                    return priceUtils.formatPrice(totals.totals().grand_total, quote.getPriceFormat());
65
                },
66
67
                getPublicKey: function () {
68
                    return window.checkoutConfig.payment.paylater.publicKey
69
                },
70
71
                getSecretKey: function () {
72
                    return window.checkoutConfig.payment.paylater.secretKey
73
                },
74
75
                getDisplayMode: function () {
76
                    return window.checkoutConfig.payment.paylater.displayMode
77
                },
78
79
                selectPaymentMethod: function() {
80
                    selectPaymentMethodAction(this.getData());
81
                    checkoutData.setSelectedPaymentMethod(this.item.method);
82
                    return true;
83
                },
84
85
                placeOrder: function () {
86
                    var paymentUrl = url.build('paylater/Payment');
87
                    $.post(paymentUrl, { email: window.checkoutConfig.payment.paylater.guestEmail }, 'json')
88
                        .done(function (response) {
89
                            window.location.replace(response);
90
                        })
91
                        .fail(function (response) {
92
                            window.location.replace(response);
93
                        })
94
                },
95
            });
96
    }
97
);
98