checkForExclusions()   C
last analyzed

Complexity

Conditions 12
Paths 11

Size

Total Lines 59
Code Lines 35

Duplication

Lines 18
Ratio 30.51 %

Importance

Changes 0
Metric Value
dl 18
loc 59
rs 6.4485
c 0
b 0
f 0
cc 12
eloc 35
nc 11
nop 1

How to fix   Long Method    Complexity   

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:

1
<?php
2
3
/*
4
 * To change this license header, choose License Headers in Project Properties.
5
 * To change this template file, choose Tools | Templates
6
 * and open the template in the editor.
7
 */
8
9
class DiscountCouponCountriesModifierExtension extends DataExtension
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
10
{
11
12
    /**
13
     * returns TRUE if the country
14
     * is not in the included list or if it is in the excluded list
15
     * in all other cases it returns null to basically ignore the country specific
16
     * rules
17
     *
18
     * @param DiscountCouponOption $coupon
19
     *
20
     * @return bool | null
0 ignored issues
show
Documentation introduced by
Should the return type not be null|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
21
     *
22
     */
23
    public function checkForExclusions($coupon)
24
    {
25
        $countryCode = CountryPrice_EcommerceCountry::get_real_country();
26
        if ($countryCode) {
27
            $countryCode = $countryCode->Code;
28
            $includedCountries = $coupon->IncludedCountries();
29
            $excludedCountries = $coupon->ExcludedCountries();
30
31
            //first situation: no country information => ALLOW => return NULL to ignore this.
32
            if ($includedCountries->count() == 0 && $excludedCountries->count() == 0) {
33
                $this->owner->DebugString .= '--- no country rules apply ---';
34
                return null;
35
            }
36
37
            //second situation: includes and excludes
38
            if ($includedCountries->count() > 0 && $excludedCountries->count() > 0) {
39
                $this->owner->DebugString .= '--- inclusions and exclusions apply - checking for '.$countryCode.' ---';
40
                $returnFlag = true;
41
42
                $includeArray = $includedCountries->column('Code');
43
                if (in_array($countryCode, $includeArray)) {
44
                    $returnFlag = null;
45
                }
46
47
                $excludeArray = $excludedCountries->column('Code');
48
                if (in_array($countryCode, $excludeArray)) {
49
                    $returnFlag = true;
50
                }
51
52
                return $returnFlag;
53
            }
54
55
            //third situation: is it included?
56 View Code Duplication
            if ($includedCountries->count() > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
57
                $this->owner->DebugString .= '--- inclusions apply - checking for '.$countryCode.' ---';
58
                $includeArray = $includedCountries->column('Code');
59
                if (in_array($countryCode, $includeArray)) {
60
                    return null;
61
                } else {
62
                    return true;
63
                }
64
            }
65
66
            //fourth situation: is it excluded?
67 View Code Duplication
            if ($excludedCountries->count() > 0) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
68
                $this->owner->DebugString .= '--- exclusions apply - checking for '.$countryCode.' ---';
69
                $excludeArray = $excludedCountries->column('Code');
70
                if (in_array($countryCode, $excludeArray)) {
71
                    return true;
72
                } else {
0 ignored issues
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
73
                    //return true;
74
                }
75
            }
76
        } else {
77
            $this->owner->DebugString .= '--- no country code could be found ---';
78
        }
79
80
        return null;
81
    }
82
}
83