Passed
Push — master ( d5ff9b...1c7575 )
by Carsten
02:03
created

Runtime::insertUpdate()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * This file is part of Lenius Basket, a PHP package to handle
5
 * your shopping basket.
6
 *
7
 * Copyright (c) 2017 Lenius.
8
 * http://github.com/lenius/basket
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 *
13
 * @author Carsten Jonstrup<[email protected]>
14
 * @copyright 2017 Lenius.
15
 *
16
 * @version production
17
 *
18
 * @link http://github.com/lenius/basket
19
 */
20
21
namespace Lenius\Basket\Storage;
22
23
use Lenius\Basket\Item;
24
use Lenius\Basket\StorageInterface;
25
26
/**
27
 * @property  id
28
 */
29
class Runtime implements StorageInterface
30
{
31
    protected $identifier;
32
    protected static $cart = [];
33
34
    /**
35
     * Add or update an item in the cart.
36
     *
37
     * @param Item $item The item to insert or update
38
     *
39
     * @return void
40
     */
41 20
    public function insertUpdate(Item $item)
42
    {
43 20
        static::$cart[$this->id][$item->identifier] = $item;
0 ignored issues
show
Documentation introduced by
The property $identifier is declared protected in Lenius\Basket\Item. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write 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.");
        }
    }

}

Since the property has write access only, you can use the @property-write 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...
Bug introduced by
The property id 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...
44 20
    }
45
46
    /**
47
     * Retrieve the cart data.
48
     *
49
     * @return array
50
     */
51 15
    public function &data($asArray = false)
52
    {
53 15
        $cart = &static::$cart[$this->id];
54
55 15
        if (!$asArray) {
56 14
            return $cart;
57
        }
58
59 1
        $data = $cart;
60
61 1
        foreach ($data as &$item) {
62 1
            $item = $item->toArray();
63
        }
64
65 1
        return $data;
66
    }
67
68
    /**
69
     * Check if the item exists in the cart.
70
     *
71
     * @param mixed $identifier
72
     *
73
     * @return bool
74
     *
75
     * @internal param mixed $id
76
     */
77 20 View Code Duplication
    public function has($identifier)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
78
    {
79 20
        foreach (static::$cart[$this->id] as $item) {
80 2
            if ($item->identifier == $identifier) {
81 2
                return true;
82
            }
83
        }
84
85 20
        return false;
86
    }
87
88
    /**
89
     * Get a single cart item by id.
90
     *
91
     * @param mixed $identifier
92
     *
93
     * @return bool|Item
94
     *
95
     * @internal param mixed $id The item id
96
     */
97 6 View Code Duplication
    public function item($identifier)
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in 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...
98
    {
99 6
        foreach (static::$cart[$this->id] as $item) {
100 6
            if ($item->identifier == $identifier) {
101 6
                return $item;
102
            }
103
        }
104
105
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Lenius\Basket\StorageInterface::item of type Lenius\Basket\Item.

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...
106
    }
107
108
    /**
109
     * Returns the first occurance of an item with a given id.
110
     *
111
     * @param string $id The item id
112
     *
113
     * @return bool|Item
114
     */
115 1
    public function find($id)
116
    {
117 1
        foreach (static::$cart[$this->id] as $item) {
118 1
            if ($item->id == $id) {
119 1
                return $item;
120
            }
121
        }
122
123
        return false;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return false; (false) is incompatible with the return type declared by the interface Lenius\Basket\StorageInterface::find of type Lenius\Basket\Item.

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...
124
    }
125
126
    /**
127
     * Remove an item from the cart.
128
     *
129
     * @param mixed $id
130
     *
131
     * @return void
132
     */
133 2
    public function remove($id)
134
    {
135 2
        unset(static::$cart[$this->id][$id]);
136 2
    }
137
138
    /**
139
     * Destroy the cart.
140
     *
141
     * @return void
142
     */
143 20
    public function destroy()
144
    {
145 20
        static::$cart[$this->id] = [];
146 20
    }
147
148
    /**
149
     * Set the cart identifier.
150
     *
151
     * @param string $id
152
     *
153
     * @internal param string $identifier
154
     */
155 20
    public function setIdentifier($id)
156
    {
157 20
        $this->id = $id;
158
159 20
        if (!array_key_exists($this->id, static::$cart)) {
160 1
            static::$cart[$this->id] = [];
161
        }
162 20
    }
163
164
    /**
165
     * Return the current cart identifier.
166
     *
167
     * @return string The identifier
168
     */
169
    public function getIdentifier()
170
    {
171
        return $this->identifier;
172
    }
173
}
174