Issues (19)

Security Analysis    no request data  

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/ObjectMap.php (3 issues)

Upgrade to new PHP Analysis Engine

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
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 180.

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 42
    public function __construct(int $behavior = self::DEFAULT)
0 ignored issues
show
Possible parse error: non-abstract method defined as abstract
Loading history...
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 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