Completed
Push — master ( 79f24a...4b5497 )
by Emily
02:16
created

HashMap::getHashProducer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
dl 0
loc 9
ccs 3
cts 4
cp 0.75
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2.0625
1
<?php
2
/**
3
 * This file is part of the Composite Utils package.
4
 *
5
 * (c) Emily Shepherd <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the
8
 * LICENSE.md file that was distributed with this source code.
9
 *
10
 * @package spaark/composite-utils
11
 * @author Emily Shepherd <[email protected]>
12
 * @license MIT
13
 */
14
15
namespace Spaark\CompositeUtils\Model\Collection\Map;
16
17
use Spaark\CompositeUtils\Service\HashProducer;
18
use Spaark\CompositeUtils\Service\HashProducerInterface;
19
20
/**
21
 * Represents a HashMap which contains mapings from one element to
22
 * another
23
 *
24
 * @generic KeyType
25
 * @generic ValueType
26
 */
27
class HashMap extends AbstractMap
28
{
29
    /**
30
     * @var HashProducerInterface
31
     */
32
    protected static $defaultHashProducer;
33
34 46
    protected static function getHashProducer() : HashProducerInterface
35
    {
36 46
        if (!static::$defaultHashProducer)
37
        {
38
            static::$defaultHashProducer = new HashProducer();
39
        }
40
41 46
        return static::$defaultHashProducer;
42
    }
43
44
    public static function setDefaultHashProducer
45
    (
46
        HashProducerInterface $hashProducer
47
    )
48
    : HashProducerInterface
49
    {
50
        static::$defaultHashProducer = $hashProducer;
51
    }
52
53
    /**
54
     * @var Pair<KeyType, ValueType>[]
55
     */
56
    protected $data = [];
57
58
    /**
59
     * @var HashProducerInterface
60
     */
61
    protected $hashProducer;
62
63 46
    public function __construct
64
    (
65
        ?HashProducerInterface $hashProducer = null
66
    )
67
    {
68 46
        $this->hashProducer =
69 46
            $hashProducer ?: static::getHashProducer();
70 46
    }
71
72
    /**
73
     * {@inheritDoc}
74
     */
75 15
    public function getIterator()
76
    {
77 15
        return new HashMapIterator($this->data);
78
    }
79
80
    /**
81
     * {@inheritDoc}
82
     */
83 46
    public function containsKey($key) : bool
84
    {
85 46
        return isset($this->data[$this->hashProducer->getHash($key)]);
86
    }
87
88
    /**
89
     * {@inheritDoc}
90
     */
91 50
    public function getPair($key) : Pair
92
    {
93 50
        return $this->data[$this->hashProducer->getHash($key)];
94
    }
95
96
    /**
97
     * {@inheritDoc}
98
     */
99 43
    public function insert(Pair $pair)
100
    {
101 43
        $this->data[$this->hashProducer->getHash($pair->key)] = $pair;
0 ignored issues
show
Documentation introduced by
The property $key is declared protected in Spaark\CompositeUtils\Model\Collection\Map\Pair. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
102 43
    }
103
104
    /**
105
     * {@inheritDoc}
106
     */
107 2
    public function remove($key)
108
    {
109 2
        unset($this->data[$this->hashProducer->getHash($key)]);
110 2
    }
111
112
    /**
113
     * {@inheritDoc}
114
     */
115 8
    public function size() : int
116
    {
117 8
        return count($this->data);
118
    }
119
}
120