Completed
Push — refactor ( a403e4...c06c99 )
by Bogdan
02:18
created

ObjectMap::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php declare(strict_types=1);
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 26 and the first side effect is on line 173.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
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 Ref\WeakReference;
19
20
use Pinepain\ObjectMaps\Exceptions\OutOfBoundsException;
21
use Pinepain\ObjectMaps\Exceptions\OverflowException;
22
23
use function spl_object_hash;
24
25
26
class ObjectMap implements ObjectMapInterface
27
{
28
    use ObjectTypeHintTrait;
29
30
    protected $behavior = self::DEFAULT;
31
32
    /**
33
     * @var Bucket[]
34
     */
35
    protected $keys = [];
36
37
    /**
38
     * @param int $behavior
39
     */
40 40
    public function __construct(int $behavior = self::DEFAULT)
0 ignored issues
show
Coding Style introduced by
Possible parse error: non-abstract method defined as abstract
Loading history...
Coding Style introduced by
It is generally advisable to only define one property per statement.

Only declaring a single property per statement allows you to later on add doc comments more easily.

It is also recommended by PSR2, so it is a common style that many people expect.

Loading history...
41
    {
42 40
        $this->behavior = $behavior;
43 40
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48 26
    public function put($key, $value)
49
    {
50 26
        $this->assertObject($key, 'Key');
51 25
        $this->assertObject($value, 'Value'); // while we may associate non-object value, for interface compatibility we don't do that
52
53 24
        $hash = $this->getHash($key);
54
55 24
        echo $hash, PHP_EOL;
56
57 24
        if (isset($this->keys[$hash])) {
58 1
            throw new OverflowException('Value with such key already exists');
59
        }
60
61 24
        $bucket = $this->createBucket($key, $value, $hash);
62
63 24
        $this->keys[$hash] = $bucket;
64 24
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 5
    public function get($key)
70
    {
71 5
        $this->assertObject($key, 'Key');
72
73 4
        $hash = $this->getHash($key);
74
75 4
        if (!isset($this->keys[$hash])) {
76 2
            throw new OutOfBoundsException('Value with such key not found');
77
        }
78
79 2
        return $this->keys[$hash]->value;
80
    }
81
82
    /**
83
     * {@inheritdoc}
84
     */
85 21
    public function has($key): bool
86
    {
87 21
        $this->assertObject($key, 'Key');
88
89 20
        $hash = $this->getHash($key);
90
91 20
        return isset($this->keys[$hash]);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97 6
    public function remove($key)
98
    {
99 6
        $this->assertObject($key, 'Key');
100
101 5
        $hash = $this->getHash($key);
102
103 5
        if (!isset($this->keys[$hash])) {
104 1
            throw new OutOfBoundsException('Value with such key not found');
105
        }
106
107 4
        $bucket = $this->keys[$hash];
108
109 4
        $this->doRemove($hash);
110
111 4
        return $bucket->value;
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117 16
    public function count()
118
    {
119 16
        return count($this->keys);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125 3
    public function clear()
126
    {
127 3
        $this->keys = [];
128 3
    }
129
130
    /**
131
     * @param object $value
132
     *
133
     * @return string
134
     */
135 31
    protected function getHash($value)
136
    {
137 31
        return spl_object_hash($value);
138
    }
139
140
    /**
141
     * @param string $hash
142
     *
143
     * @return void
144
     */
145 13
    protected function doRemove(string $hash)
146
    {
147 13
        unset($this->keys[$hash]);
148 13
    }
149
150
    /**
151
     * @param object $key
152
     * @param object $value
153
     * @param string $hash
154
     *
155
     * @return Bucket
156
     */
157 24
    protected function createBucket($key, $value, string $hash)
158
    {
159 24 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...
160 8
            $key = new WeakReference($key, function () use ($hash) {
161 8
                $this->doRemove($hash);
162 8
            });
163
        }
164
165 24 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...
166 8
            $value = new WeakReference($value, function () use ($hash) {
167 8
                $this->doRemove($hash);
168 8
            });
169
        }
170
171
        return new Bucket($key, $value);
172
    }
173
}
174
175
176
177
178
179
180
181