| Conditions | 19 |
| Paths | 61 |
| Total Lines | 76 |
| Code Lines | 45 |
| 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 |
||
| 95 | protected function matchSchema($name, $schema, $body) |
||
| 96 | { |
||
| 97 | if (isset($schema['type'])) { |
||
| 98 | |||
| 99 | $type = $schema['type']; |
||
| 100 | if (is_null($body)) { |
||
| 101 | return $this->matchNull($name, $type); |
||
| 102 | } |
||
| 103 | |||
| 104 | if ($type == 'string') { |
||
| 105 | return $this->matchString($name, $schema, $body); |
||
| 106 | } |
||
| 107 | |||
| 108 | if ($type == 'integer' || $type == 'float' || $schema['type'] == 'number') { |
||
| 109 | return $this->matchNumber($name, $body); |
||
| 110 | } |
||
| 111 | |||
| 112 | if ($type == 'bool' || $schema['type'] == 'boolean') { |
||
| 113 | return $this->matchBool($name, $body); |
||
| 114 | } |
||
| 115 | |||
| 116 | if ($type == 'array') { |
||
| 117 | return $this->matchArray($name, $schema, $body); |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | if (isset($schema['$ref'])) { |
||
| 122 | $defintion = $this->swaggerSchema->getDefintion($schema['$ref']); |
||
| 123 | return $this->matchSchema($schema['$ref'], $defintion, $body); |
||
| 124 | } |
||
| 125 | |||
| 126 | if (isset($schema['properties'])) { |
||
| 127 | if (!isset($schema['required'])) { |
||
| 128 | $schema['required'] = []; |
||
| 129 | } |
||
| 130 | foreach ($schema['properties'] as $prop => $def) { |
||
| 131 | $required = array_search($prop, $schema['required']); |
||
| 132 | |||
| 133 | if (!array_key_exists($prop, $body)) { |
||
| 134 | if ($required !== false) { |
||
| 135 | throw new NotMatchedException("Required property '$prop' in '$name' not found in object"); |
||
| 136 | } |
||
| 137 | unset($body[$prop]); |
||
| 138 | continue; |
||
| 139 | } |
||
| 140 | |||
| 141 | $this->matchSchema($prop, $def, $body[$prop]); |
||
| 142 | unset($schema['properties'][$prop]); |
||
| 143 | if ($required !== false) { |
||
| 144 | unset($schema['required'][$required]); |
||
| 145 | } |
||
| 146 | unset($body[$prop]); |
||
| 147 | } |
||
| 148 | |||
| 149 | if (count($schema['required']) > 0) { |
||
| 150 | throw new NotMatchedException( |
||
| 151 | "The required property(ies) '" |
||
| 152 | . implode(', ', $schema['required']) |
||
| 153 | . "' does not exists in the body.", |
||
| 154 | $this->structure |
||
| 155 | ); |
||
| 156 | } |
||
| 157 | |||
| 158 | if (count($body) > 0) { |
||
| 159 | throw new NotMatchedException( |
||
| 160 | "The property(ies) '" |
||
| 161 | . implode(', ', array_keys($body)) |
||
| 162 | . "' has not defined in '$name'", |
||
| 163 | $body |
||
| 164 | ); |
||
| 165 | } |
||
| 166 | return true; |
||
| 167 | } |
||
| 168 | |||
| 169 | throw new \Exception("Not all cases are defined. Please open an issue about this. Schema: $name"); |
||
| 170 | } |
||
| 171 | |||
| 190 |