Completed
Push — master ( bcf6c3...37bd96 )
by
unknown
13s queued 10s
created

assets/js/pg-product-variation-simulator.js   A

Complexity

Total Complexity 8
Complexity/F 4

Size

Lines of Code 38
Function Count 2

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 25
c 0
b 0
f 0
dl 0
loc 38
rs 10
wmc 8
mnd 6
bc 6
fnc 2
bpm 3
cpm 4
noi 5

1 Function

Rating   Name   Duplication   Size   Complexity  
B pg-product-variation-simulator.js ➔ updateSimulator 0 30 7
1
/* global variationSimulatorData   */
2
3
if (typeof variationSimulatorData == 'undefined'){
4
  throw new Error("variationSimulatorData is undefined");
5
}
6
7
window.lastPrice = '';
8
function updateSimulator()
9
{
10
    if (window.WCSimulatorId !== '')
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if window.WCSimulatorId !== "" 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...
11
    {
12
        var updateSelector = variationSimulatorData.variationSelector;
13
        if (updateSelector === 'default') {
14
            updateSelector = 'div.woocommerce-variation-price span.price span.woocommerce-Price-amount';
15
        }
16
17
        var productType = variationSimulatorData.productType;
18
19
        if (productType !=='variable')
20
        {
21
            clearInterval(window.variationInterval);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
22
        }
23
        else
24
        {
25
            var priceDOM = document.querySelector(updateSelector);
26
            if (priceDOM != null) {
0 ignored issues
show
Best Practice introduced by
Comparing priceDOM to null using the != operator is not safe. Consider using !== instead.
Loading history...
27
                var newPrice = priceDOM.innerText;
28
                if (newPrice !== window.lastPrice) {
0 ignored issues
show
Complexity Best Practice introduced by
There is no return statement if newPrice !== window.lastPrice 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...
29
                    window.lastPrice = newPrice;
30
                    window.pgSDK.simulator.update(window.WCSimulatorId, {itemAmountSelector: updateSelector})
31
                }
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
32
            } else {
33
                return false;
34
            }
35
        }
36
    }
37
}
38
window.variationInterval = setInterval(function () {
39
    updateSimulator();
40
}, 5000);