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/Attribute/Attribute.php (2 issues)

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
 * This file is part of Properties package.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
declare(strict_types=1);
9
10
namespace Serafim\Properties\Attribute;
11
12
/**
13
 * Class Attribute
14
 */
15
class Attribute implements AttributeInterface
16
{
17
    /**
18
     * @var string
19
     */
20
    protected $name;
21
22
    /**
23
     * @var int
24
     */
25
    protected $type;
26
27
    /**
28
     * @var Matchable
29
     */
30
    protected $matcher;
31
32
    /**
33
     * @var string
34
     */
35
    protected $camel;
36
37
    /**
38
     * Attribute constructor.
39
     * @param string $name
40
     * @param int $type
41
     */
42
    public function __construct(string $name, int $type = self::TYPE_UNDEFINED)
43
    {
44
        $this->name  = $name;
45
        $this->type  = $type;
46
        $this->camel = $this->toCamelCase($name);
47
    }
48
49
    /**
50
     * @param string $name
51
     * @return string
52
     */
53
    private function toCamelCase(string $name): string
54
    {
55
        $name = \ucwords($name, '_');
56
        $name = \str_replace('_', '', $name);
57
58
        return $name;
59
    }
60
61
    /**
62
     * @param mixed|object $context
63
     * @return mixed
64
     */
65
    public function getValueFrom($context)
66
    {
67
        $value = $this->getFromAttribute($context);
68
69
        switch (true) {
70
            case \method_exists($context, $getter = $this->getPropertyGetter()):
71
                return $this->getUsingGetter($context, $getter, $value);
0 ignored issues
show
The variable $getter seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
72
73
            case \method_exists($context, $getter = $this->getBooleanGetter()):
74
                return $this->getUsingGetter($context, $getter, $value);
0 ignored issues
show
The variable $getter seems only to be defined at a later point. Did you maybe move this code here without moving the variable definition?

This error can happen if you refactor code and forget to move the variable initialization.

Let’s take a look at a simple example:

function someFunction() {
    $x = 5;
    echo $x;
}

The above code is perfectly fine. Now imagine that we re-order the statements:

function someFunction() {
    echo $x;
    $x = 5;
}

In that case, $x would be read before it is initialized. This was a very basic example, however the principle is the same for the found issue.

Loading history...
75
76
            default:
77
                return $value;
78
        }
79
    }
80
81
    /**
82
     * @param mixed $context
83
     * @return mixed
84
     */
85
    private function getFromAttribute($context)
86
    {
87
        return (function (string $name) {
88
            return \property_exists($this, $name) ? $this->$name : null;
89
        })->call($context, $this->name);
90
    }
91
92
    /**
93
     * @return string
94
     */
95
    private function getPropertyGetter(): string
96
    {
97
        return 'get' . $this->camel;
98
    }
99
100
    /**
101
     * @param object $context
102
     * @param string $getter
103
     * @param mixed $value
104
     * @return mixed
105
     */
106
    private function getUsingGetter($context, string $getter, $value)
107
    {
108
        return (function ($getter) use ($value) {
109
            return $this->$getter($value);
110
        })->call($context, $getter);
111
    }
112
113
    /**
114
     * @return string
115
     */
116
    private function getBooleanGetter(): string
117
    {
118
        return 'is' . $this->camel;
119
    }
120
121
    /**
122
     * @param mixed|object $context
123
     * @param mixed $value
124
     * @return void
125
     */
126
    public function setValueTo($context, $value): void
127
    {
128
        if (\method_exists($context, $setter = $this->getPropertySetter())) {
129
            $this->setUsingSetter($context, $setter, $value);
130
131
            return;
132
        }
133
134
        $this->setToAttribute($context, $value);
135
    }
136
137
    /**
138
     * @return string
139
     */
140
    private function getPropertySetter(): string
141
    {
142
        return 'set' . $this->camel;
143
    }
144
145
    /**
146
     * @param object $context
147
     * @param string $setter
148
     * @param mixed $value
149
     * @return void
150
     */
151
    private function setUsingSetter($context, string $setter, $value): void
152
    {
153
        (function () use ($setter, $value) {
154
            $this->$setter($value);
155
        })->call($context);
156
    }
157
158
    /**
159
     * @param object $context
160
     * @param mixed $value
161
     */
162
    private function setToAttribute($context, $value): void
163
    {
164
        (function (string $name) use ($value) {
165
            return $this->$name = $value;
166
        })->call($context, $this->name);
167
    }
168
169
    /**
170
     * @param Matchable $hint
171
     * @return Matchable
172
     */
173
    public function addMatcher(Matchable $hint): Matchable
174
    {
175
        $this->matcher = $hint;
176
177
        return $this;
178
    }
179
180
    /**
181
     * @return bool
182
     */
183
    public function isReadable(): bool
184
    {
185
        return $this->type === static::TYPE_READABLE || $this->type === static::TYPE_PROPERTY;
186
    }
187
188
    /**
189
     * @return bool
190
     */
191
    public function isWritable(): bool
192
    {
193
        return $this->type === static::TYPE_WRITABLE || $this->type === static::TYPE_PROPERTY;
194
    }
195
196
    /**
197
     * @param mixed $value
198
     * @return bool
199
     */
200
    public function match($value): bool
201
    {
202
        if ($this->matcher === null) {
203
            return true;
204
        }
205
206
        return $this->matcher->match($value);
207
    }
208
209
    /**
210
     * @return string
211
     */
212
    public function getName(): string
213
    {
214
        return $this->name;
215
    }
216
}
217