| Conditions | 45 |
| Paths | > 20000 |
| Total Lines | 173 |
| Code Lines | 94 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 145 | protected function readSchemaDeeper($schemaArray) |
||
| 146 | { |
||
| 147 | $schema = new Schema(); |
||
| 148 | if (null === $this->rootSchema) { |
||
| 149 | $this->rootSchema = $schema; |
||
| 150 | $this->rootData = $schemaArray; |
||
| 151 | } |
||
| 152 | |||
| 153 | if ($schemaArray instanceof \stdClass) { |
||
| 154 | $schemaArray = (array)$schemaArray; |
||
| 155 | } |
||
| 156 | |||
| 157 | if (isset($schemaArray[self::ID])) { |
||
| 158 | $parentScope = $this->resolutionScope; |
||
| 159 | $this->resolutionScope = Helper::resolveURI($parentScope, $schemaArray[self::ID]); |
||
| 160 | /** @noinspection PhpUnusedLocalVariableInspection */ |
||
| 161 | $defer = new ScopeExit(function () use ($parentScope) { |
||
| 162 | $this->resolutionScope = $parentScope; |
||
| 163 | }); |
||
| 164 | } |
||
| 165 | |||
| 166 | if (isset($schemaArray[self::TYPE])) { |
||
| 167 | $schema->type = $schemaArray[self::TYPE]; |
||
| 168 | } |
||
| 169 | |||
| 170 | |||
| 171 | // Object |
||
| 172 | if (isset($schemaArray[self::PROPERTIES])) { |
||
| 173 | $properties = new Properties(); |
||
| 174 | $schema->properties = $properties; |
||
| 175 | foreach ($schemaArray[self::PROPERTIES] as $name => $data) { |
||
| 176 | $properties->__set($name, $this->readSchemaDeeper($data)); |
||
| 177 | } |
||
| 178 | } |
||
| 179 | |||
| 180 | if (isset($schemaArray[self::PATTERN_PROPERTIES])) { |
||
| 181 | foreach ($schemaArray[self::PATTERN_PROPERTIES] as $name => $data) { |
||
| 182 | $schema->patternProperties[$name] = $this->readSchemaDeeper($data); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | if (isset($schemaArray[self::ADDITIONAL_PROPERTIES])) { |
||
| 187 | $additionalProperties = $schemaArray[self::ADDITIONAL_PROPERTIES]; |
||
| 188 | if ($additionalProperties instanceof \stdClass) { |
||
| 189 | $schema->additionalProperties = $this->readSchemaDeeper($additionalProperties); |
||
| 190 | } elseif (is_bool($additionalProperties)) { |
||
| 191 | $schema->additionalProperties = $additionalProperties; |
||
| 192 | } else { |
||
| 193 | throw new InvalidValue('Object or boolean required for additionalProperties', InvalidValue::INVALID_VALUE); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 197 | if (isset($schemaArray[self::REQUIRED])) { |
||
| 198 | $schema->required = $schemaArray[self::REQUIRED]; |
||
| 199 | } |
||
| 200 | |||
| 201 | if (isset($schemaArray[self::DEPENDENCIES])) { |
||
| 202 | foreach ($schemaArray[self::DEPENDENCIES] as $key => $value) { |
||
| 203 | if ($value instanceof \stdClass) { |
||
| 204 | $schema->dependencies[$key] = $this->readSchemaDeeper($value); |
||
| 205 | } else { |
||
| 206 | $schema->dependencies[$key] = $value; |
||
| 207 | } |
||
| 208 | } |
||
| 209 | } |
||
| 210 | |||
| 211 | if (isset($schemaArray[self::MIN_PROPERTIES])) { |
||
| 212 | $schema->minProperties = $schemaArray[self::MIN_PROPERTIES]; |
||
| 213 | } |
||
| 214 | if (isset($schemaArray[self::MAX_PROPERTIES])) { |
||
| 215 | $schema->maxProperties = $schemaArray[self::MAX_PROPERTIES]; |
||
| 216 | } |
||
| 217 | |||
| 218 | |||
| 219 | // Array |
||
| 220 | if (isset($schemaArray[self::ITEMS])) { |
||
| 221 | $items = $schemaArray[self::ITEMS]; |
||
| 222 | if (is_array($items)) { |
||
| 223 | $schema->items = array(); |
||
| 224 | foreach ($items as $item) { |
||
| 225 | $schema->items[] = $this->readSchemaDeeper($item); |
||
| 226 | } |
||
| 227 | } elseif ($items instanceof \stdClass) { |
||
| 228 | $schema->items = $this->readSchemaDeeper($items); |
||
| 229 | } |
||
| 230 | } |
||
| 231 | |||
| 232 | |||
| 233 | if (isset($schemaArray[self::ADDITIONAL_ITEMS])) { |
||
| 234 | $additionalItems = $schemaArray[self::ADDITIONAL_ITEMS]; |
||
| 235 | if ($additionalItems instanceof \stdClass) { |
||
| 236 | $schema->additionalItems = $this->readSchemaDeeper($additionalItems); |
||
| 237 | } else { |
||
| 238 | $schema->additionalItems = $additionalItems; |
||
| 239 | } |
||
| 240 | } |
||
| 241 | |||
| 242 | if (isset($schemaArray[self::UNIQUE_ITEMS]) && $schemaArray[self::UNIQUE_ITEMS] === true) { |
||
| 243 | $schema->uniqueItems = true; |
||
| 244 | } |
||
| 245 | |||
| 246 | if (isset($schemaArray[self::MIN_ITEMS])) { |
||
| 247 | $schema->minItems = $schemaArray[self::MIN_ITEMS]; |
||
| 248 | } |
||
| 249 | |||
| 250 | if (isset($schemaArray[self::MAX_ITEMS])) { |
||
| 251 | $schema->maxItems = $schemaArray[self::MAX_ITEMS]; |
||
| 252 | } |
||
| 253 | |||
| 254 | |||
| 255 | // Number |
||
| 256 | if (isset($schemaArray[self::MINIMUM])) { |
||
| 257 | $schema->minimum = $schemaArray[self::MINIMUM]; |
||
| 258 | } |
||
| 259 | if (isset($schemaArray[self::EXCLUSIVE_MINIMUM])) { |
||
| 260 | $schema->exclusiveMinimum = $schemaArray[self::EXCLUSIVE_MINIMUM]; |
||
| 261 | } |
||
| 262 | if (isset($schemaArray[self::MAXIMUM])) { |
||
| 263 | $schema->maximum = $schemaArray[self::MAXIMUM]; |
||
| 264 | } |
||
| 265 | if (isset($schemaArray[self::EXCLUSIVE_MAXIMUM])) { |
||
| 266 | $schema->exclusiveMaximum = $schemaArray[self::EXCLUSIVE_MAXIMUM]; |
||
| 267 | } |
||
| 268 | if (isset($schemaArray[self::MULTIPLE_OF])) { |
||
| 269 | $schema->multipleOf = $schemaArray[self::MULTIPLE_OF]; |
||
| 270 | } |
||
| 271 | |||
| 272 | |||
| 273 | // String |
||
| 274 | if (isset($schemaArray[self::PATTERN])) { |
||
| 275 | $schema->pattern = $schemaArray[self::PATTERN]; |
||
| 276 | |||
| 277 | } |
||
| 278 | if (isset($schemaArray[self::MIN_LENGTH])) { |
||
| 279 | $schema->minLength = $schemaArray[self::MIN_LENGTH]; |
||
| 280 | } |
||
| 281 | if (isset($schemaArray[self::MAX_LENGTH])) { |
||
| 282 | $schema->maxLength = $schemaArray[self::MAX_LENGTH]; |
||
| 283 | } |
||
| 284 | |||
| 285 | |||
| 286 | // Misc |
||
| 287 | if (isset($schemaArray[self::ENUM])) { |
||
| 288 | $schema->enum = $schemaArray[self::ENUM]; |
||
| 289 | } |
||
| 290 | |||
| 291 | // Logic |
||
| 292 | if (isset($schemaArray[self::ALL_OF])) { |
||
| 293 | foreach ($schemaArray[self::ALL_OF] as $item) { |
||
| 294 | $schema->allOf[] = $this->readSchemaDeeper($item); |
||
| 295 | } |
||
| 296 | } |
||
| 297 | if (isset($schemaArray[self::ANY_OF])) { |
||
| 298 | foreach ($schemaArray[self::ANY_OF] as $item) { |
||
| 299 | $schema->anyOf[] = $this->readSchemaDeeper($item); |
||
| 300 | } |
||
| 301 | } |
||
| 302 | if (isset($schemaArray[self::ONE_OF])) { |
||
| 303 | foreach ($schemaArray[self::ONE_OF] as $item) { |
||
| 304 | $schema->oneOf[] = $this->readSchemaDeeper($item); |
||
| 305 | } |
||
| 306 | } |
||
| 307 | if (isset($schemaArray[self::NOT])) { |
||
| 308 | $schema->not = $this->readSchemaDeeper($schemaArray[self::NOT]); |
||
| 309 | } |
||
| 310 | |||
| 311 | // should resolve references on load |
||
| 312 | if (isset($schemaArray[self::REF])) { |
||
| 313 | $schema->ref = $this->resolveReference($schemaArray[self::REF]); |
||
| 314 | } |
||
| 315 | |||
| 316 | return $schema; |
||
| 317 | } |
||
| 318 | |||
| 380 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: