1 | <?php declare(strict_types=1); |
||
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) |
|
60 | |||
61 | /** |
||
62 | * {@inheritdoc} |
||
63 | */ |
||
64 | 4 | public function values(): ObjectBiMapInterface |
|
83 | |||
84 | /** |
||
85 | * {@inheritdoc} |
||
86 | */ |
||
87 | 17 | public function put($key, $value) |
|
88 | { |
||
89 | 17 | $this->assertObject($key, 'Key'); |
|
90 | |||
91 | 16 | if ($this->keys->has($key)) { |
|
92 | 1 | throw new OverflowException('Value with such key already exists'); |
|
93 | } |
||
94 | |||
95 | 16 | $this->assertObject($value, 'Value'); |
|
96 | |||
97 | 15 | if ($this->values->has($value)) { |
|
98 | // UNEXPECTED |
||
99 | 1 | throw new OverflowException('Key with such value already exists'); |
|
100 | } |
||
101 | |||
102 | 14 | $this->keys->put($key, $value); |
|
103 | 14 | $this->values->put($value, $key); |
|
104 | 14 | } |
|
105 | |||
106 | /** |
||
107 | * {@inheritdoc} |
||
108 | */ |
||
109 | 3 | public function get($key) |
|
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | |||
120 | 6 | public function has($key): bool |
|
126 | |||
127 | /** |
||
128 | * {@inheritdoc} |
||
129 | */ |
||
130 | 5 | public function remove($key) |
|
131 | { |
||
132 | 5 | $this->assertObject($key, 'Key'); |
|
133 | |||
134 | 4 | if (!$this->keys->has($key)) { |
|
135 | 1 | throw new OutOfBoundsException('Value with such key not found'); |
|
136 | } |
||
137 | |||
138 | 3 | $value = $this->keys->remove($key); |
|
139 | |||
140 | 3 | if (!$this->values->has($value)) { |
|
141 | // UNEXPECTED |
||
142 | 1 | throw new OutOfBoundsException('Key with such value not found'); |
|
143 | } |
||
144 | |||
145 | 2 | $this->values->remove($value); |
|
146 | |||
147 | 2 | return $value; |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * {@inheritdoc} |
||
152 | */ |
||
153 | 10 | public function count() |
|
157 | |||
158 | /** |
||
159 | * {@inheritdoc} |
||
160 | */ |
||
161 | 2 | public function clear() |
|
166 | } |
||
167 |
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.