This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
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
|
|||
| 135 | 138 | $newFormat = null; |
|
| 136 | |||
| 137 | 138 | switch (data_get($schema, 'type')) { |
|
| 138 | 92 | case 'integer': |
|
| 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 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.