Passed
Pull Request — develop (#38)
by
unknown
12:36
created

AbstractStructArrayBase::offsetUnset()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 1
dl 0
loc 10
ccs 6
cts 6
cp 1
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WsdlToPhp\PackageBase;
6
7
abstract class AbstractStructArrayBase extends AbstractStructBase implements StructArrayInterface
8
{
9
    /**
10
     * Array that contains values when only one parameter is set when calling __construct method
11
     * @var array
12
     */
13
    private array $internArray = [];
14
15
    /**
16
     * Bool that tells if array is set or not
17
     * @var bool
18
     */
19
    private bool $internArrayIsArray = false;
20
21
    /**
22
     * Items index browser
23
     * @var int
24
     */
25
    private int $internArrayOffset = 0;
26
27
    /**
28
     * Method alias to count
29
     * @return int
30
     */
31 4
    public function length(): int
32
    {
33 4
        $this->initInternArray();
34
35 4
        return $this->count();
36
    }
37
38
    /**
39
     * Method returning item length, alias to length
40
     * @return int
41
     */
42 4
    public function count(): int
43
    {
44 4
        $this->initInternArray();
45
46 4
        return $this->getInternArrayIsArray() ? count($this->getInternArray()) : -1;
47
    }
48
49
    /**
50
     * Method returning the current element
51
     * @return mixed
52
     */
53 6
    public function current()
54
    {
55 6
        $this->initInternArray();
56
57 6
        return $this->offsetGet($this->internArrayOffset);
58
    }
59
60
    /**
61
     * Method moving the current position to the next element
62
     * @return AbstractStructArrayBase
63
     */
64 2
    public function next(): self
65
    {
66 2
        $this->initInternArray();
67
68 2
        return $this->setInternArrayOffset($this->getInternArrayOffset() + 1);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->setInternA...nternArrayOffset() + 1) returns the type WsdlToPhp\PackageBase\AbstractStructArrayBase which is incompatible with the return type mandated by Iterator::next() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
69
    }
70
71
    /**
72
     * Method resetting itemOffset
73
     * @return AbstractStructArrayBase
74
     */
75 2
    public function rewind(): self
76
    {
77 2
        $this->initInternArray();
78
79 2
        return $this->setInternArrayOffset(0);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->setInternArrayOffset(0) returns the type WsdlToPhp\PackageBase\AbstractStructArrayBase which is incompatible with the return type mandated by Iterator::rewind() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
80
    }
81
82
    /**
83
     * Method checking if current itemOffset points to an existing item
84
     * @return bool
85
     */
86 2
    public function valid(): bool
87
    {
88 2
        $this->initInternArray();
89
90 2
        return $this->offsetExists($this->getInternArrayOffset());
91
    }
92
93
    /**
94
     * Method returning current itemOffset value, alias to getInternArrayOffset
95
     * @return int
96
     */
97 2
    public function key(): int
98
    {
99 2
        $this->initInternArray();
100
101 2
        return $this->getInternArrayOffset();
102
    }
103
104
    /**
105
     * Method alias to offsetGet
106
     * @param mixed $index
107
     * @return mixed
108
     */
109 4
    public function item($index)
110
    {
111 4
        $this->initInternArray();
112
113 4
        return $this->offsetGet($index);
114
    }
115
116
    /**
117
     * Default method adding item to array
118
     * @param mixed $item value
119
     * @return AbstractStructArrayBase
120
     */
121 16
    public function add($item): self
122
    {
123
        // init array
124 16
        if (!is_array($this->getPropertyValue($this->getAttributeName()))) {
125 2
            $this->setPropertyValue($this->getAttributeName(), []);
126
        }
127
128
        // current array
129 16
        $currentArray = $this->getPropertyValue($this->getAttributeName());
130 16
        $currentArray[] = $item;
131
        $this
132 16
            ->setPropertyValue($this->getAttributeName(), $currentArray)
133 16
            ->setInternArray($currentArray)
134 16
            ->setInternArrayIsArray(true)
135 16
            ->setInternArrayOffset(0);
136
137 16
        return $this;
138
    }
139
140
    /**
141
     * Method returning the first item
142
     * @return mixed
143
     */
144 2
    public function first()
145
    {
146 2
        $this->initInternArray();
147
148 2
        return $this->item(0);
149
    }
150
151
    /**
152
     * Method returning the last item
153
     * @return mixed
154
     */
155 2
    public function last()
156
    {
157 2
        $this->initInternArray();
158
159 2
        return $this->item($this->length() - 1);
160
    }
161
162
    /**
163
     * Method testing index in item
164
     * @param mixed $offset
165
     * @return bool
166
     */
167 22
    public function offsetExists($offset): bool
168
    {
169 22
        $this->initInternArray();
170
171 22
        return ($this->getInternArrayIsArray() && array_key_exists($offset, $this->getInternArray()));
172
    }
173
174
    /**
175
     * Method returning the item at "index" value
176
     * @param mixed $offset
177
     * @return mixed
178
     */
179 16
    public function offsetGet($offset)
180
    {
181 16
        $this->initInternArray();
182
183 16
        return $this->offsetExists($offset) ? $this->internArray[$offset] : null;
184
    }
185
186
    /**
187
     * Method setting value at offset
188
     * @param mixed $offset
189
     * @param mixed $value
190
     * @return AbstractStructArrayBase
191
     */
192 2
    public function offsetSet($offset, $value): self
193
    {
194 2
        $this->initInternArray();
195
196 2
        $this->internArray[$offset] = $value;
197
198 2
        return $this->setPropertyValue($this->getAttributeName(), $this->internArray);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->setPropert...(), $this->internArray) returns the type WsdlToPhp\PackageBase\AbstractStructArrayBase which is incompatible with the return type mandated by ArrayAccess::offsetSet() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
199
    }
