Passed
Push — develop ( 0a103b...2b4aaa )
by Mikaël
43:22 queued 42:04
created

AbstractStructArrayBase::getInternArrayOffset()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
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
        @trigger_error(sprintf('%s() will be private in WsdlToPhp/PackageBase 5.0.', __METHOD__), E_USER_DEPRECATED);
225
226 24
        return $this->internArray;
227
    }
228
229
    /**
230
     * Method setting intern array to iterate trough
231
     * @param array $internArray
232
     * @return AbstractStructArrayBase
233
     */
234 24
    private function setInternArray(array $internArray): self
235
    {
236 24
        @trigger_error(sprintf('%s() will be private in WsdlToPhp/PackageBase 5.0.', __METHOD__), E_USER_DEPRECATED);
237
238 24
        $this->internArray = $internArray;
239
240 24
        return $this;
241
    }
242
243
    /**
244
     * Method returns intern array index when iterating trough
245
     * @return int
246
     */
247 2
    private function getInternArrayOffset(): int
248
    {
249 2
        @trigger_error(sprintf('%s() will be private in WsdlToPhp/PackageBase 5.0.', __METHOD__), E_USER_DEPRECATED);
250
251 2
        return $this->internArrayOffset;
252
    }
253
254
    /**
255
     * Method initiating internArray
256
     * @param array $array the array to iterate trough
257
     * @param bool $internCall indicates that methods is calling itself
258
     * @return AbstractStructArrayBase
259
     */
260 24
    private function initInternArray(array $array = [], bool $internCall = false): self
261
    {
262 24
        @trigger_error(sprintf('%s() will be private in WsdlToPhp/PackageBase 5.0.', __METHOD__), E_USER_DEPRECATED);
263
264 24
        if (is_array($array) && count($array) > 0) {
265
            $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
        @trigger_error(sprintf('%s() will be private in WsdlToPhp/PackageBase 5.0.', __METHOD__), E_USER_DEPRECATED);
284
285 24
        $this->internArrayOffset = $internArrayOffset;
286
287 24
        return $this;
288
    }
289
290
    /**
291
     * Method returning true if intern array is an actual array
292
     * @return bool
293
     */
294 24
    private function getInternArrayIsArray(): bool
295
    {
296 24
        @trigger_error(sprintf('%s() will be private in WsdlToPhp/PackageBase 5.0.', __METHOD__), E_USER_DEPRECATED);
297
298 24
        return $this->internArrayIsArray;
299
    }
300
301
    /**
302
     * Method setting if intern array is an actual array
303
     * @param bool $internArrayIsArray
304
     * @return AbstractStructArrayBase
305
     */
306 24
    private function setInternArrayIsArray(bool $internArrayIsArray = false): self
307
    {
308 24
        @trigger_error(sprintf('%s() will be private in WsdlToPhp/PackageBase 5.0.', __METHOD__), E_USER_DEPRECATED);
309
310 24
        $this->internArrayIsArray = $internArrayIsArray;
311
312 24
        return $this;
313
    }
314
}
315