| 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 |
||
| 78 | public function getTypeString($schema, $path = '') |
||
| 79 | { |
||
| 80 | if ($schema === null) { |
||
| 81 | return ''; |
||
| 82 | } |
||
| 83 | |||
| 84 | $schema = Schema::unboolSchema($schema); |
||
| 85 | |||
| 86 | $isOptional = false; |
||
| 87 | $isObject = false; |
||
| 88 | $isArray = false; |
||
| 89 | $isBoolean = false; |
||
| 90 | $isString = false; |
||
| 91 | $isNumber = false; |
||
| 92 | |||
| 93 | if ($schema->const !== null) { |
||
| 94 | return '`' . var_export($schema->const, true) . '`'; |
||
| 95 | } |
||
| 96 | |||
| 97 | if (!empty($schema->enum)) { |
||
| 98 | $res = ''; |
||
| 99 | if ($this->confluence) { |
||
| 100 | foreach ($schema->enum as $value) { |
||
| 101 | $res .= '`' . var_export($value, true) . '`, '; |
||
| 102 | } |
||
| 103 | return substr($res, 0, -2); |
||
| 104 | } else { |
||
| 105 | foreach ($schema->enum as $value) { |
||
| 106 | $res .= '<br>`' . var_export($value, true) . '`, '; |
||
| 107 | } |
||
| 108 | return substr($res, 4, -2); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | if (!empty($schema->getFromRefs())) { |
||
| 113 | $refs = $schema->getFromRefs(); |
||
| 114 | $path = $refs[0]; |
||
| 115 | } |
||
| 116 | |||
| 117 | $type = $schema->type; |
||
| 118 | if ($type === null) { |
||
|
|
|||
| 119 | $type = []; |
||
| 120 | |||
| 121 | if (!empty($schema->properties) || !empty($schema->additionalProperties) || !empty($schema->patternProperties)) { |
||
| 122 | $type[] = Schema::OBJECT; |
||
| 123 | } |
||
| 124 | |||
| 125 | if (!empty($schema->items) || !empty($schema->additionalItems)) { |
||
| 126 | $type[] = Schema::_ARRAY; |
||
| 127 | } |
||
| 128 | } |
||
| 129 | |||
| 130 | if (!is_array($type)) { |
||
| 131 | $type = [$type]; |
||
| 132 | } |
||
| 133 | |||
| 134 | $or = []; |
||
| 135 | |||
| 136 | if ($schema->oneOf !== null) { |
||
| 137 | foreach ($schema->oneOf as $i => $item) { |
||
| 138 | $or[] = $this->getTypeString($item, $path . '/oneOf/' . $i); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | if ($schema->anyOf !== null) { |
||
| 143 | foreach ($schema->anyOf as $i => $item) { |
||
| 144 | $or[] = $this->getTypeString($item, $path . '/anyOf/' . $i); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | if ($schema->allOf !== null) { |
||
| 149 | foreach ($schema->allOf as $i => $item) { |
||
| 150 | $or[] = $this->getTypeString($item, $path . '/allOf/' . $i); |
||
| 151 | } |
||
| 152 | } |
||
| 153 | |||
| 154 | if ($schema->then !== null) { |
||
| 155 | $or[] = $this->getTypeString($schema->then, $path . '/then'); |
||
| 156 | } |
||
| 157 | |||
| 158 | if ($schema->else !== null) { |
||
| 159 | $or[] = $this->getTypeString($schema->else, $path . '/else'); |
||
| 160 | } |
||
| 161 | |||
| 162 | foreach ($type as $i => $t) { |
||
| 163 | switch ($t) { |
||
| 164 | case Schema::NULL: |
||
| 165 | $isOptional = true; |
||
| 166 | break; |
||
| 167 | |||
| 168 | case Schema::OBJECT: |
||
| 169 | $isObject = true; |
||
| 170 | break; |
||
| 171 | |||
| 172 | case Schema::_ARRAY: |
||
| 173 | $isArray = true; |
||
| 174 | break; |
||
| 175 | |||
| 176 | case Schema::NUMBER: |
||
| 177 | case Schema::INTEGER: |
||
| 178 | $isNumber = true; |
||
| 179 | break; |
||
| 180 | |||
| 181 | case Schema::STRING: |
||
| 182 | $isString = true; |
||
| 183 | break; |
||
| 184 | |||
| 185 | case Schema::BOOLEAN: |
||
| 186 | $isBoolean = true; |
||
| 187 | break; |
||
| 188 | |||
| 189 | } |
||
| 190 | } |
||
| 191 | |||
| 192 | |||
| 193 | $namedTypeAdded = false; |
||
| 194 | if (!empty($schema->properties) || $this->hasConstraints($schema)) { |
||
| 195 | if ($this->processed->contains($schema)) { |
||
| 196 | $or [] = $this->processed->offsetGet($schema); |
||
| 197 | $namedTypeAdded = true; |
||
| 198 | } else { |
||
| 199 | if ($schema instanceof Schema) { |
||
| 200 | $typeName = $this->typeName($schema, $path); |
||
| 201 | $this->makeTypeDef($schema, $path); |
||
| 202 | |||
| 203 | $or [] = $typeName; |
||
| 204 | $namedTypeAdded = true; |
||
| 205 | } |
||
| 206 | } |
||
| 207 | } |
||
| 208 | |||
| 209 | if ($isObject) { |
||
| 210 | $typeAdded = false; |
||
| 211 | |||
| 212 | if ($namedTypeAdded) { |
||
| 213 | $typeAdded = true; |
||
| 214 | } |
||
| 215 | |||
| 216 | if ($schema->additionalProperties instanceof Schema) { |
||
| 217 | $typeName = $this->getTypeString($schema->additionalProperties, $path . '/additionalProperties'); |
||
| 218 | $or [] = "`Map<String,`$typeName`>`"; |
||
| 219 | $typeAdded = true; |
||
| 220 | } |
||
| 221 | |||
| 222 | if (!empty($schema->patternProperties)) { |
||
| 223 | foreach ($schema->patternProperties as $pattern => $propertySchema) { |
||
| 224 | if ($propertySchema instanceof Schema) { |
||
| 225 | $typeName = $this->getTypeString($propertySchema, $path . '/patternProperties/' . $pattern); |
||
| 226 | $or [] = $typeName; |
||
| 227 | $typeAdded = true; |
||
| 228 | } |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | if (!$typeAdded) { |
||
| 233 | $or [] = '`Object`'; |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | if ($isArray) { |
||
| 238 | $typeAdded = false; |
||
| 239 | |||
| 240 | if ($schema->items instanceof Schema) { |
||
| 241 | $typeName = $this->getTypeString($schema->items, $path . '/items'); |
||
| 242 | $or [] = "`Array<`$typeName`>`"; |
||
| 243 | $typeAdded = true; |
||
| 244 | } |
||
| 245 | |||
| 246 | if ($schema->additionalItems instanceof Schema) { |
||
| 247 | $typeName = $this->getTypeString($schema->additionalItems, $path . '/additionalItems'); |
||
| 248 | $or [] = "`Array<`$typeName`>`"; |
||
| 249 | $typeAdded = true; |
||
| 250 | } |
||
| 251 | |||
| 252 | if (!$typeAdded) { |
||
| 253 | $or [] = '`Array`'; |
||
| 254 | } |
||
| 255 | } |
||
| 256 | |||
| 257 | if ($isOptional) { |
||
| 258 | $or [] = '`null`'; |
||
| 259 | } |
||
| 260 | |||
| 261 | if ($isString) { |
||
| 262 | $or [] = '`String`'; |
||
| 263 | } |
||
| 264 | |||
| 265 | if ($isNumber) { |
||
| 266 | $or [] = '`Number`'; |
||
| 267 | } |
||
| 268 | |||
| 269 | if ($isBoolean) { |
||
| 270 | $or [] = '`Boolean`'; |
||
| 271 | } |
||
| 272 | |||
| 273 | if ($schema->format !== null) { |
||
| 274 | $or [] = 'Format: `' . $schema->format . '`'; |
||
| 275 | } |
||
| 276 | |||
| 277 | $res = ''; |
||
| 278 | foreach ($or as $item) { |
||
| 279 | if (!empty($item) && $item !== '*') { |
||
| 280 | $res .= ', ' . $item; |
||
| 281 | } |
||
| 282 | } |
||
| 283 | |||
| 284 | if ($res !== '') { |
||
| 285 | $res = substr($res, 2); |
||
| 286 | } else { |
||
| 287 | $res = '`*`'; |
||
| 288 | } |
||
| 289 | |||
| 290 | if (empty($res)) { |
||
| 291 | $res = ''; |
||
| 292 | } |
||
| 293 | |||
| 294 | $res = str_replace('``', '', $res); |
||
| 295 | |||
| 296 | return $res; |
||
| 297 | } |
||
| 554 | } |