Completed
Push — refactor ( 4e5faf...5ad188 )
by Bogdan
02:12
created

ObjectMap::clear()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php declare(strict_types=1);
2
3
/*
4
 * This file is part of the pinepain/php-object-maps PHP library.
5
 *
6
 * Copyright (c) 2016-2017 Bogdan Padalko <[email protected]>
7
 *
8
 * Licensed under the MIT license: http://opensource.org/licenses/MIT
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code or visit http://opensource.org/licenses/MIT
12
 */
13
14
15
namespace Pinepain\ObjectMaps;
16
17
18
use OutOfBoundsException;
19
use OverflowException;
20
use Ref\WeakReference;
21
22
23
class ObjectMap implements ObjectMapInterface
24
{
25
    const WEAK_KEY = 1 << 0;
26
    const WEAK_VALUE = 1 << 1;
27
    const WEAK_KEY_VALUE = self::WEAK_KEY | self::WEAK_VALUE;
28
29
    protected $behavior = 0;
30
31
    /**
32
     * @var Bucket[]
33
     */
34
    protected $keys = [];
35
36
    public function __construct(int $behavior = 0)
37
    {
38
        $this->behavior = $behavior;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function put($key, $value)
45
    {
46
        $hash = $this->getHash($key);
47
48
        if (isset($this->keys[$hash])) {
49
            throw new OverflowException('Value with such key already exists');
50
        }
51
52
        $bucket = $this->createBucket($key, $value, $hash);
53
54
        $this->keys[$hash] = $bucket;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function get($key)
61
    {
62
        $hash = $this->getHash($key);
63
64
        if (!isset($this->keys[$hash])) {
65
            throw new OutOfBoundsException('Value with such key not found');
66
        }
67
68
        return $this->keys[$hash]->value;
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function has($key): bool
75
    {
76
        $hash = $this->getHash($key);
77
78
        return isset($this->keys[$hash]);
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function remove($key)
85
    {
86
        $hash = $this->getHash($key);
87
88
        if (!isset($this->keys[$hash])) {
89
            throw new OutOfBoundsException('Value with such key not found');
90
        }
91
92
        $bucket = $this->keys[$hash];
93
94
        $this->doRemove($hash);
95
96
        return $bucket->value;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function count()
103
    {
104
        return count($this->keys);
105
    }
106
107
    /**
108
     * @return void
109
     */
110
    public function clear()
111
    {
112
        $this->keys = [];
113
    }
114
115
    /**
116
     * @param $value
117
     *
118
     * @return string
119
     */
120
    protected function getHash($value)
121
    {
122
        return spl_object_hash($value);
123
    }
124
125
    /**
126
     * @param string $hash
127
     *
128
     * @return void
129
     */
130
    protected function doRemove(string $hash)
131
    {
132
        unset($this->keys[$hash]);
133
    }
134
135
    /**
136
     * @param object $key
137
     * @param object $value
138
     * @param string $hash
139
     *
140
     * @return Bucket
141
     */
142
    protected function createBucket($key, $value, string $hash)
143
    {
144 View Code Duplication
        if ($this->behavior & self::WEAK_KEY) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
145
            $key = new WeakReference($key, function () use ($hash) {
146
                $this->doRemove($hash);
147
            });
148
        }
149
150 View Code Duplication
        if ($this->behavior & self::WEAK_VALUE) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
151
            $value = new WeakReference($value, function () use ($hash) {
152
                $this->doRemove($hash);
153
            });
154
        }
155
156
        return new Bucket($key, $value);
157
    }
158
}
159
160
161
162
163
164
165
166