Completed
Push — master ( 4c7769...90a6b5 )
by Stephen
05:33
created

Store   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 97.14%

Importance

Changes 4
Bugs 0 Features 2
Metric Value
wmc 18
lcom 1
cbo 0
dl 0
loc 116
ccs 34
cts 35
cp 0.9714
rs 10
c 4
b 0
f 2

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 18 4
A getIterator() 0 4 1
A offsetExists() 0 7 2
A offsetGet() 0 7 2
A offsetSet() 0 8 3
A offsetUnset() 0 5 3
A count() 0 4 1
A __get() 0 4 1
A __set() 0 4 1
1
<?php
2
namespace StarCitizen\Models;
3
4
use ArrayAccess;
5
use Countable;
6
use IteratorAggregate;
7
use ArrayIterator;
8
9
/**
10
 * Class Store
11
 *
12
 * @package StarCitizen\Models;
13
 */
14
class Store implements ArrayAccess, Countable, IteratorAggregate
15
{
16
    protected $items = [];
17
18
    private $className;
19
20
    /**
21
     * Store constructor.
22
     *
23
     * @param array $data
24
     * @param string $className
25
     * @param string $dataRoot
26
     * @param string $idName
27
     */
28 13
    public function __construct(array $data, $className, $dataRoot, $idName)
29
    {
30 13
        $this->className = 'StarCitizen\Models' . $className;
31
32
        // start count
33 13
        $idCounter = 0;
0 ignored issues
show
Unused Code introduced by
$idCounter 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...
34
35 13
        foreach ($data as $item) {
36
37
            // Check the data root and idName are good
38 13
            $objectData = ($dataRoot != '' ? $item[$dataRoot] : $item);
39 13
            $id = ($idName == '' ? null : $objectData[$idName]);
40
41
            // create the appropriate class and add it to items
42 13
            $storageObject = new \ReflectionClass('StarCitizen\Models' . $className);
43 13
            $this->offsetSet($id, $storageObject->newInstance($objectData));
44
        }
45 13
    }
46
47
    /**
48
     * @return ArrayIterator
49
     */
50 11
    public function getIterator()
51
    {
52 11
        return new ArrayIterator($this->items);
53
    }
54
55
    /**
56
     * @param mixed $offset
57
     *
58
     * @return bool
59
     */
60 5
    public function offsetExists($offset)
61
    {
62 5
        if (array_key_exists($offset, $this->items))
63 3
            return true;
64
65 2
        return false;
66
    }
67
68
    /**
69
     * @param mixed $offset
70
     *
71
     * @return bool|mixed
72
     */
73 3
    public function offsetGet($offset)
74
    {
75 3
        if ($this->offsetExists($offset))
76 2
            return $this->items[$offset];
77
78 1
        return false;
79
    }
80
81
    /**
82
     * @param mixed $offset
83
     * @param mixed $value
84
     */
85 13
    public function offsetSet($offset, $value)
86
    {
87 13
        if ($value instanceof $this->className)
88 13
            if ($offset == null)
89
                $this->items[] = $value;
90
            else
91 13
                $this->items[$offset] = $value;
92 13
    }
93
94
    /**
95
     * @param mixed $offset
96
     */
97 1
    public function offsetUnset($offset)
98
    {
99 1
        if ($this->offsetExists($offset) && isset($this->items[$offset]))
100
            unset ($this->items[$offset]);
101 1
    }
102
103
    /**
104
     * @return int
105
     */
106 5
    public function count()
107
    {
108 5
        return count($this->items);
109
    }
110
111
    /**
112
     * @param $name
113
     *
114
     * @return bool|mixed
115
     */
116 3
    public function __get($name)
117
    {
118 3
        return $this->offsetGet($name);
119
    }
120
121
    /**
122
     * @param $name
123
     * @param $value
124
     */
125 2
    public function __set($name, $value)
126
    {
127 2
        $this->offsetSet($name, $value);
128
    }
129
}