| Conditions | 56 |
| Paths | > 20000 |
| Total Lines | 219 |
| Code Lines | 129 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 82 | public function getTypeString($schema, $path = '') |
||
| 83 | { |
||
| 84 | if ($schema === null) { |
||
| 85 | return ''; |
||
| 86 | } |
||
| 87 | |||
| 88 | $schema = Schema::unboolSchema($schema); |
||
| 89 | |||
| 90 | $isOptional = false; |
||
| 91 | $isObject = false; |
||
| 92 | $isArray = false; |
||
| 93 | $isBoolean = false; |
||
| 94 | $isString = false; |
||
| 95 | $isNumber = false; |
||
| 96 | |||
| 97 | if ($schema->const !== null) { |
||
| 98 | return '`' . var_export($schema->const, true) . '`'; |
||
| 99 | } |
||
| 100 | |||
| 101 | if (!empty($schema->enum)) { |
||
| 102 | $res = ''; |
||
| 103 | if ($this->confluence) { |
||
| 104 | foreach ($schema->enum as $value) { |
||
| 105 | $res .= '`' . var_export($value, true) . '`, '; |
||
| 106 | } |
||
| 107 | return substr($res, 0, -2); |
||
| 108 | } else { |
||
| 109 | foreach ($schema->enum as $value) { |
||
| 110 | $res .= '<br>`' . var_export($value, true) . '`, '; |
||
| 111 | } |
||
| 112 | return substr($res, 4, -2); |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | if (!empty($schema->getFromRefs())) { |
||
| 117 | $refs = $schema->getFromRefs(); |
||
| 118 | $path = $refs[0]; |
||
| 119 | } |
||
| 120 | |||
| 121 | $type = $schema->type; |
||
| 122 | if ($type === null) { |
||
|
|
|||
| 123 | $type = []; |
||
| 124 | |||
| 125 | if (!empty($schema->properties) || !empty($schema->additionalProperties) || !empty($schema->patternProperties)) { |
||
| 126 | $type[] = Schema::OBJECT; |
||
| 127 | } |
||
| 128 | |||
| 129 | if (!empty($schema->items) || !empty($schema->additionalItems)) { |
||
| 130 | $type[] = Schema::_ARRAY; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | |||
| 134 | if (!is_array($type)) { |
||
| 135 | $type = [$type]; |
||
| 136 | } |
||
| 137 | |||
| 138 | $or = []; |
||
| 139 | |||
| 140 | if ($schema->oneOf !== null) { |
||
| 141 | foreach ($schema->oneOf as $i => $item) { |
||
| 142 | $or[] = $this->getTypeString($item, $path . '/oneOf/' . $i); |
||
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | if ($schema->anyOf !== null) { |
||
| 147 | foreach ($schema->anyOf as $i => $item) { |
||
| 148 | $or[] = $this->getTypeString($item, $path . '/anyOf/' . $i); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | if ($schema->allOf !== null) { |
||
| 153 | foreach ($schema->allOf as $i => $item) { |
||
| 154 | $or[] = $this->getTypeString($item, $path . '/allOf/' . $i); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | if ($schema->then !== null) { |
||
| 159 | $or[] = $this->getTypeString($schema->then, $path . '/then'); |
||
| 160 | } |
||
| 161 | |||
| 162 | if ($schema->else !== null) { |
||
| 163 | $or[] = $this->getTypeString($schema->else, $path . '/else'); |
||
| 164 | } |
||
| 165 | |||
| 166 | foreach ($type as $i => $t) { |
||
| 167 | switch ($t) { |
||
| 168 | case Schema::NULL: |
||
| 169 | $isOptional = true; |
||
| 170 | break; |
||
| 171 | |||
| 172 | case Schema::OBJECT: |
||
| 173 | $isObject = true; |
||
| 174 | break; |
||
| 175 | |||
| 176 | case Schema::_ARRAY: |
||
| 177 | $isArray = true; |
||
| 178 | break; |
||
| 179 | |||
| 180 | case Schema::NUMBER: |
||
| 181 | case Schema::INTEGER: |
||
| 182 | $isNumber = true; |
||
| 183 | break; |
||
| 184 | |||
| 185 | case Schema::STRING: |
||
| 186 | $isString = true; |
||
| 187 | break; |
||
| 188 | |||
| 189 | case Schema::BOOLEAN: |
||
| 190 | $isBoolean = true; |
||
| 191 | break; |
||
| 192 | |||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | |||
| 197 | $namedTypeAdded = false; |
||
| 198 | if (!empty($schema->properties) || $this->hasConstraints($schema)) { |
||
| 199 | if ($this->processed->contains($schema)) { |
||
| 200 | $or [] = $this->processed->offsetGet($schema); |
||
| 201 | $namedTypeAdded = true; |
||
| 202 | } else { |
||
| 203 | if ($schema instanceof Schema) { |
||
| 204 | $typeName = $this->typeName($schema, $path); |
||
| 205 | $this->makeTypeDef($schema, $path); |
||
| 206 | |||
| 207 | $or [] = $typeName; |
||
| 208 | $namedTypeAdded = true; |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | if ($isObject) { |
||
| 214 | $typeAdded = false; |
||
| 215 | |||
| 216 | if ($namedTypeAdded) { |
||
| 217 | $typeAdded = true; |
||
| 218 | } |
||
| 219 | |||
| 220 | if ($schema->additionalProperties instanceof Schema) { |
||
| 221 | $typeName = $this->getTypeString($schema->additionalProperties, $path . '/additionalProperties'); |
||
| 222 | $or [] = "`Map<String,`$typeName`>`"; |
||
| 223 | $typeAdded = true; |
||
| 224 | } |
||
| 225 | |||
| 226 | if (!empty($schema->patternProperties)) { |
||
| 227 | foreach ($schema->patternProperties as $pattern => $propertySchema) { |
||
| 228 | if ($propertySchema instanceof Schema) { |
||
| 229 | $typeName = $this->getTypeString($propertySchema, $path . '/patternProperties/' . $pattern); |
||
| 230 | $or [] = $typeName; |
||
| 231 | $typeAdded = true; |
||
| 232 | } |
||
| 233 | } |
||
| 234 | } |
||
| 235 | |||
| 236 | if (!$typeAdded) { |
||
| 237 | $or [] = '`Object`'; |
||
| 238 | } |
||
| 239 | } |
||
| 240 | |||
| 241 | if ($isArray) { |
||
| 242 | $typeAdded = false; |
||
| 243 | |||
| 244 | if ($schema->items instanceof Schema) { |
||
| 245 | $typeName = $this->getTypeString($schema->items, $path . '/items'); |
||
| 246 | $or [] = "`Array<`$typeName`>`"; |
||
| 247 | $typeAdded = true; |
||
| 248 | } |
||
| 249 | |||
| 250 | if ($schema->additionalItems instanceof Schema) { |
||
| 251 | $typeName = $this->getTypeString($schema->additionalItems, $path . '/additionalItems'); |
||
| 252 | $or [] = "`Array<`$typeName`>`"; |
||
| 253 | $typeAdded = true; |
||
| 254 | } |
||
| 255 | |||
| 256 | if (!$typeAdded) { |
||
| 257 | $or [] = '`Array`'; |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | if ($isOptional) { |
||
| 262 | $or [] = '`null`'; |
||
| 263 | } |
||
| 264 | |||
| 265 | if ($isString) { |
||
| 266 | $or [] = '`String`'; |
||
| 267 | } |
||
| 268 | |||
| 269 | if ($isNumber) { |
||
| 270 | $or [] = '`Number`'; |
||
| 271 | } |
||
| 272 | |||
| 273 | if ($isBoolean) { |
||
| 274 | $or [] = '`Boolean`'; |
||
| 275 | } |
||
| 276 | |||
| 277 | if ($schema->format !== null) { |
||
| 278 | $or [] = 'Format: `' . $schema->format . '`'; |
||
| 279 | } |
||
| 280 | |||
| 281 | $res = ''; |
||
| 282 | foreach ($or as $item) { |
||
| 283 | if (!empty($item) && $item !== '*') { |
||
| 284 | $res .= ', ' . $item; |
||
| 285 | } |
||
| 286 | } |
||
| 287 | |||
| 288 | if ($res !== '') { |
||
| 289 | $res = substr($res, 2); |
||
| 290 | } else { |
||
| 291 | $res = '`*`'; |
||
| 292 | } |
||
| 293 | |||
| 294 | if (empty($res)) { |
||
| 295 | $res = ''; |
||
| 296 | } |
||
| 297 | |||
| 298 | $res = str_replace('``', '', $res); |
||
| 299 | |||
| 300 | return $res; |
||
| 301 | } |
||
| 562 | } |