Completed
Push — master ( 5c637a...214bce )
by Julien
03:47
created

Cart::remove()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 5
rs 9.4286
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace App\Http\Cart;
4
use Illuminate\Database\Eloquent\Collection;
5
6
7
/**
8
 * Class Cart
9
 * @package App\Http\Cart
10
 */
11
class Cart extends AbstractCart implements ItemInterface, \IteratorAggregate, \Countable{
12
13
    /**
14
     * @var array
15
     */
16
    protected $products;
17
18
    /**
19
     * @var array
20
     */
21
    protected $total;
22
23
    /**
24
     * @var array
25
     */
26
    protected $promotions;
27
28
    /**
29
     * Constructor
30
     */
31
    public function __construct(Array $products = []){
32
        $this->products = $products;
33
        $this->promotions = [];
34
        $this->total = 0;
0 ignored issues
show
Documentation Bug introduced by
It seems like 0 of type integer is incompatible with the declared type array of property $total.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
35
    }
36
37
38
    /**
39
     * @param ItemInterface $item
40
     */
41
    public function add(ItemInterface $item){
42
        $this->products[] = $item;
43
44
        return $this;
45
    }
46
    /**
47
     * @param ItemInterface $item
0 ignored issues
show
Bug introduced by
There is no parameter named $item. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
48
     */
49
    public function addItems(Collection $collection){
50
        foreach($collection as $item){
51
            $this->products[] = $item;
52
        }
53
        return $this->products;
54
    }
55
56
    /**
57
     * @param ItemInterface $item
58
     */
59
    public function remove(ItemInterface $item){
60
        $this->products[array_search($item,$this->products)] = $item;
61
62
        return $this;
63
    }
64
65
66
    /**
67
     * @return array
68
     */
69
    public function clear()
70
    {
71
        $this->products = [];
72
73
        return $this->products;
74
    }
75
76
    /**
77
     * @return array
78
     */
79
    public function all(){
80
81
        return $this->products;
82
    }
83
84
    /**
85
     * @return \ArrayIterator
86
     */
87
    public function getIterator()
88
    {
89
        return new \ArrayIterator($this->products);
90
    }
91
92
    /**
93
     * @return \ArrayIterator
94
     */
95
    public function count()
96
    {
97
        return count($this->products);
0 ignored issues
show
Bug Best Practice introduced by
The return type of return count($this->products); (integer) is incompatible with the return type declared by the abstract method App\Http\Cart\AbstractCart::count of type ArrayIterator.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
98
    }
99
100
101
    /**
102
     * Total TTC
103
     * @param int $taxe
104
     * @return int
105
     */
106
    public function total($taxe = 1)
107
    {
108
        foreach($this as $movie){
109
            $this->total += $movie->getMovie()->prix;
110
        }
111
112
        return $this->total * $taxe;
113
    }
114
115
    /**
116
     * @return array
117
     */
118
    public function getProducts()
119
    {
120
        return $this->products;
121
    }
122
123
    /**
124
     * @param array $products
125
     */
126
    public function setProducts($products)
127
    {
128
        $this->products = $products;
129
    }
130
131
    /**
132
     * @return array
133
     */
134
    public function getTotal()
135
    {
136
        return $this->total;
137
    }
138
139
    /**
140
     * @param array $total
141
     */
142
    public function setTotal($total)
143
    {
144
        $this->total = $total;
145
    }
146
147
    /**
148
     * @return array
149
     */
150
    public function getPromotions()
151
    {
152
        return $this->promotions;
153
    }
154
155
    /**
156
     * @param array $promotions
157
     */
158
    public function setPromotions($promotions)
159
    {
160
        $this->promotions = $promotions;
161
    }
162
163
164
    /**
165
     * @param array $total
0 ignored issues
show
Bug introduced by
There is no parameter named $total. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
166
     */
167
    public function emptycart()
168
    {
169
        return empty($this->products) ? true : false;
170
171
    }
172
173
174
175
176
}
177
178