Issues (9)

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/Weew/Container/Registry.php (1 issue)

Labels
Severity

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
2
3
namespace Weew\Container;
4
5
use Weew\Container\Definitions\AliasDefinition;
6
use Weew\Container\Definitions\ClassDefinition;
7
use Weew\Container\Definitions\InterfaceDefinition;
8
use Weew\Container\Definitions\ValueDefinition;
9
use Weew\Container\Definitions\WildcardDefinition;
10
use Weew\Container\Exceptions\MissingDefinitionIdentifierException;
11
use Weew\Container\Exceptions\MissingDefinitionValueException;
12
13
class Registry {
14
    /**
15
     * @var IDefinition[]
16
     */
17
    protected $definitions = [];
18
19
    /**
20
     * @param $id
21
     * @param null $value
22
     *
23
     * @return IDefinition
24
     */
25
    public function createDefinition($id, $value = null) {
26
        list($id, $value) = $this->getIdAndValueFromCreateDefinitionArgs(
27
            func_get_args(), $id, $value
28
        );
29
30
        $definition = $this->delegateDefinitionCreation($id, $value);
31
        $this->addDefinition($definition);
0 ignored issues
show
It seems like $definition defined by $this->delegateDefinitionCreation($id, $value) on line 30 can be null; however, Weew\Container\Registry::addDefinition() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
32
33
        return $definition;
34
    }
35
36
    /**
37
     * @param array $args
38
     * @param $id
39
     * @param $value
40
     *
41
     * @return array
42
     */
43
    protected function getIdAndValueFromCreateDefinitionArgs(array $args, $id, $value) {
44
        if (count($args) > 2) {
45
            array_shift($args);
46
            $value = $args;
47
        }
48
49
        if ( ! is_array($id) && $value === null) {
50
            $value = $id;
51
52
            if (is_object($id)) {
53
                $id = get_class($id);
54
            }
55
        }
56
57
        return [$id, $value];
58
    }
59
60
    /**
61
     * @param $id
62
     * @param $value
63
     *
64
     * @return IDefinition
65
     * @throws MissingDefinitionIdentifierException
66
     * @throws MissingDefinitionValueException
67
     */
68
    protected function delegateDefinitionCreation($id, $value) {
69
        if (is_array($id)) {
70
            $definition = $this->createDefinitionWithAliases($id, $value);
71
        } else if ($this->isRegexPattern($id)) {
72
            $definition = new WildcardDefinition($id, $value);
73
        } else if (class_exists($id)) {
74
            $definition = new ClassDefinition($id, $value);
75
        } else if (interface_exists($id)) {
76
            $definition = new InterfaceDefinition($id, $value);
77
        } else {
78
            $definition = new ValueDefinition($id, $value);
79
        }
80
81
        return $definition;
82
    }
83
84
    /**
85
     * @param $id
86
     *
87
     * @return bool
88
     */
89
    public function hasDefinition($id) {
90
        return $this->getDefinition($id) !== null;
91
    }
92
93
    /**
94
     * @param $id
95
     */
96
    public function removeDefinition($id) {
97
        $index = $this->getDefinitionIndex($id);
98
99
        if ($index !== null) {
100
            $definition = $this->definitions[$index];
101
102
            if ($definition instanceof IDefinition &&
103
                ! $definition instanceof WildcardDefinition) {
104
                array_remove($this->definitions, $index);
105
            }
106
        }
107
    }
108
109
    /**
110
     * @param array $ids
111
     * @param $value
112
     *
113
     * @return IDefinition
114
     * @throws MissingDefinitionIdentifierException
115
     * @throws MissingDefinitionValueException
116
     */
117
    protected function createDefinitionWithAliases(array $ids, $value) {
118
        if ($value == null) {
119
            throw new MissingDefinitionValueException(
120
                s('Trying to register a class with alias without a value. Received %s.', json_encode($ids))
121
            );
122
        } else if (count($ids) == 0) {
123
            throw new MissingDefinitionIdentifierException(
124
                'Trying to create a definition without an identifier.'
125
            );
126
        }
127
128
        $definition = null;
129
130
        foreach ($ids as $id) {
131
            if ( ! $definition instanceof IDefinition) {
132
                $definition = $this->createDefinition($id, $value);
133
            } else {
134
                $alias = $this->createAliasDefinition($definition, $id);
135
                $definition->addAlias($alias);
136
                $this->addDefinition($alias);
137
            }
138
        }
139
140
        return $definition;
141
    }
142
143
    /**
144
     * @param IDefinition $definition
145
     * @param $id
146
     *
147
     * @return AliasDefinition
148
     */
149
    protected function createAliasDefinition(IDefinition $definition, $id) {
150
        return new AliasDefinition($id, $definition);
151
    }
152
153
    /**
154
     * @param $id
155
     *
156
     * @return IDefinition
157
     */
158
    public function getDefinition($id) {
159
        foreach ($this->definitions as $definition) {
160
            if ($definition instanceof WildcardDefinition) {
161
                if ($this->matchRegexPattern($id, $definition->getId())) {
162
                    return $definition;
163
                }
164
            } else if ($definition->getId() == $id) {
165
                if ($definition instanceof AliasDefinition) {
166
                    return $definition->getValue();
167
                }
168
169
                return $definition;
170
            }
171
        }
172
173
        return null;
174
    }
175
176
    /**
177
     * @param $id
178
     *
179
     * @return int|null|string
180
     */
181
    public function getDefinitionIndex($id) {
182
        $definition = $this->getDefinition($id);
183
184
        foreach ($this->definitions as $index => $item) {
185
            if ($item === $definition) {
186
                return $index;
187
            }
188
        }
189
    }
190
191
    /**
192
     * @param IDefinition $definition
193
     */
194
    public function addDefinition(IDefinition $definition) {
195
        $this->removeDefinition($definition->getId());
196
        array_unshift($this->definitions, $definition);
197
    }
198
199
    /**
200
     * @param $string
201
     *
202
     * @return bool
203
     */
204
    protected function isRegexPattern($string) {
205
        return str_starts_with($string, '/') &&
206
            str_ends_with($string, '/') ||
207
            str_starts_with($string, '#') &&
208
            str_ends_with($string, '#');
209
    }
210
211
    /**
212
     * @param $string
213
     * @param $pattern
214
     *
215
     * @return bool
216
     */
217
    protected function matchRegexPattern($string, $pattern) {
218
        return preg_match($pattern, $string) == 1;
219
    }
220
}
221