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; |
|
|
|
|
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
|
|
|
} |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.