SessionStorage::read()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
/*
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the MIT license.
17
 */
18
19
namespace Cart\Storage;
20
21
/**
22
 * @author Rafael Queiroz <[email protected]>
23
 */
24
class SessionStorage implements StorageInterface
25
{
26
    /**
27
     * @var string
28
     */
29
    protected $_key = "Cart";
30
31
    /**
32
     * @var \Cake\Network\Session
33
     */
34
    protected $_session;
35
36
    /**
37
     * @var array
38
     */
39
    protected $_objects;
40
41
    /**
42
     * @param \Cake\Http\ServerRequest $request
43
     */
44
    public function __construct($request)
45
    {
46
        $this->_session = $request->session();
47
    }
48
49
    /**
50
     * @return array
51
     */
52
    public function read()
53
    {
54
        $this->_objects = $this->_session->read($this->_key) ?: [];
0 ignored issues
show
Documentation Bug introduced by
It seems like $this->_session->read($this->_key) ?: array() can also be of type string. However, the property $_objects is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

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

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
55
        return $this->_objects;
56
    }
57
58
    /**
59
     * @param $objects
60
     * @return void
61
     */
62
    public function write($objects)
63
    {
64
        $this->_objects = $objects;
65
        $this->_session->write($this->_key, $this->_objects);
66
    }
67
68
    /**
69
     * @return void
70
     */
71
    public function delete()
72
    {
73
        $this->_session->delete($this->_key);
74
    }
75
76
}
77