Passed
Push — 1.x ( c94754...89dbdb )
by Ulises Jeremias
02:31
created

Set::keys()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php namespace Mbh\Collection;
2
3
/**
4
 * MBHFramework
5
 *
6
 * @link      https://github.com/MBHFramework/mbh-framework
7
 * @copyright Copyright (c) 2017 Ulises Jeremias Cornejo Fandos
8
 * @license   https://github.com/MBHFramework/mbh-framework/blob/master/LICENSE (MIT License)
9
 */
10
11
use Mbh\Collection\Interfaces\Collection as CollectionInterface;
12
use Mbh\Collection\Interfaces\Hashable as HashableInterface;
13
use Mbh\Collection\Interfaces\Sequenceable as SequenceableInterface;
14
use Traversable;
15
use ArrayAccess;
16
use IteratorAggregate;
17
use OutOfBoundsException;
18
use OutOfRangeException;
19
20
/**
21
 * A Map is a sequential collection of key-value pairs, almost identical to an
22
 * array used in a similar context. Keys can be any type, but must be unique.
23
 *
24
 * @package structures
25
 * @author Ulises Jeremias Cornejo Fandos <[email protected]>
26
 */
27
class Set implements ArrayAccess, CollectionInterface, IteratorAggregate
28
{
29
    use Traits\Collection;
30
    use Traits\Functional;
31
    use Traits\SquaredCapacity;
32
33
    const MIN_CAPACITY = 8.0;
34
35
    /**
36
     * @var FixedArray internal array to store pairs
37
     */
38
    private $pairs;
39
40
    /**
41
     * Creates a new instance.
42
     *
43
     * @param array|Traversable $pairs
44
     */
45
    public function __construct($pairs = [])
46
    {
47
        FixedArray::fromArray([]);
48
49
        $this->pushAll($pairs);
0 ignored issues
show
Bug introduced by
The method pushAll() does not exist on Mbh\Collection\Set. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

49
        $this->/** @scrutinizer ignore-call */ 
50
               pushAll($pairs);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
50
    }
51
52
    /**
53
     * @inheritDoc
54
     */
55
    public function clear()
56
    {
57
        $this->pairs->clear();
58
        $this->capacity = self::MIN_CAPACITY;
59
    }
60
61
    /**
62
     * @inheritDoc
63
     */
64
    public function count(): int
65
    {
66
        return count($this->pairs);
67
    }
68
69
    /**
70
     * Returns whether an association a given key exists.
71
     *
72
     * @param mixed $key
73
     *
74
     * @return bool
75
     */
76
    public function hasKey($key): bool
77
    {
78
        return $this->lookupKey($key) !== null;
79
    }
80
81
    /**
82
     * Returns whether an association for a given value exists.
83
     *
84
     * @param mixed $value
85
     *
86
     * @return bool
87
     */
88
    public function hasValue($value): bool
89
    {
90
        return $this->lookupValue($value) !== null;
91
    }
92
93
    /**
94
     * Returns a set of all the keys in the map.
95
     *
96
     * @return Set
97
     */
98
    public function keys(): Set
99
    {
100
        return new static($this->pairs->map(function ($pair) {
101
            return $pair->key;
102
        }));
103
    }
104
105
    /**
106
     * Determines whether two keys are equal.
107
     *
108
     * @param mixed $a
109
     * @param mixed $b
110
     *
111
     * @return bool
112
     */
113
    private function keysAreEqual($a, $b): bool
114
    {
115
        if (is_object($a) && $a instanceof HashableInterface) {
116
            return get_class($a) === get_class($b) && $a->equals($b);
117
        }
118
119
        return $a === $b;
120
    }
121
122
    /**
123
     * Attempts to look up a key in the table.
124
     *
125
     * @param $key
126
     *
127
     * @return Pair|null
128
     */
129
    private function lookupKey($key)
130
    {
131
        foreach ($this->pairs as $pair) {
132
            if ($this->keysAreEqual($pair->key, $key)) {
133
                return $pair;
134
            }
135
        }
136
    }
137
138
    /**
139
     * Attempts to look up a key in the table.
140
     *
141
     * @param $value
142
     *
143
     * @return Pair|null
144
     */
145
    private function lookupValue($value)
146
    {
147
        foreach ($this->pairs as $pair) {
148
            if ($pair->value === $value) {
149
                return $pair;
150
            }
151
        }
152
    }
153
154
    /**
155
     * Associates a key with a value, replacing a previous association if there
156
     * was one.
157
     *
158
     * @param mixed $key
159
     * @param mixed $value
160
     */
161
    public function put($key, $value)
162
    {
163
        $pair = $this->lookupKey($key);
164
        if ($pair) {
165
            $pair->value = $value;
166
        } else {
167
            $this->checkCapacity();
168
            $this->pairs[] = new Pair($key, $value);
169
        }
170
    }
171
172
    /**
173
     * Creates associations for all keys and corresponding values of either an
174
     * array or iterable object.
175
     *
176
     * @param Traversable|array $values
177
     */
178
    public function putAll($values)
179
    {
180
        foreach ($values as $key => $value) {
181
            $this->put($key, $value);
182
        }
183
    }
184
185
    /**
186
     * Returns a sequence of all the associated values in the Map.
187
     *
188
     * @return SequenceInterface
189
     */
190
    public function values(): SequenceInterface
0 ignored issues
show
Bug introduced by
The type Mbh\Collection\SequenceInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
191
    {
192
        return $this->pairs->map(function ($pair) {
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->pairs->map...ion(...) { /* ... */ }) returns the type Mbh\Collection\FixedArray which is incompatible with the type-hinted return Mbh\Collection\SequenceInterface.
Loading history...
193
            return $pair->key;
194
        });
195
    }
196
}
197