| Conditions | 53 |
| Paths | > 20000 |
| Total Lines | 208 |
| Code Lines | 122 |
| 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 |
||
| 43 | public function getTypeString($schema, $path = '') |
||
| 44 | { |
||
| 45 | if ($schema === null) { |
||
| 46 | return ''; |
||
| 47 | } |
||
| 48 | |||
| 49 | $schema = Schema::unboolSchema($schema); |
||
| 50 | |||
| 51 | $isOptional = false; |
||
| 52 | $isObject = false; |
||
| 53 | $isArray = false; |
||
| 54 | $isBoolean = false; |
||
| 55 | $isString = false; |
||
| 56 | $isNumber = false; |
||
| 57 | |||
| 58 | if ($schema->const !== null) { |
||
| 59 | return '`' . var_export($schema->const, true) . '`'; |
||
| 60 | } |
||
| 61 | |||
| 62 | if (!empty($schema->enum)) { |
||
| 63 | $res = ''; |
||
| 64 | foreach ($schema->enum as $value) { |
||
| 65 | $res .= '<br>`' . var_export($value, true) . '`, '; |
||
| 66 | } |
||
| 67 | return substr($res, 4, -2); |
||
| 68 | } |
||
| 69 | |||
| 70 | if (!empty($schema->getFromRefs())) { |
||
| 71 | $refs = $schema->getFromRefs(); |
||
| 72 | $path = $refs[0]; |
||
| 73 | } |
||
| 74 | |||
| 75 | $type = $schema->type; |
||
| 76 | if ($type === null) { |
||
|
|
|||
| 77 | $type = []; |
||
| 78 | |||
| 79 | if (!empty($schema->properties) || !empty($schema->additionalProperties) || !empty($schema->patternProperties)) { |
||
| 80 | $type[] = Schema::OBJECT; |
||
| 81 | } |
||
| 82 | |||
| 83 | if (!empty($schema->items) || !empty($schema->additionalItems)) { |
||
| 84 | $type[] = Schema::_ARRAY; |
||
| 85 | } |
||
| 86 | } |
||
| 87 | |||
| 88 | if (!is_array($type)) { |
||
| 89 | $type = [$type]; |
||
| 90 | } |
||
| 91 | |||
| 92 | $or = []; |
||
| 93 | |||
| 94 | if ($schema->oneOf !== null) { |
||
| 95 | foreach ($schema->oneOf as $i => $item) { |
||
| 96 | $or[] = $this->getTypeString($item, $path . '/oneOf/' . $i); |
||
| 97 | } |
||
| 98 | } |
||
| 99 | |||
| 100 | if ($schema->anyOf !== null) { |
||
| 101 | foreach ($schema->anyOf as $i => $item) { |
||
| 102 | $or[] = $this->getTypeString($item, $path . '/anyOf/' . $i); |
||
| 103 | } |
||
| 104 | } |
||
| 105 | |||
| 106 | if ($schema->allOf !== null) { |
||
| 107 | foreach ($schema->allOf as $i => $item) { |
||
| 108 | $or[] = $this->getTypeString($item, $path . '/allOf/' . $i); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | if ($schema->then !== null) { |
||
| 113 | $or[] = $this->getTypeString($schema->then, $path . '/then'); |
||
| 114 | } |
||
| 115 | |||
| 116 | if ($schema->else !== null) { |
||
| 117 | $or[] = $this->getTypeString($schema->else, $path . '/else'); |
||
| 118 | } |
||
| 119 | |||
| 120 | foreach ($type as $i => $t) { |
||
| 121 | switch ($t) { |
||
| 122 | case Schema::NULL: |
||
| 123 | $isOptional = true; |
||
| 124 | break; |
||
| 125 | |||
| 126 | case Schema::OBJECT: |
||
| 127 | $isObject = true; |
||
| 128 | break; |
||
| 129 | |||
| 130 | case Schema::_ARRAY: |
||
| 131 | $isArray = true; |
||
| 132 | break; |
||
| 133 | |||
| 134 | case Schema::NUMBER: |
||
| 135 | case Schema::INTEGER: |
||
| 136 | $isNumber = true; |
||
| 137 | break; |
||
| 138 | |||
| 139 | case Schema::STRING: |
||
| 140 | $isString = true; |
||
| 141 | break; |
||
| 142 | |||
| 143 | case Schema::BOOLEAN: |
||
| 144 | $isBoolean = true; |
||
| 145 | break; |
||
| 146 | |||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | |||
| 151 | $namedTypeAdded = false; |
||
| 152 | if (!empty($schema->properties) || $this->hasConstraints($schema)) { |
||
| 153 | if ($this->processed->contains($schema)) { |
||
| 154 | $or [] = $this->processed->offsetGet($schema); |
||
| 155 | $namedTypeAdded = true; |
||
| 156 | } else { |
||
| 157 | if ($schema instanceof Schema) { |
||
| 158 | $typeName = $this->typeName($schema, $path); |
||
| 159 | $this->makeTypeDef($schema, $path); |
||
| 160 | |||
| 161 | $or [] = $typeName; |
||
| 162 | $namedTypeAdded = true; |
||
| 163 | } |
||
| 164 | } |
||
| 165 | } |
||
| 166 | |||
| 167 | if ($isObject) { |
||
| 168 | $typeAdded = false; |
||
| 169 | |||
| 170 | if ($namedTypeAdded) { |
||
| 171 | $typeAdded = true; |
||
| 172 | } |
||
| 173 | |||
| 174 | if ($schema->additionalProperties instanceof Schema) { |
||
| 175 | $typeName = $this->getTypeString($schema->additionalProperties, $path . '/additionalProperties'); |
||
| 176 | $or [] = "`Map<String,`$typeName`>`"; |
||
| 177 | $typeAdded = true; |
||
| 178 | } |
||
| 179 | |||
| 180 | if (!empty($schema->patternProperties)) { |
||
| 181 | foreach ($schema->patternProperties as $pattern => $propertySchema) { |
||
| 182 | if ($propertySchema instanceof Schema) { |
||
| 183 | $typeName = $this->getTypeString($propertySchema, $path . '/patternProperties/' . $pattern); |
||
| 184 | $or [] = $typeName; |
||
| 185 | $typeAdded = true; |
||
| 186 | } |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | if (!$typeAdded) { |
||
| 191 | $or [] = '`Object`'; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | if ($isArray) { |
||
| 196 | $typeAdded = false; |
||
| 197 | |||
| 198 | if ($schema->items instanceof Schema) { |
||
| 199 | $typeName = $this->getTypeString($schema->items, $path . '/items'); |
||
| 200 | $or [] = "`Array<`$typeName`>`"; |
||
| 201 | $typeAdded = true; |
||
| 202 | } |
||
| 203 | |||
| 204 | if ($schema->additionalItems instanceof Schema) { |
||
| 205 | $typeName = $this->getTypeString($schema->additionalItems, $path . '/additionalItems'); |
||
| 206 | $or [] = "`Array<`$typeName`>`"; |
||
| 207 | $typeAdded = true; |
||
| 208 | } |
||
| 209 | |||
| 210 | if (!$typeAdded) { |
||
| 211 | $or [] = '`Array`'; |
||
| 212 | } |
||
| 213 | } |
||
| 214 | |||
| 215 | if ($isOptional) { |
||
| 216 | $or [] = '`null`'; |
||
| 217 | } |
||
| 218 | |||
| 219 | if ($isString) { |
||
| 220 | $or [] = '`String`'; |
||
| 221 | } |
||
| 222 | |||
| 223 | if ($isNumber) { |
||
| 224 | $or [] = '`Number`'; |
||
| 225 | } |
||
| 226 | |||
| 227 | if ($isBoolean) { |
||
| 228 | $or [] = '`Boolean`'; |
||
| 229 | } |
||
| 230 | |||
| 231 | if ($schema->format !== null) { |
||
| 232 | $or [] = 'Format: `' . $schema->format . '`'; |
||
| 233 | } |
||
| 234 | |||
| 235 | $res = ''; |
||
| 236 | foreach ($or as $item) { |
||
| 237 | if (!empty($item) && $item !== '*') { |
||
| 238 | $res .= ', ' . $item; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | if ($res !== '') { |
||
| 243 | $res = substr($res, 2); |
||
| 244 | } else { |
||
| 245 | $res = '`*`'; |
||
| 246 | } |
||
| 247 | |||
| 248 | $res = str_replace('``', '', $res); |
||
| 249 | |||
| 250 | return $res; |
||
| 251 | } |
||
| 467 | } |