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/Resolver.php (2 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
2
3
namespace Weew\Container;
4
5
use Weew\Container\Definitions\ClassDefinition;
6
use Weew\Container\Definitions\InterfaceDefinition;
7
use Weew\Container\Definitions\WildcardDefinition;
8
use Weew\Container\Exceptions\ImplementationNotFoundException;
9
use Weew\Container\Exceptions\TypeMismatchException;
10
use Weew\Container\Exceptions\ValueNotFoundException;
11
12
class Resolver {
13
    /**
14
     * @var IContainer
15
     */
16
    protected $container;
17
18
    /**
19
     * @var Reflector
20
     */
21
    protected $reflector;
22
23
    /**
24
     * @var bool
25
     */
26
    protected $strictMode = true;
27
28
    /**
29
     * @param IContainer $container
30
     * @param Reflector $reflector
31
     */
32
    public function __construct(
33
        IContainer $container,
34
        Reflector $reflector
35
    ) {
36
        $this->container = $container;
37
        $this->reflector = $reflector;
38
    }
39
40
    /**
41
     * @param string $id
42
     * @param array $args
43
     *
44
     * @return mixed
45
     * @throws ImplementationNotFoundException
46
     * @throws TypeMismatchException
47
     * @throws ValueNotFoundException
48
     */
49
    public function resolveWithoutDefinition($id, array $args = []) {
50
        if (class_exists($id)) {
51
            return $this->getClass(new ClassDefinition($id, null), $args);
52
        }
53
54
        if (interface_exists($id)) {
55
            throw new ImplementationNotFoundException(
56
                s('No implementation found in container for interface %s.', $id)
57
            );
58
        }
59
60
        throw new ValueNotFoundException(
61
            s('No value found in container for id %s.', $id)
62
        );
63
    }
64
65
    /**
66
     * @param IDefinition $definition
67
     * @param $id
68
     * @param array $args
69
     *
70
     * @return mixed
71
     * @throws TypeMismatchException
72
     */
73
    public function resolveDefinition(IDefinition $definition, $id, array $args = []) {
74
        if ($definition instanceof WildcardDefinition) {
75
            $value = $this->getWildcard($definition, $id, $args);
76
        } else if ($definition instanceof InterfaceDefinition) {
77
            $value = $this->getInterface($definition, $args);
78
        } else if ($definition instanceof ClassDefinition) {
79
            $value = $this->getClass($definition, $args);
80
        } else {
81
            $value = $definition->getValue();
82
        }
83
84
        return $value;
85
    }
86
87
    /**
88
     * @param ClassDefinition $definition
89
     * @param array $args
90
     *
91
     * @return mixed
92
     * @throws TypeMismatchException
93
     */
94
    public function getClass(ClassDefinition $definition, array $args = []) {
95
        $abstract = $definition->getValue();
96
        $class = $definition->getId();
97
98
        if (is_callable($abstract)) {
99
            $instance = $this->container->call($abstract, $args);
100
        } else if (is_object($abstract)) {
101
            $instance = $abstract;
102
        } else {
103
            if ($abstract === null) {
104
                $abstract = $class;
105
            }
106
107
            $instance = $this->reflector
108
                ->resolveClass($this->container, $abstract, $args);
109
        }
110
111
        $this->matchClassType($class, $instance);
112
113
        return $instance;
114
    }
115
116
    /**
117
     * @param InterfaceDefinition $definition
118
     * @param array $args
119
     *
120
     * @return mixed
121
     * @throws TypeMismatchException
122
     */
123 View Code Duplication
    public function getInterface(InterfaceDefinition $definition, array $args = []) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
124
        $abstract = $definition->getValue();
125
        $interface = $definition->getId();
126
        $instance = $this->resolveAbstract($abstract, $args);
127
        $this->matchClassType($interface, $instance);
128
129
        return $instance;
130
    }
131
132
    /**
133
     * @param IDefinition $definition
134
     * @param $id
135
     * @param $args
136
     *
137
     * @return mixed|null
138
     * @throws TypeMismatchException
139
     */
140 View Code Duplication
    public function getWildcard(IDefinition $definition, $id, $args) {
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
141
        $abstract = $definition->getValue();
142
        $args['abstract'] = $id;
143
        $instance = $this->resolveAbstract($abstract, $args);
144
        $this->matchClassType($id, $instance);
145
146
        return $instance;
147
    }
148
149
    /**
150
     * @param bool $strictMode
151
     */
152
    public function setStrictMode($strictMode) {
153
        $this->strictMode = $strictMode;
154
    }
155
156
    /**
157
     * @return bool
158
     */
159
    public function isInStrictMode() {
160
        return $this->strictMode;
161
    }
162
163
    /**
164
     * @param $abstract
165
     * @param array $args
166
     *
167
     * @return mixed
168
     */
169
    protected function resolveAbstract($abstract, array $args) {
170
        if (is_callable($abstract)) {
171
            return $this->container->call($abstract, $args);
172
        } else if (is_object($abstract)) {
173
            return $abstract;
174
        } else if (class_exists($abstract)) {
175
            return $this->getClass(new ClassDefinition($abstract, null), $args);
176
        }
177
178
        return null;
179
    }
180
181
    /**
182
     * @param $class
183
     * @param $instance
184
     *
185
     * @throws TypeMismatchException
186
     */
187
    protected function matchClassType($class, $instance) {
188
        if ( ! $this->isInStrictMode()) {
189
            return;
190
        }
191
192
        if ( ! $instance instanceof $class) {
193
            throw new TypeMismatchException(
194
                s(
195
                    'Container expects an implementation of type %s, %s given.',
196
                    $class, get_type($instance)
197
                )
198
            );
199
        }
200
    }
201
}
202