Passed
Push — develop ( 1a5237...8ee2a6 )
by Mikaël
01:27
created

AbstractStructArrayBase::length()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
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
    protected array $internArray = [];
14
15
    /**
16
     * Bool that tells if array is set or not
17
     * @var bool
18
     */
19
    protected bool $internArrayIsArray = false;
20
21
    /**
22
     * Items index browser
23
     * @var int
24
     */
25
    protected int $internArrayOffset = 0;
26
27
    /**
28
     * Method alias to count
29
     * @return int
30
     */
31 4
    public function length(): int
32
    {
33 4
        return $this->count();
34
    }
35
36
    /**
37
     * Method returning item length, alias to length
38
     * @return int
39
     */
40 6
    public function count(): int
41
    {
42 6
        return $this->getInternArrayIsArray() ? count($this->getInternArray()) : -1;
43
    }
44
45
    /**
46
     * Method returning the current element
47
     * @return mixed
48
     */
49 6
    public function current()
50
    {
51 6
        return $this->offsetGet($this->internArrayOffset);
52
    }
53
54
    /**
55
     * Method moving the current position to the next element
56
     * @return AbstractStructArrayBase
57
     */
58 10
    public function next(): self
59
    {
60 10
        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...
61
    }
62
63
    /**
64
     * Method resetting itemOffset
65
     * @return AbstractStructArrayBase
66
     */
67 2
    public function rewind(): self
68
    {
69 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...
70
    }
71
72
    /**
73
     * Method checking if current itemOffset points to an existing item
74
     * @return bool
75
     */
76 4
    public function valid(): bool
77
    {
78 4
        return $this->offsetExists($this->getInternArrayOffset());
79
    }
80
81
    /**
82
     * Method returning current itemOffset value, alias to getInternArrayOffset
83
     * @return int
84
     */
85 4
    public function key(): int
86
    {
87 4
        return $this->getInternArrayOffset();
88
    }
89
90
    /**
91
     * Method alias to offsetGet
92
     * @param mixed $index
93
     * @return mixed
94
     */
95 6
    public function item($index)
96
    {
97 6
        return $this->offsetGet($index);
98
    }
99
100
    /**
101
     * Default method adding item to array
102
     * @param mixed $item value
103
     * @return AbstractStructArrayBase
104
     */
105 16
    public function add($item): self
106
    {
107
        // init array
108 16
        if (!is_array($this->getPropertyValue($this->getAttributeName()))) {
109 2
            $this->setPropertyValue($this->getAttributeName(), []);
110
        }
111
112
        // current array
113 16
        $currentArray = $this->getPropertyValue($this->getAttributeName());
114 16
        $currentArray[] = $item;
115
        $this
116 16
            ->setPropertyValue($this->getAttributeName(), $currentArray)
117 16
            ->setInternArray($currentArray)
118 16
            ->setInternArrayIsArray(true)
119 16
            ->setInternArrayOffset(0);
120
121 16
        return $this;
122
    }
123
124
    /**
125
     * Method returning the first item
126
     * @return mixed
127
     */
128 2
    public function first()
129
    {
130 2
        return $this->item(0);
131
    }
132
133
    /**
134
     * Method returning the last item
135
     * @return mixed
136
     */
137 2
    public function last()
138
    {
139 2
        return $this->item($this->length() - 1);
140
    }
141
142
    /**
143
     * Method testing index in item
144
     * @param mixed $offset
145
     * @return bool
146
     */
147 28
    public function offsetExists($offset): bool
148
    {
149 28
        return ($this->getInternArrayIsArray() && array_key_exists($offset, $this->getInternArray()));
150
    }
151
152
    /**
153
     * Method returning the item at "index" value
154
     * @param mixed $offset
155
     * @return mixed
156
     */
157 18
    public function offsetGet($offset)
158
    {
159 18
        return $this->offsetExists($offset) ? $this->internArray[$offset] : null;
160
    }
161
162
    /**
163
     * Method setting value at offset
164
     * @param mixed $offset
165
     * @param mixed $value
166
     * @return AbstractStructArrayBase
167
     */
168 2
    public function offsetSet($offset, $value): self
169
    {
170 2
        $this->internArray[$offset] = $value;
171
172 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...
173
    }
174
175
    /**
176
     * Method unsetting value at offset
177
     * @param mixed $offset
178
     * @return AbstractStructArrayBase
179
     */
180 2
    public function offsetUnset($offset): self
181
    {
182 2
        if ($this->offsetExists($offset)) {
183 2
            unset($this->internArray[$offset]);
184 2
            $this->setPropertyValue($this->getAttributeName(), $this->internArray);
185
        }
186
187 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::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...
188
    }
189
190
    /**
191
     * Method returning intern array to iterate trough
192
     * @return array
193
     */
194 32
    public function getInternArray(): array
195
    {
196 32
        return $this->internArray;
197
    }
198
199
    /**
200
     * Method setting intern array to iterate trough
201
     * @param array $internArray
202
     * @return AbstractStructArrayBase
203
     */
204 36
    public function setInternArray(array $internArray): self
205
    {
206 36
        $this->internArray = $internArray;
207
208 36
        return $this;
209
    }
210
211
    /**
212
     * Method returns intern array index when iterating trough
213
     * @return int
214
     */
215 10
    public function getInternArrayOffset(): int
216
    {
217 10
        return $this->internArrayOffset;
218
    }
219
220
    /**
221
     * Method initiating internArray
222
     * @param array $array the array to iterate trough
223
     * @param bool $internCall indicates that methods is calling itself
224
     * @return AbstractStructArrayBase
225
     */
226 34
    public function initInternArray(array $array = [], bool $internCall = false): self
227
    {
228 34
        if (is_array($array) && count($array) > 0) {
229
            $this
230 34
                ->setInternArray($array)
231 34
                ->setInternArrayOffset(0)
232 34
                ->setInternArrayIsArray(true);
233 20
        } elseif (!$internCall && property_exists($this, $this->getAttributeName())) {
234 20
            $this->initInternArray($this->getPropertyValue($this->getAttributeName()), true);
235
        }
236
237 34
        return $this;
238
    }
239
240
    /**
241
     * Method setting intern array offset when iterating trough
242
     * @param int $internArrayOffset
243
     * @return AbstractStructArrayBase
244
     */
245 36
    public function setInternArrayOffset(int $internArrayOffset): self
246
    {
247 36
        $this->internArrayOffset = $internArrayOffset;
248
249 36
        return $this;
250
    }
251
252
    /**
253
     * Method returning true if intern array is an actual array
254
     * @return bool
255
     */
256 32
    public function getInternArrayIsArray(): bool
257
    {
258 32
        return $this->internArrayIsArray;
259
    }
260
261
    /**
262
     * Method setting if intern array is an actual array
263
     * @param bool $internArrayIsArray
264
     * @return AbstractStructArrayBase
265
     */
266 36
    public function setInternArrayIsArray(bool $internArrayIsArray = false): self
267
    {
268 36
        $this->internArrayIsArray = $internArrayIsArray;
269
270 36
        return $this;
271
    }
272
}
273