Completed
Pull Request — master (#187)
by
unknown
01:11
created

ItemCollection::getPriceOfCondition()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.9297
c 0
b 0
f 0
cc 6
nc 6
nop 3
1
<?php namespace Darryldecode\Cart;
2
3
/**
4
 * Created by PhpStorm.
5
 * User: darryl
6
 * Date: 1/17/2015
7
 * Time: 11:03 AM
8
 */
9
10
use Darryldecode\Cart\Helpers\Helpers;
11
use Illuminate\Support\Collection;
12
13
class ItemCollection extends Collection {
14
15
    /**
16
     * Sets the config parameters.
17
     *
18
     * @var
19
     */
20
    protected $config;
21
22
    /**
23
     * ItemCollection constructor.
24
     * @param array|mixed $items
25
     * @param $config
26
     */
27
    public function __construct($items, $config)
28
    {
29
        parent::__construct($items);
30
31
        $this->config = $config;
32
    }
33
34
    /**
35
     * get the sum of price
36
     *
37
     * @return mixed|null
38
     */
39
    public function getPriceSum($formatted = true)
40
    {
41
        return Helpers::formatValue($this->price * $this->quantity, $formatted, $this->config);
0 ignored issues
show
Documentation introduced by
The property price does not exist on object<Darryldecode\Cart\ItemCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Documentation introduced by
The property quantity does not exist on object<Darryldecode\Cart\ItemCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
42
43
    }
44
45
    public function __get($name)
46
    {
47
        if( $this->has($name) ) return $this->get($name);
48
        return null;
49
    }
50
51
    /**
52
     * check if item has conditions
53
     *
54
     * @return bool
55
     */
56
    public function hasConditions()
57
    {
58
        if( ! isset($this['conditions']) ) return false;
59
        if( is_array($this['conditions']) )
60
        {
61
            return count($this['conditions']) > 0;
62
        }
63
        $conditionInstance = "Darryldecode\\Cart\\CartCondition";
64
        if( $this['conditions'] instanceof $conditionInstance ) return true;
65
66
        return false;
67
    }
68
69
    /**
70
     * check if item has conditions
71
     *
72
     * @return mixed|null
73
     */
74
    public function getConditions()
75
    {
76
        if(! $this->hasConditions() ) return [];
77
        return $this['conditions'];
78
    }
79
80
    /**
81
     * get the single price in which conditions are already applied
82
     * @param bool $formatted
83
     * @return mixed|null
84
     */
85
    public function getPriceWithConditions($formatted = true)
86
    {
87
        $originalPrice = $this->price;
0 ignored issues
show
Bug introduced by
The property price does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
88
        $newPrice = 0.00;
89
        $processed = 0;
90
91
        if( $this->hasConditions() )
92
        {
93
            if( is_array($this->conditions) )
0 ignored issues
show
Documentation introduced by
The property conditions does not exist on object<Darryldecode\Cart\ItemCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
94
            {
95
                foreach($this->conditions as $condition)
0 ignored issues
show
Documentation introduced by
The property conditions does not exist on object<Darryldecode\Cart\ItemCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
96
                {
97
                    ( $processed > 0 ) ? $toBeCalculated = $newPrice : $toBeCalculated = $originalPrice;
98
                    $newPrice = $condition->applyCondition($toBeCalculated);
99
                    $processed++;
100
                }
101
            }
102
            else
103
            {
104
                $newPrice = $this['conditions']->applyCondition($originalPrice);
105
            }
106
107
            return Helpers::formatValue($newPrice, $formatted, $this->config);
108
        }
109
        return Helpers::formatValue($originalPrice, $formatted, $this->config);
110
    }
111
112
    /**
113
     * get the sum of price in which conditions are already applied
114
     * @param bool $formatted
115
     * @return mixed|null
116
     */
117
    public function getPriceSumWithConditions($formatted = true)
118
    {
119
        return Helpers::formatValue($this->getPriceWithConditions(false) * $this->quantity, $formatted, $this->config);
0 ignored issues
show
Documentation introduced by
The property quantity does not exist on object<Darryldecode\Cart\ItemCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
120
    }
121
122
    /**
123
     * get the sum of condition type
124
     * @param string $type
125
     * @param bool $multiple
126
     * @param bool $formatted
127
     * @return mixed|null
128
     */
129
    public function getPriceOfCondition($type, $multiple = true, $formatted = true)
130
    {
131
        $originalPrice = $this->price;
132
        $newPrice = 0.00;
0 ignored issues
show
Unused Code introduced by
$newPrice is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
133
        $processed = 0;
0 ignored issues
show
Unused Code introduced by
$processed is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
134
        $quantity = ($multiple == true) ? $this->quantity : 1;
0 ignored issues
show
Documentation introduced by
The property quantity does not exist on object<Darryldecode\Cart\ItemCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
135
136
        if ($this->hasConditions()) {
137
            if (is_array($this->conditions)) {
0 ignored issues
show
Documentation introduced by
The property conditions does not exist on object<Darryldecode\Cart\ItemCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
138
                foreach ($this->conditions as $condition) {
0 ignored issues
show
Documentation introduced by
The property conditions does not exist on object<Darryldecode\Cart\ItemCollection>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
139
                    if ($condition->getType() == $type) {
140
                        $conditionValue = $condition->getValue() * $quantity;
141
                    }
142
                }
143
            } else {
144
                $conditionValue = $this['conditions']->getValue() * $quantity;
145
            }
146
147
            return Helpers::formatValue($conditionValue, $formatted, $this->config);
0 ignored issues
show
Bug introduced by
The variable $conditionValue 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...
148
        }
149
150
        return Helpers::formatValue($originalPrice, $formatted, $this->config);
151
    }
152
}
153