Completed
Pull Request — master (#7)
by Volodymyr
02:27
created

ArrayList::at()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 9.4286
cc 1
eloc 4
nc 1
nop 1
crap 1
1
<?php
2
3
// Copyright (c) Lellys Informática. All rights reserved. See License.txt in the project root for license information.
4
namespace Collections;
5
6
use Collections\Iterator\VectorIterator;
7
use Collections\Traits\GuardTrait;
8
use Collections\Traits\StrictIterableTrait;
9
use InvalidArgumentException;
10
use Traversable;
11
12
/**
13
 * Represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort,
14
 * and manipulate lists.
15
 */
16
class ArrayList extends AbstractCollectionArray implements VectorInterface, \ArrayAccess
17
{
18
    use StrictIterableTrait,
19
        GuardTrait;
20
21
    /**
22
     * {@inheritdoc}
23
     */
24 1
    public function at($key)
25
    {
26 1
        $this->validateKeyType($key);
27 1
        $this->validateKeyBounds($key);
28
29 1
        return $this->container[$key];
30
    }
31
32
    /**
33
     * {@inheritdoc}
34
     */
35 17
    public function set($key, $value)
36
    {
37 17
        $this->validateKeyType($key);
38 16
        $this->container[$key] = $value;
39
40 16
        return $this;
41
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 1
    public function get($index)
47 1
    {
48 1
        $this->validateKeyType($index);
49
50 1
        return $this->container[$index];
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56 22
    public function add($item)
57
    {
58 22
        $this->container[] = $item;
59
60 22
        return $this;
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66 3
    public function addAll($items)
67
    {
68 3
        if (!is_array($items) && !$items instanceof Traversable) {
69 1
            throw new \InvalidArgumentException('Parameter must be an array or an instance of Traversable');
70
        }
71
72 2
        foreach ($items as $item) {
73 2
            if (is_array($item)) {
74
                $item = new static($item);
75
            }
76 2
            $this->add($item);
77 2
        }
78
79 2
        return $this;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 4
    public function containsKey($key)
86
    {
87 4
        $this->validateKeyType($key);
88
89 3
        return $key >= 0 && $key < $this->count();
90
    }
91
92
93
    /**
94
     * {@inheritdoc}
95
     */
96 3
    public function removeKey($key)
97
    {
98 3
        $this->validateKeyType($key);
99 2
        $this->validateKeyBounds($key);
100
101 1
        array_splice($this->container, $key, 1);
102
103 1
        return $this;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109 3
    public function indexOf($item)
110
    {
111 3
        return array_search($item, $this->container, true);
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function insert($index, $item)
118
    {
119
        if (!is_numeric($index)) {
120
            throw new InvalidArgumentException('The index must be numeric');
121
        }
122
        if ($index < 0 || $index >= $this->count()) {
123
            throw new InvalidArgumentException('The index is out of bounds (must be >=0 and <= size of te array)');
124
        }
125
126
        $current = $this->count() - 1;
127
        for (; $current >= $index; $current--) {
128
            $this->container[$current + 1] = $this->container[$current];
129
        }
130
        $this->container[$index] = $item;
131
132
        return $this;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138 2
    public function offsetExists($offset)
139
    {
140 2
        $this->validateKeyType($offset);
141
142 1
        return $this->containsKey($offset) && $this->at($offset) !== null;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 1
    public function offsetGet($offset)
149
    {
150 1
        return $this->get($offset);
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156 19
    public function offsetSet($offset, $value)
157
    {
158 19
        if (is_null($offset)) {
159 4
            $this->add($value);
160 4
        } else {
161 17
            $this->set($offset, $value);
162
        }
163 18
    }
164
165
    /**
166
     * {@inheritdoc}
167
     */
168 1
    public function offsetUnset($offset)
169
    {
170 1
        throw new \RuntimeException(
171 1
            'Cannot unset an element of a '.get_class($this)
172 1
        );
173
    }
174
175
    /**
176
     * {@inheritdoc}
177
     */
178 1
    public function toMap()
179
    {
180 1
        return new Dictionary($this->getIterator());
181
    }
182
183
    /**
184
     * {@inheritdoc}
185
     */
186
    public function reverse()
187
    {
188
        return static::fromArray(array_reverse($this->container));
189
    }
190
191
    /**
192
     * {@inheritdoc}
193
     */
194
    public function splice($offset, $length = null)
195
    {
196
        return static::fromArray(array_splice($this->container, $offset, $length));
197
    }
198
199
    /**
200
     * {@inheritdoc}
201
     * @return ArrayList;
0 ignored issues
show
Documentation introduced by
The doc-type ArrayList; could not be parsed: Expected "|" or "end of type", but got ";" at position 9. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
202
     */
203
    public static function fromArray(array $arr)
204
    {
205
        $map = new ArrayList();
206
        foreach ($arr as $v) {
207
            if (is_array($v)) {
208
                $map->add(new ArrayList($v));
209
            } else {
210
                $map->add($v);
211
            }
212
        }
213
214
        return $map;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $map; (Collections\ArrayList) is incompatible with the return type declared by the interface Collections\CollectionCo...bleInterface::fromArray of type Collections\MapInterface.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
215
    }
216
217
    /**
218
     * Gets the collection's iterator
219
     * @return VectorIterator
220
     */
221 18
    public function getIterator()
222
    {
223 18
        return new VectorIterator($this->container);
224
    }
225
}
226