200
201
    /**
202
     * Method unsetting value at offset
203
     * @param mixed $offset
204
     * @return AbstractStructArrayBase
205
     */
206 4
    public function offsetUnset($offset): self
207
    {
208 4
        $this->initInternArray();
209
210 4
        if ($this->offsetExists($offset)) {
211 4
            unset($this->internArray[$offset]);
212 4
            $this->setPropertyValue($this->getAttributeName(), $this->internArray);
213
        }
214
215 4
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type WsdlToPhp\PackageBase\AbstractStructArrayBase which is incompatible with the return type mandated by ArrayAccess::offsetUnset() of void.

In the issue above, the returned value is violating the contract defined by the mentioned interface.

Let's take a look at an example:

interface HasName {
    /** @return string */
    public function getName();
}

class Name {
    public $name;
}

class User implements HasName {
    /** @return string|Name */
    public function getName() {
        return new Name('foo'); // This is a violation of the ``HasName`` interface
                                // which only allows a string value to be returned.
    }
}
Loading history...
216
    }
217
218
    /**
219
     * Method returning intern array to iterate trough
220
     * @return array
221
     */
222 24
    private function getInternArray(): array
223
    {
224 24
        return $this->internArray;
225
    }
226
227
    /**
228
     * Method setting intern array to iterate trough
229
     * @param array $internArray
230
     * @return AbstractStructArrayBase
231
     */
232 24
    private function setInternArray(array $internArray): self
233
    {
234 24
        $this->internArray = $internArray;
235
236 24
        return $this;
237
    }
238
239
    /**
240
     * Method returns intern array index when iterating trough
241
     * @return int
242
     */
243 2
    private function getInternArrayOffset(): int
244
    {
245 2
        return $this->internArrayOffset;
246
    }
247
248
    /**
249
     * Method initiating internArray
250
     * @param array $array the array to iterate trough
251
     * @param bool $internCall indicates that methods is calling itself
252
     * @return AbstractStructArrayBase
253
     */
254 24
    private function initInternArray($array = [], bool $internCall = false): self
255
    {
256 24
        if (is_array($array) && count($array) > 0) {
257
            return $this
258 8
                ->setInternArray($array)
259 8
                ->setInternArrayOffset(0)
260 8
                ->setInternArrayIsArray(true);
261 24
        } elseif (!$this->internArrayIsArray && !$internCall && property_exists($this, $this->getAttributeName())) {
262 8
            return $this->initInternArray($this->getPropertyValue($this->getAttributeName()), true);
263
        }
264
265 24
        return $this;
266
    }
267
268
    /**
269
     * Method setting intern array offset when iterating trough
270
     * @param int $internArrayOffset
271
     * @return AbstractStructArrayBase
272
     */
273 24
    private function setInternArrayOffset(int $internArrayOffset): self
274
    {
275 24
        $this->internArrayOffset = $internArrayOffset;
276
277 24
        return $this;
278
    }
279
280
    /**
281
     * Method returning true if intern array is an actual array
282
     * @return bool
283
     */
284 24
    private function getInternArrayIsArray(): bool
285
    {
286 24
        return $this->internArrayIsArray;
287
    }
288
289
    /**
290
     * Method setting if intern array is an actual array
291
     * @param bool $internArrayIsArray
292
     * @return AbstractStructArrayBase
293
     */
294 24
    private function setInternArrayIsArray(bool $internArrayIsArray = false): self
295
    {
296 24
        $this->internArrayIsArray = $internArrayIsArray;
297
298 24
        return $this;
299
    }
300
}
301