This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php declare(strict_types=1); |
||
0 ignored issues
–
show
|
|||
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 | 42 | public function __construct(int $behavior = self::DEFAULT) |
|
0 ignored issues
–
show
|
|||
41 | { |
||
42 | 42 | $this->behavior = $behavior; |
|
43 | 42 | } |
|
44 | |||
45 | /** |
||
46 | * {@inheritdoc} |
||
47 | */ |
||
48 | 28 | public function put($key, $value) |
|
49 | { |
||
50 | 28 | $this->assertObject($key, 'Key'); |
|
51 | 27 | $this->assertObject($value, 'Value'); // while we may associate non-object value, for interface compatibility we don't do that |
|
52 | |||
53 | 26 | $hash = $this->getHash($key); |
|
54 | |||
55 | 26 | if (isset($this->keys[$hash])) { |
|
56 | 1 | throw new OverflowException('Value with such key already exists'); |
|
57 | } |
||
58 | |||
59 | 26 | $bucket = $this->createBucket($key, $value, $hash); |
|
60 | |||
61 | 26 | $this->keys[$hash] = $bucket; |
|
62 | 26 | } |
|
63 | |||
64 | /** |
||
65 | * {@inheritdoc} |
||
66 | */ |
||
67 | 5 | public function get($key) |
|
68 | { |
||
69 | 5 | $this->assertObject($key, 'Key'); |
|
70 | |||
71 | 4 | $hash = $this->getHash($key); |
|
72 | |||
73 | 4 | if (!isset($this->keys[$hash])) { |
|
74 | 2 | throw new OutOfBoundsException('Value with such key not found'); |
|
75 | } |
||
76 | |||
77 | 2 | return $this->keys[$hash]->value; |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * {@inheritdoc} |
||
82 | */ |
||
83 | 23 | public function has($key): bool |
|
84 | { |
||
85 | 23 | $this->assertObject($key, 'Key'); |
|
86 | |||
87 | 22 | $hash = $this->getHash($key); |
|
88 | |||
89 | 22 | return isset($this->keys[$hash]); |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | 7 | public function remove($key) |
|
96 | { |
||
97 | 7 | $this->assertObject($key, 'Key'); |
|
98 | |||
99 | 6 | $hash = $this->getHash($key); |
|
100 | |||
101 | 6 | if (!isset($this->keys[$hash])) { |
|
102 | 1 | throw new OutOfBoundsException('Value with such key not found'); |
|
103 | } |
||
104 | |||
105 | 5 | $bucket = $this->keys[$hash]; |
|
106 | |||
107 | 5 | $this->doRemove($hash); |
|
108 | |||
109 | 5 | return $bucket->value; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * {@inheritdoc} |
||
114 | */ |
||
115 | 16 | public function count() |
|
116 | { |
||
117 | 16 | return count($this->keys); |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * {@inheritdoc} |
||
122 | */ |
||
123 | 3 | public function clear() |
|
124 | { |
||
125 | 3 | $this->keys = []; |
|
126 | 3 | } |
|
127 | |||
128 | /** |
||
129 | * @param object $value |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | 33 | protected function getHash($value) |
|
134 | { |
||
135 | 33 | return spl_object_hash($value); |
|
136 | } |
||
137 | |||
138 | /** |
||
139 | * @param string $hash |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | 14 | protected function doRemove(string $hash) |
|
144 | { |
||
145 | 14 | unset($this->keys[$hash]); |
|
146 | 14 | } |
|
147 | |||
148 | /** |
||
149 | * @param object $key |
||
150 | * @param object $value |
||
151 | * @param string $hash |
||
152 | * |
||
153 | * @return Bucket |
||
154 | */ |
||
155 | 26 | protected function createBucket($key, $value, string $hash): Bucket |
|
156 | { |
||
157 | 26 | if ($this->behavior & self::WEAK_KEY) { |
|
158 | 8 | $key = $this->createReference($key, $hash); |
|
159 | } |
||
160 | |||
161 | 26 | if ($this->behavior & self::WEAK_VALUE) { |
|
162 | 8 | $value = $this->createReference($value, $hash); |
|
163 | } |
||
164 | |||
165 | 26 | return new Bucket($key, $value); |
|
166 | } |
||
167 | |||
168 | /** |
||
169 | * @param $obj |
||
170 | * @param string $hash |
||
171 | * |
||
172 | * @return WeakReference |
||
173 | */ |
||
174 | protected function createReference($obj, string $hash): WeakReference |
||
175 | { |
||
176 | 9 | return new WeakReference($obj, function () use ($hash) { |
|
177 | 9 | $this->doRemove($hash); |
|
178 | 9 | }); |
|
179 | } |
||
180 | } |
||
181 | |||
182 | |||
183 | |||
184 | |||
185 | |||
186 | |||
187 | |||
188 |
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.