Issues (6)

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/Converter.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 namespace HSkrasek\OpenAPI;
2
3
class Converter
4
{
5
    private const STRUCTS = [
6
        'allOf',
7
        'anyOf',
8
        'oneOf',
9
        'not',
10
        'items',
11
        'additionalProperties',
12
    ];
13
14
    private const NOT_SUPPORTED = [
15
        'nullable',
16
        'discriminator',
17
        'readOnly',
18
        'writeOnly',
19
        'xml',
20
        'externalDocs',
21
        'example',
22
        'deprecated',
23
    ];
24
25
    /**
26
     * @var array
27
     */
28
    private $options;
29
30 138
    public function __construct(?array $options = null)
31
    {
32 138
        $this->options = $this->createOptions($options ?: []);
33 138
    }
34
35
    /**
36
     * @param mixed $schema
37
     *
38
     * @return mixed
39
     */
40 138
    public function convert($schema)
41
    {
42 138
        $schema = $this->convertSchema($schema);
43 138
        data_set($schema, '$schema', 'http://json-schema.org/draft-04/schema#');
44
45 138
        return $schema;
46
    }
47
48
    /**
49
     * @param mixed $schema
50
     *
51
     * @return mixed
52
     */
53 138
    private function convertSchema($schema)
54
    {
55 138
        foreach (self::STRUCTS as $i => $struct) {
56 138
            if (\is_array(data_get($schema, $struct))) {
57 24
                foreach ($schema->$struct as $j => $nestedStruct) {
58 24
                    $schema->$struct[$j] = $this->convertSchema($nestedStruct);
59
                }
60 138
            } elseif (\is_object(data_get($schema, $struct))) {
61 138
                $schema->$struct = $this->convertSchema($schema->$struct);
62
            }
63
        }
64
65 138
        if (\is_object($properties = data_get($schema, 'properties'))) {
66 69
            data_set($schema, 'properties', $this->convertProperties($properties));
67
68 69
            if (\is_array($required = data_get($schema, 'required'))) {
69 27
                data_set($schema, 'required', $required = $this->cleanRequired($required, $properties));
70
71 27
                if (\count($required) === 0) {
72 3
                    $this->removeFromSchema($schema, 'required');
73
                }
74
            }
75
76 69
            if (\count(get_object_vars($properties)) === 0) {
77 6
                $this->removeFromSchema($schema, 'properties');
78
            }
79
        }
80
81 138
        $schema = $this->convertTypes($schema);
82
83 138
        if (\is_object(data_get($schema, 'x-patternProperties')) && $this->options['support_pattern_properties']) {
84 27
            $schema = $this->convertPatternProperties($schema, $this->options['pattern_properties_handler']);
85
        }
86
87 138
        foreach ($this->options['not_supported'] as $notSupported) {
88 138
            $this->removeFromSchema($schema, $notSupported);
89
        }
90
91 138
        return $schema;
92
    }
93
94 69
    private function convertProperties($properties)
95
    {
96 69
        foreach ($properties as $key => $property) {
97 69
            $removeProperty = false;
98
99 69
            foreach ($this->options['remove_properties'] as $prop) {
100 21
                if (data_get($property, $prop) === true) {
101 21
                    $removeProperty = true;
102
                }
103
            }
104
105 69
            if ($removeProperty) {
106 21
                $this->removeFromSchema($properties, $key);
107 21
                continue;
108
            }
109
110 66
            data_set($properties, $key, $this->convertSchema($property));
111
        }
112
113 69
        return $properties;
114
    }
115
116
    /**
117
     * @param mixed $schema
118
     *
119
     * @return mixed
120
     */
121 138
    private function convertTypes($schema)
122
    {
123 138
        if (null === data_get($schema, 'type')) {
124 24
            return $schema;
125
        }
126
127 138
        if (data_get($schema, 'type') === 'string' && data_get(
128 87
            $schema,
129 87
            'format'
130 138
        ) === 'date' && $this->options['convert_date'] === true) {
131 3
            data_set($schema, 'format', 'date-time');
132
        }
133
134 138
        $newType   = null;
0 ignored issues
show
$newType is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
135 138
        $newFormat = null;
136
137 138
        switch (data_get($schema, 'type')) {
138 92
            case 'integer':
0 ignored issues
show
case statements should be defined using a colon.

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

There is also the option to use a semicolon instead of a colon, this is discouraged because many programmers do not even know it works and the colon is universal between programming languages.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B"; //wrong
        doSomething();
        break;
    case "C": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
139 12
                $newType = 'integer';
140 12
                break;
141 92
            case 'long':
142 12
                $newType   = 'integer';
143 12
                $newFormat = 'int64';
144 12
                break;
145 90
            case 'float':
146 3
                $newType   = 'number';
147 3
                $newFormat = 'float';
148 3
                break;
149 90
            case 'double':
150 12
                $newType   = 'number';
151 12
                $newFormat = 'double';
152 12
                break;
153 90
            case 'byte':
154 3
                $newType   = 'string';
155 3
                $newFormat = 'byte';
156 3
                break;
157 88
            case 'binary':
158 3
                $newType   = 'string';
159 3
                $newFormat = 'binary';
160 3
                break;
161 86
            case 'date':
162 3
                $newType   = 'string';
163 3
                $newFormat = $this->options['convert_date'] ? 'date-time' : 'date';
164 3
                break;
165 86
            case 'dateTime':
166 6
                $newType   = 'string';
167 6
                $newFormat = 'date-time';
168 6
                break;
169 84
            case 'password':
170 9
                $newType   = 'string';
171 9
                $newFormat = 'password';
172 9
                break;
173
            default:
174 123
                $newType = data_get($schema, 'type');
175
        }
176
177 138
        data_set($schema, 'type', $newType);
178 138
        data_set($schema, 'format', \is_string($newFormat) ? $newFormat : data_get($schema, 'format'));
179
180 138
        if (null === data_get($schema, 'format')) {
181 120
            $this->removeFromSchema($schema, 'format');
182
        }
183
184 138
        if (data_get($schema, 'nullable', false) === true) {
185 12
            data_set($schema, 'type', [data_get($schema, 'type'), 'null']);
186
        }
187
188 138
        return $schema;
189
    }
190
191 27
    private function cleanRequired(?array $required = [], $properties = null): array
192
    {
193 27
        foreach ($required as $key => $requiredProperty) {
194 27
            if (!isset($properties->{$requiredProperty}, $properties)) {
195 27
                unset($required[$key]);
196
            }
197
        }
198
199 27
        return array_values($required);
200
    }
201
202 27
    private function convertPatternProperties($schema, callable $handler)
203
    {
204 27
        data_set($schema, 'patternProperties', data_get($schema, 'x-patternProperties'));
205 27
        $this->removeFromSchema($schema, 'x-patternProperties');
206
207 27
        return call_user_func($handler, $schema);
208
    }
209
210 24
    private function patternPropertiesHandler($schema)
211
    {
212 24
        $patternProperties = data_get($schema, 'patternProperties');
213
214 24
        if (!\is_object($additionalProperties = data_get($schema, 'additionalProperties'))) {
215 3
            return $schema;
216
        }
217
218 21
        foreach ($patternProperties as $patternProperty) {
219 21
            if ($patternProperty == $additionalProperties) {
220 18
                data_set($schema, 'additionalProperties', false);
221 21
                break;
222
            }
223
        }
224
225 21
        return $schema;
226
    }
227
228 138
    private function createOptions(array $options): array
229
    {
230 138
        $options['convert_date']               = $options['convert_date'] ?? false;
231 138
        $options['support_pattern_properties'] = $options['support_pattern_properties'] ?? false;
232 138
        $options['keep_not_supported']         = $options['keep_not_supported'] ?? [];
233 138
        $options['pattern_properties_handler'] = $options['pattern_properties_handler'] ?? [
234 135
                $this,
235 135
                'patternPropertiesHandler',
236
            ];
237
238 138
        $options['remove_properties'] = [];
239
240 138
        if ($options['remove_read_only'] ?? false) {
241 18
            $options['remove_properties'][] = 'readOnly';
242
        }
243
244 138
        if ($options['remove_write_only'] ?? false) {
245 3
            $options['remove_properties'][] = 'writeOnly';
246
        }
247
248 138
        $options['not_supported'] = $this->resolveNotSupported(self::NOT_SUPPORTED, $options['keep_not_supported']);
249
250 138
        return $options;
251
    }
252
253 138
    private function resolveNotSupported(array $notSupported, array $toRetain): array
254
    {
255 138
        return array_values(array_diff($notSupported, $toRetain));
256
    }
257
258 138
    private function removeFromSchema($schema, string $key): void
259
    {
260 138
        if (\is_object($schema)) {
261 138
            unset($schema->{$key});
262
263 138
            return;
264
        }
265
266
        unset($schema[$key]);
267
268
        return;
269
    }
270
}
271