Issues (16)

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/Node/BaseNode.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
2
3
declare(strict_types=1);
4
5
namespace Linio\Component\Input\Node;
6
7
use Linio\Component\Input\Constraint\ConstraintInterface;
8
use Linio\Component\Input\Exception\InvalidConstraintException;
9
use Linio\Component\Input\Exception\RequiredFieldException;
10
use Linio\Component\Input\InputHandler;
11
use Linio\Component\Input\Instantiator\InstantiatorInterface;
12
use Linio\Component\Input\Transformer\TransformerInterface;
13
use Linio\Component\Input\TypeHandler;
14
15
class BaseNode
16
{
17
    /**
18
     * @var ConstraintInterface[]
19
     */
20
    protected $constraints = [];
21
22
    /**
23
     * @var TransformerInterface
24
     */
25
    protected $transformer;
26
27
    /**
28
     * @var InstantiatorInterface
29
     */
30
    protected $instantiator;
31
32
    /**
33
     * @var string
34
     */
35
    protected $type = 'array';
36
37
    /**
38
     * @var string
39
     */
40
    protected $typeAlias = 'array';
41
42
    /**
43
     * @var bool
44
     */
45
    protected $required = true;
46
47
    /**
48
     * @var mixed
49
     */
50
    protected $default;
51
52
    /**
53
     * @var BaseNode[]
54
     */
55
    protected $children = [];
56
57
    /**
58
     * @var TypeHandler
59
     */
60
    protected $typeHandler;
61
62
    /**
63
     * @var bool
64
     */
65
    protected $allowNull = false;
66
67
    public function setConstraints(array $constraints)
68
    {
69
        $this->constraints = $constraints;
70
    }
71
72
    public function addConstraint(ConstraintInterface $constraint)
73
    {
74
        $this->constraints[] = $constraint;
75
    }
76
77
    public function addConstraints(array $constraints)
78
    {
79
        $this->constraints = array_merge($this->constraints, $constraints);
0 ignored issues
show
Documentation Bug introduced by
It seems like array_merge($this->constraints, $constraints) of type array is incompatible with the declared type array<integer,object<Lin...t\ConstraintInterface>> of property $constraints.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
80
    }
81
82
    public function setTransformer(TransformerInterface $transformer)
83
    {
84
        $this->transformer = $transformer;
85
    }
86
87
    public function setInstantiator(InstantiatorInterface $instantiator)
88
    {
89
        $this->instantiator = $instantiator;
90
    }
91
92
    public function setTypeHandler(TypeHandler $typeHandler)
93
    {
94
        $this->typeHandler = $typeHandler;
95
    }
96
97
    public function setType(string $type)
98
    {
99
        $this->type = $type;
100
    }
101
102
    public function setTypeAlias(string $typeAlias)
103
    {
104
        $this->typeAlias = $typeAlias;
105
    }
106
107
    public function getTypeAlias(): string
108
    {
109
        return $this->typeAlias;
110
    }
111
112
    public function setRequired(bool $required)
113
    {
114
        $this->required = $required;
115
    }
116
117
    public function setDefault($default)
118
    {
119
        $this->default = $default;
120
    }
121
122
    public function setAllowNull(bool $allowNull)
123
    {
124
        $this->allowNull = $allowNull;
125
    }
126
127
    public function getDefault()
128
    {
129
        return $this->default;
130
    }
131
132
    public function hasDefault(): bool
133
    {
134
        return (bool) $this->default;
135
    }
136
137
    public function add(string $key, string $type, array $options = []): BaseNode
138
    {
139
        $child = $this->typeHandler->getType($type);
140
141
        if (isset($options['handler'])) {
142
            /** @var InputHandler $handler */
143
            $handler = $options['handler'];
144
            $handler->setRootType($type);
145
            $handler->define();
146
147
            $child = $handler->getRoot();
148
        }
149
150
        if (isset($options['required'])) {
151
            $child->setRequired($options['required']);
152
        }
153
154
        if (isset($options['default'])) {
155
            $child->setDefault($options['default']);
156
        }
157
158
        if (isset($options['instantiator'])) {
159
            $child->setInstantiator($options['instantiator']);
160
        }
161
162
        if (isset($options['transformer'])) {
163
            $child->setTransformer($options['transformer']);
164
        }
165
166
        if (isset($options['constraints'])) {
167
            $child->addConstraints($options['constraints']);
168
        }
169
170
        if (isset($options['allow_null'])) {
171
            $child->setAllowNull($options['allow_null']);
172
        }
173
174
        $this->children[$key] = $child;
175
176
        return $child;
177
    }
178
179
    public function remove(string $key)
180
    {
181
        unset($this->children[$key]);
182
    }
183
184
    /**
185
     * @return BaseNode[]
186
     */
187
    public function getChildren(): array
188
    {
189
        return $this->children;
190
    }
191
192
    public function hasChildren(): bool
193
    {
194
        return !empty($this->children);
195
    }
196
197
    public function isRequired(): bool
198
    {
199
        if ($this->hasDefault()) {
200
            return false;
201
        }
202
203
        return $this->required;
204
    }
205
206
    public function allowNull(): bool
0 ignored issues
show
function allowNull() does not seem to conform to the naming convention (^(?:is|has|should|may|supports)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
207
    {
208
        return $this->allowNull;
209
    }
210
211
    public function getValue(string $field, $value)
212
    {
213
        if ($this->allowNull() && $value === null) {
214
            return $value;
215
        }
216
217
        $this->checkConstraints($field, $value);
218
219
        if ($this->transformer) {
220
            return $this->transformer->transform($value);
221
        }
222
223
        return $value;
224
    }
225
226
    public function walk($input)
227
    {
228
        $result = [];
229
230
        if (!$this->hasChildren()) {
231
            return $input;
232
        }
233
234 View Code Duplication
        foreach ($this->getChildren() as $field => $config) {
0 ignored issues
show
This code seems to be duplicated across 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...
235
            if (!array_key_exists($field, $input)) {
236
                if ($config->isRequired()) {
237
                    throw new RequiredFieldException($field);
238
                }
239
240
                if (!$config->hasDefault()) {
241
                    continue;
242
                }
243
244
                $input[$field] = $config->getDefault();
245
            }
246
247
            $result[$field] = $config->getValue($field, $config->walk($input[$field]));
248
        }
249
250
        return $result;
251
    }
252
253
    protected function checkConstraints(string $field, $value)
254
    {
255
        foreach ($this->constraints as $constraint) {
256
            if (!$constraint->validate($value)) {
257
                throw new InvalidConstraintException($constraint->getErrorMessage($field));
258
            }
259
        }
260
    }
261
}
262