AbstractStructArrayBase::count()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 5
ccs 3
cts 3
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 3
    #[\ReturnTypeWillChange]
54 3
    public function current()
55
    {
56 6
        $this->initInternArray();
57
58 6
        return $this->offsetGet($this->internArrayOffset);
59
    }
60
61
    /**
62
     * Method moving the current position to the next element
63
     * @return AbstractStructArrayBase
64
     */
65 1
    #[\ReturnTypeWillChange]
66 1
    public function next(): self
67
    {
68 2
        $this->initInternArray();
69
70 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...
71
    }
72
73
    /**
74
     * Method resetting itemOffset
75
     * @return AbstractStructArrayBase
76
     */
77 1
    #[\ReturnTypeWillChange]
78 1
    public function rewind(): self
79
    {
80 2
        $this->initInternArray();
81
82 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...
83
    }
84
85
    /**
86
     * Method checking if current itemOffset points to an existing item
87
     * @return bool
88
     */
89 2
    public function valid(): bool
90
    {
91 2
        $this->initInternArray();
92
93 2
        return $this->offsetExists($this->getInternArrayOffset());
94
    }
95
96
    /**
97
     * Method returning current itemOffset value, alias to getInternArrayOffset
98
     * @return int
99
     */
100 2
    public function key(): int
101
    {
102 2
        $this->initInternArray();
103
104 2
        return $this->getInternArrayOffset();
105
    }
106
107
    /**
108
     * Method alias to offsetGet
109
     * @param mixed $index
110
     * @return mixed
111
     */
112 4
    public function item($index)
113
    {
114 4
        $this->initInternArray();
115
116 4
        return $this->offsetGet($index);
117
    }
118
119
    /**
120
     * Default method adding item to array
121
     * @param mixed $item value
122
     * @return AbstractStructArrayBase
123
     */
124 16
    public function add($item): self
125
    {
126
        // init array
127 16
        if (!is_array($this->getPropertyValue($this->getAttributeName()))) {
128 2
            $this->setPropertyValue($this->getAttributeName(), []);
129
        }
130
131
        // current array
132 16
        $currentArray = $this->getPropertyValue($this->getAttributeName());
133 16
        $currentArray[] = $item;
134 16
        $this
135 16
            ->setInternArray($currentArray)
136 16
            ->setInternArrayIsArray(true)
137 16
            ->setInternArrayOffset(0)
138 16
            ->setPropertyValue($this->getAttributeName(), $currentArray);
139
140 16
        return $this;
141
    }
142
143
    /**
144
     * Method returning the first item
145
     * @return mixed
146
     */
147 2
    public function first()
148
    {
149 2
        $this->initInternArray();
150
151 2
        return $this->item(0);
152
    }
153
154
    /**
155
     * Method returning the last item
156
     * @return mixed
157
     */
158 2
    public function last()
159
    {
160 2
        $this->initInternArray();
161
162 2
        return $this->item($this->length() - 1);
163
    }
164
165
    /**
166
     * Method testing index in item
167
     * @param mixed $offset
168
     * @return bool
169
     */
170 22
    public function offsetExists($offset): bool
171
    {
172 22
        $this->initInternArray();
173
174 22
        return ($this->getInternArrayIsArray() && array_key_exists($offset, $this->getInternArray()));
175
    }
176
177
    /**
178
     * Method returning the item at "index" value
179
     * @param mixed $offset
180
     * @return mixed
181
     */
182 8
    #[\ReturnTypeWillChange]
183 8
    public function offsetGet($offset)
184
    {
185 16
        $this->initInternArray();
186
187 16
        return $this->offsetExists($offset) ? $this->internArray[$offset] : null;
188
    }
189
190
    /**
191
     * Method setting value at offset
192
     * @param mixed $offset
193
     * @param mixed $value
194
     * @return AbstractStructArrayBase
195
     */
196 1
    #[\ReturnTypeWillChange]
197 1
    public function offsetSet($offset, $value): self
198
    {
199 2
        $this->initInternArray();
200
201 2
        $this->internArray[$offset] = $value;
202
203 2
        $this->setPropertyValue($this->getAttributeName(), $this->internArray);
204
205 2
        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::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...
206
    }
207
208
    /**
209
     * Method unsetting value at offset
210
     * @param mixed $offset
211
     * @return AbstractStructArrayBase
212
     */
213 2
    #[\ReturnTypeWillChange]
214 2
    public function offsetUnset($offset): self
215
    {
216 4
        $this->initInternArray();
217
218 4
        if ($this->offsetExists($offset)) {
219 4
            unset($this->internArray[$offset]);
220 4
            $this->setPropertyValue($this->getAttributeName(), $this->internArray);
221
        }
222
223 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...
224
    }
225
226
    /**
227
     * Method returning intern array to iterate trough
228
     * @return array
229
     */
230 24
    private function getInternArray(): array
231
    {
232 24
        return $this->internArray;
233
    }
234
235
    /**
236
     * Method setting intern array to iterate trough
237
     * @param array $internArray
238
     * @return AbstractStructArrayBase
239
     */
240 24
    private function setInternArray(array $internArray): self
241
    {
242 24
        $this->internArray = $internArray;
243
244 24
        return $this;
245
    }
246
247
    /**
248
     * Method returns intern array index when iterating trough
249
     * @return int
250
     */
251 2
    private function getInternArrayOffset(): int
252
    {
253 2
        return $this->internArrayOffset;
254
    }
255
256
    /**
257
     * Method initiating internArray
258
     * @param array $array the array to iterate trough
259
     * @param bool $internCall indicates that methods is calling itself
260
     * @return AbstractStructArrayBase
261
     */
262 24
    private function initInternArray($array = [], bool $internCall = false): self
263
    {
264 24
        if (is_array($array) && count($array) > 0) {
265 8
            $this
266 8
                ->setInternArray($array)
267 8
                ->setInternArrayOffset(0)
268 8
                ->setInternArrayIsArray(true);
269 24
        } elseif (!$this->internArrayIsArray && !$internCall && property_exists($this, $this->getAttributeName())) {
270 8
            $this->initInternArray($this->getPropertyValue($this->getAttributeName()), true);
271
        }
272
273 24
        return $this;
274
    }
275
276
    /**
277
     * Method setting intern array offset when iterating trough
278
     * @param int $internArrayOffset
279
     * @return AbstractStructArrayBase
280
     */
281 24
    private function setInternArrayOffset(int $internArrayOffset): self
282
    {
283 24
        $this->internArrayOffset = $internArrayOffset;
284
285 24
        return $this;
286
    }
287
288
    /**
289
     * Method returning true if intern array is an actual array
290
     * @return bool
291
     */
292 24
    private function getInternArrayIsArray(): bool
293
    {
294 24
        return $this->internArrayIsArray;
295
    }
296
297
    /**
298
     * Method setting if intern array is an actual array
299
     * @param bool $internArrayIsArray
300
     * @return AbstractStructArrayBase
301
     */
302 24
    private function setInternArrayIsArray(bool $internArrayIsArray = false): self
303
    {
304 24
        $this->internArrayIsArray = $internArrayIsArray;
305
306 24
        return $this;
307
    }
308
}
309