Passed
Push — 1.x ( 089220...c94754 )
by Ulises Jeremias
02:40
created

Set::keysAreEqual()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 3
nc 3
nop 2
dl 0
loc 7
rs 9.2
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 Traversable;
14
use ArrayAccess;
15
use IteratorAggregate;
16
use OutOfBoundsException;
17
use OutOfRangeException;
18
19
/**
20
 * A Map is a sequential collection of key-value pairs, almost identical to an
21
 * array used in a similar context. Keys can be any type, but must be unique.
22
 *
23
 * @package structures
24
 * @author Ulises Jeremias Cornejo Fandos <[email protected]>
25
 */
26
class Set implements ArrayAccess, CollectionInterface, IteratorAggregate
27
{
28
    use Traits\Collection;
29
    use Traits\Functional;
30
    use Traits\SquaredCapacity;
31
32
    /**
33
     * @var FixedArray internal array to store pairs
34
     */
35
    private $pairs;
36
37
    /**
38
     * Creates a new instance.
39
     *
40
     * @param array|Traversable $pairs
41
     */
42
    public function __construct($pairs = [])
43
    {
44
        FixedArray::fromArray([]);
45
46
        $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

46
        $this->/** @scrutinizer ignore-call */ 
47
               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...
47
    }
48
49
    /**
50
     * Determines whether two keys are equal.
51
     *
52
     * @param mixed $a
53
     * @param mixed $b
54
     *
55
     * @return bool
56
     */
57
    private function keysAreEqual($a, $b): bool
58
    {
59
        if (is_object($a) && $a instanceof HashableInterface) {
60
            return get_class($a) === get_class($b) && $a->equals($b);
61
        }
62
63
        return $a === $b;
64
    }
65
66
    /**
67
     * Attempts to look up a key in the table.
68
     *
69
     * @param $key
70
     *
71
     * @return Pair|null
72
     */
73
    private function lookupKey($key)
74
    {
75
        foreach ($this->pairs as $pair) {
76
            if ($this->keysAreEqual($pair->key, $key)) {
77
                return $pair;
78
            }
79
        }
80
    }
81
82
    /**
83
     * Attempts to look up a key in the table.
84
     *
85
     * @param $value
86
     *
87
     * @return Pair|null
88
     */
89
    private function lookupValue($value)
0 ignored issues
show
Unused Code introduced by
The method lookupValue() is not used, and could be removed.

This check looks for private methods that have been defined, but are not used inside the class.

Loading history...
90
    {
91
        foreach ($this->pairs as $pair) {
92
            if ($pair->value === $value) {
93
                return $pair;
94
            }
95
        }
96
    }
97
98
    /**
99
     * Associates a key with a value, replacing a previous association if there
100
     * was one.
101
     *
102
     * @param mixed $key
103
     * @param mixed $value
104
     */
105
    public function put($key, $value)
106
    {
107
        $pair = $this->lookupKey($key);
108
        if ($pair) {
109
            $pair->value = $value;
110
        } else {
111
            $this->pairs[] = new Pair($key, $value);
112
        }
113
    }
114
115
    /**
116
     * Creates associations for all keys and corresponding values of either an
117
     * array or iterable object.
118
     *
119
     * @param Traversable|array $values
120
     */
121
    public function putAll($values)
122
    {
123
        foreach ($values as $key => $value) {
124
            $this->put($key, $value);
125
        }
126
    }
127
}
128