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

ObjectBiMap::put()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3.009

Importance

Changes 0
Metric Value
dl 0
loc 17
c 0
b 0
f 0
ccs 9
cts 10
cp 0.9
rs 9.4285
cc 3
eloc 9
nc 3
nop 2
crap 3.009
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 22 and the first side effect is on line 165.

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 Pinepain\ObjectMaps\Exceptions\OutOfBoundsException;
19
use Pinepain\ObjectMaps\Exceptions\OverflowException;
20
21
22
class ObjectBiMap implements ObjectBiMapInterface
23
{
24
    use ObjectTypeHintTrait;
25
26
    protected $behavior = self::DEFAULT;
27
28
    /**
29
     * @var ObjectMap
30
     */
31
    protected $keys;
32
    /**
33
     * @var ObjectMap
34
     */
35
    protected $values;
36
37
    /**
38
     * @param int $behavior
39
     */
40 22
    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 22
        $key_behavior   = 0;
43 22
        $value_behavior = 0;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $value_behavior.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
44
45 22
        if ($behavior & self::WEAK_KEY) {
0 ignored issues
show
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...
Coding Style introduced by
The visibility should be declared for property $behavior.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
46 5
            $key_behavior   |= self::WEAK_KEY;
47 5
            $value_behavior |= self::WEAK_VALUE;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $value_behavior.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
48
        }
49
50 22
        if ($behavior & self::WEAK_VALUE) {
0 ignored issues
show
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...
Coding Style introduced by
The visibility should be declared for property $behavior.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
51 5
            $key_behavior   |= self::WEAK_VALUE;
52 5
            $value_behavior |= self::WEAK_KEY;
0 ignored issues
show
Coding Style introduced by
The visibility should be declared for property $value_behavior.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
53
        }
54
55 22
        $this->keys   = new ObjectMap($key_behavior);
0 ignored issues
show
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...
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
56 22
        $this->values = new ObjectMap($value_behavior);
0 ignored issues
show
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...
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
57
58 22
        $this->behavior = $behavior;
0 ignored issues
show
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...
Coding Style introduced by
The visibility should be declared for property $this.

The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using

class A {
    var $property;
}

the property is implicitly global.

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

Loading history...
59 22
    }
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 4
    public function values(): ObjectBiMapInterface
65
    {
66 4
        $new_behavior = 0;
67
68 4
        if ($this->behavior & self::WEAK_KEY) {
69 2
            $new_behavior |= self::WEAK_VALUE;
70
        }
71
72 4
        if ($this->behavior & self::WEAK_VALUE) {
73 2
            $new_behavior |= self::WEAK_KEY;
74
        }
75
76 4
        $new = new static($new_behavior);
77
78 4
        $new->keys   = $this->values;
79 4
        $new->values = $this->keys;
80
81 4
        return $new;
82
    }
83
84
    /**
85
     * {@inheritdoc}
86
     */
87 16
    public function put($key, $value)
88
    {
89 16
        $this->assertObject($key, 'Key');
90
91 15
        if ($this->keys->has($key)) {
92 1
            throw new OverflowException('Value with such key already exists');
93
        }
94
95 15
        $this->assertObject($value, 'Value');
96
97 14
        if ($this->values->has($value)) {
98
            throw new OverflowException('Key with such value already exists');
99
        }
100
101 14
        $this->keys->put($key, $value);
102 14
        $this->values->put($value, $key);
103 14
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 3
    public function get($key)
109
    {
110 3
        $this->assertObject($key, 'Key');
111
112 2
        return $this->keys->get($key);
113
    }
114
115
    /**
116
     * {@inheritdoc}
117
     */
118
119 6
    public function has($key): bool
120
    {
121 6
        $this->assertObject($key, 'Key');
122
123 5
        return $this->keys->has($key);
124
    }
125
126
    /**
127
     * {@inheritdoc}
128
     */
129 4
    public function remove($key)
130
    {
131 4
        $this->assertObject($key, 'Key');
132
133 3
        if (!$this->keys->has($key)) {
134 1
            throw new OutOfBoundsException('Value with such key not found');
135
        }
136
137 2
        $value = $this->keys->remove($key);
138
139 2
        if (!$this->values->has($value)) {
140
            // UNEXPECTED
141
            throw new OutOfBoundsException('Key with such value not found');
142
        }
143
144 2
        $this->values->remove($value);
145
146 2
        return $value;
147
    }
148
149
    /**
150
     * {@inheritdoc}
151
     */
152 10
    public function count()
153
    {
154 10
        return count($this->keys);
155
    }
156
157
    /**
158
     * {@inheritdoc}
159
     */
160 2
    public function clear()
161
    {
162 2
        $this->keys->clear();
163 2
        $this->values->clear();
164 2
    }
165
}
166