Completed
Push — master ( 671816...b541a8 )
by
unknown
01:23
created

setCanBeNotDiscounted()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
4
class DiscountCouponProductDataExtension extends DataExtension
5
{
6
7
8
    /**
9
     * stadard SS declaration
10
     * @var Array
11
     */
12
    private static $belongs_many_many = array(
13
        "ApplicableDiscountCoupons" => "DiscountCouponOption"
14
    );
15
16
17
    /**
18
     * Update Fields
19
     * @return FieldList
20
     */
21
    public function updateCMSFields(FieldList $fields)
22
    {
23
        $fields->addFieldsToTab(
24
            'Root.Discount',
25
            GridField::create(
0 ignored issues
show
Documentation introduced by
\GridField::create('Appl...lationEditor::create()) is of type object<GridField>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
26
                'ApplicableDiscountCoupons',
27
                'Discount Coupons',
28
                $this->owner->ApplicableDiscountCoupons(),
29
                GridFieldConfig_RelationEditor::create()
30
            )
31
        );
32
        return $fields;
33
    }
34
35
    protected static $buyable_to_be_excluded_from_discounts = [];
36
37
    public static function add_buyable_to_be_excluded($buyableOrBuyableID)
0 ignored issues
show
Unused Code introduced by
The parameter $buyableOrBuyableID is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
38
    {
39
        if(is_object($buyable)) {
0 ignored issues
show
Bug introduced by
The variable $buyable does not exist. Did you mean $buyableOrBuyableID?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
40
            $id = $buyable->ID;
0 ignored issues
show
Bug introduced by
The variable $buyable does not exist. Did you mean $buyableOrBuyableID?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
41
        } elseif(intval($buyable)) {
0 ignored issues
show
Bug introduced by
The variable $buyable does not exist. Did you mean $buyableOrBuyableID?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
42
            $id = intval($buyable);
0 ignored issues
show
Bug introduced by
The variable $buyable does not exist. Did you mean $buyableOrBuyableID?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
43
        }
44
45
        self::$buyable_to_be_excluded_from_discounts[$id] = $id;
0 ignored issues
show
Bug introduced by
The variable $id does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
46
    }
47
48
    public function setCanBeNotDiscounted()
49
    {
50
        self::$buyable_to_be_excluded_from_discounts[$this->owner->ID] = $this->owner->ID;
0 ignored issues
show
Bug introduced by
The property ID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
51
52
        return $this;
53
    }
54
55
    public function getCanBeDiscounted()
56
    {
57
        return isset(self::$buyable_to_be_excluded_from_discounts[$this->owner->ID]) ? false : true;
0 ignored issues
show
Bug introduced by
The property ID does not seem to exist in SS_Object.

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
58
    }
59
60
    /**
61
     * @param float $price
0 ignored issues
show
Documentation introduced by
Should the type for parameter $price not be double|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
62
     *
63
     * @return float | null
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|double|null?

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...
64
     */
65
    public function updateCalculatedPrice($price = null)
66
    {
67
        if($this->getCanBeDiscounted()) {
68
            $hasDiscount = false;
69
            $coupons = $this->owner->DirectlyApplicableDiscountCoupons();
70
            if ($coupons && $coupons->count()) {
71
                $discountPercentage = 0;
72
                $discountAbsolute = 0;
73
                foreach ($coupons as $coupon) {
74
                    if ($coupon->isValid()) {
75
                        $hasDiscount = true;
76
                        if ($coupon->DiscountPercentage > $discountPercentage) {
77
                            $discountPercentage = $coupon->DiscountPercentage;
78
                        }
79
                        if ($coupon->DiscountAbsolute > $discountAbsolute) {
80
                            $discountAbsolute = $coupon->DiscountAbsolute;
81
                        }
82
                    }
83
                }
84
                if ($hasDiscount) {
85
                    $priceWithPercentageDiscount = $price - ($price * ($discountPercentage / 100));
86
                    $priceWithAbsoluteDiscount = $price - $discountAbsolute;
87
                    if ($priceWithPercentageDiscount < $priceWithAbsoluteDiscount) {
88
                        return $priceWithPercentageDiscount;
89
                    } else {
90
                        return $priceWithAbsoluteDiscount;
91
                    }
92
                }
93
            }
94
        }
95
    }
96
97
    public function DirectlyApplicableDiscountCoupons()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
98
    {
99
        return $this->owner->ApplicableDiscountCoupons()
100
            ->filter(array("ApplyPercentageToApplicableProducts" => 1, "ApplyEvenWithoutCode" => 1));
101
    }
102
103
    /**
104
     *
105
     * @return SS_Date
0 ignored issues
show
Documentation introduced by
Should the return type not be DBField|null?

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...
106
     */
107
    public function DiscountsAvailableUntil()
108
    {
109
        $coupons = $this->DirectlyApplicableDiscountCoupons();
110
        $next = strtotime('+100 years');
111
        if ($coupons && $coupons->count()) {
112
            $discount = 0;
113
            foreach ($coupons as $coupon) {
114
                if ($coupon->isValid()) {
115
                    if ($coupon->EndDate && $coupon->DiscountAbsolute > $discount) {
116
                        $discount = $coupon->DiscountAbsolute;
117
                        $maxDate = strtotime($coupon->EndDate);
118
                        if ($maxDate < $next) {
119
                            $next = $maxDate;
120
                        }
121
                    }
122
                }
123
            }
124
        }
125
        if ($next) {
126
            return DBField::create_field('Date', $next);
127
        }
128
    }
129
}
130