| Total Complexity | 67 |
| Total Lines | 422 |
| Duplicated Lines | 0 % |
| Changes | 5 | ||
| Bugs | 0 | Features | 0 |
Complex classes like Body often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Body, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | abstract class Body |
||
| 17 | { |
||
| 18 | const SWAGGER_PROPERTIES="properties"; |
||
| 19 | const SWAGGER_ADDITIONAL_PROPERTIES="additionalProperties"; |
||
| 20 | const SWAGGER_REQUIRED="required"; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var Schema |
||
| 24 | */ |
||
| 25 | protected $schema; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var array |
||
| 29 | */ |
||
| 30 | protected $structure; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $name; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * OpenApi 2.0 does not describe null values, so this flag defines, |
||
| 39 | * if match is ok when one of property, which has type, is null |
||
| 40 | * |
||
| 41 | * @var bool |
||
| 42 | */ |
||
| 43 | protected $allowNullValues; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Body constructor. |
||
| 47 | * |
||
| 48 | * @param Schema $schema |
||
| 49 | * @param string $name |
||
| 50 | * @param array $structure |
||
| 51 | * @param bool $allowNullValues |
||
| 52 | */ |
||
| 53 | public function __construct(Schema $schema, $name, $structure, $allowNullValues = false) |
||
| 54 | { |
||
| 55 | $this->schema = $schema; |
||
| 56 | $this->name = $name; |
||
| 57 | if (!is_array($structure)) { |
||
|
|
|||
| 58 | throw new InvalidArgumentException('I expected the structure to be an array'); |
||
| 59 | } |
||
| 60 | $this->structure = $structure; |
||
| 61 | $this->allowNullValues = $allowNullValues; |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @param Schema $schema |
||
| 66 | * @param string $name |
||
| 67 | * @param array $structure |
||
| 68 | * @param bool $allowNullValues |
||
| 69 | * @return OpenApiResponseBody|SwaggerResponseBody |
||
| 70 | * @throws GenericSwaggerException |
||
| 71 | */ |
||
| 72 | public static function getInstance(Schema $schema, $name, $structure, $allowNullValues = false) |
||
| 73 | { |
||
| 74 | if ($schema instanceof SwaggerSchema) { |
||
| 75 | return new SwaggerResponseBody($schema, $name, $structure, $allowNullValues); |
||
| 76 | } |
||
| 77 | |||
| 78 | if ($schema instanceof OpenApiSchema) { |
||
| 79 | return new OpenApiResponseBody($schema, $name, $structure, $allowNullValues); |
||
| 80 | } |
||
| 81 | |||
| 82 | throw new GenericSwaggerException("Cannot get instance SwaggerBody or SchemaBody from " . get_class($schema)); |
||
| 83 | } |
||
| 84 | |||
| 85 | abstract public function match($body); |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @param string $name |
||
| 89 | * @param array $schemaArray |
||
| 90 | * @param string $body |
||
| 91 | * @param string $type |
||
| 92 | * @return bool |
||
| 93 | * @throws NotMatchedException |
||
| 94 | */ |
||
| 95 | protected function matchString($name, $schemaArray, $body, $type) |
||
| 96 | { |
||
| 97 | if ($type !== 'string') { |
||
| 98 | return null; |
||
| 99 | } |
||
| 100 | |||
| 101 | if (isset($schemaArray['enum']) && !in_array($body, $schemaArray['enum'])) { |
||
| 102 | throw new NotMatchedException("Value '$body' in '$name' not matched in ENUM. ", $this->structure); |
||
| 103 | } |
||
| 104 | |||
| 105 | if (isset($schemaArray['pattern'])) { |
||
| 106 | $this->checkPattern($name, $body, $schemaArray['pattern']); |
||
| 107 | } |
||
| 108 | |||
| 109 | return true; |
||
| 110 | } |
||
| 111 | |||
| 112 | private function checkPattern($name, $body, $pattern) |
||
| 118 | } |
||
| 119 | } |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * @param string $name |
||
| 124 | * @param array $schemaArray |
||
| 125 | * @param string $body |
||
| 126 | * @param string $type |
||
| 127 | * @return bool |
||
| 128 | */ |
||
| 129 | protected function matchFile($name, $schemaArray, $body, $type) |
||
| 130 | { |
||
| 131 | if ($type !== 'file') { |
||
| 132 | return null; |
||
| 133 | } |
||
| 134 | |||
| 135 | return true; |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param string $name |
||
| 140 | * @param string $body |
||
| 141 | * @param string $type |
||
| 142 | * @return bool |
||
| 143 | * @throws NotMatchedException |
||
| 144 | */ |
||
| 145 | protected function matchNumber($name, $body, $type) |
||
| 146 | { |
||
| 147 | if ($type !== 'integer' && $type !== 'float' && $type !== 'number') { |
||
| 148 | return null; |
||
| 149 | } |
||
| 150 | |||
| 151 | if (!is_numeric($body)) { |
||
| 152 | throw new NotMatchedException("Expected '$name' to be numeric, but found '$body'. ", $this->structure); |
||
| 153 | } |
||
| 154 | |||
| 155 | if (isset($schemaArray['pattern'])) { |
||
| 156 | $this->checkPattern($name, $body, $schemaArray['pattern']); |
||
| 157 | } |
||
| 158 | |||
| 159 | return true; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @param string $name |
||
| 164 | * @param string $body |
||
| 165 | * @param string $type |
||
| 166 | * @return bool |
||
| 167 | * @throws NotMatchedException |
||
| 168 | */ |
||
| 169 | protected function matchBool($name, $body, $type) |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @param string $name |
||
| 184 | * @param array $schemaArray |
||
| 185 | * @param string $body |
||
| 186 | * @param string $type |
||
| 187 | * @return bool |
||
| 188 | * @throws DefinitionNotFoundException |
||
| 189 | * @throws GenericSwaggerException |
||
| 190 | * @throws InvalidDefinitionException |
||
| 191 | * @throws InvalidRequestException |
||
| 192 | * @throws NotMatchedException |
||
| 193 | */ |
||
| 194 | protected function matchArray($name, $schemaArray, $body, $type) |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $name |
||
| 211 | * @param array $schemaArray |
||
| 212 | * @param string $body |
||
| 213 | * @return mixed|null |
||
| 214 | */ |
||
| 215 | protected function matchTypes($name, $schemaArray, $body) |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * @param string $name |
||
| 268 | * @param array $schemaArray |
||
| 269 | * @param string $body |
||
| 270 | * @return bool|null |
||
| 271 | * @throws DefinitionNotFoundException |
||
| 272 | * @throws GenericSwaggerException |
||
| 273 | * @throws InvalidDefinitionException |
||
| 274 | * @throws InvalidRequestException |
||
| 275 | * @throws NotMatchedException |
||
| 276 | */ |
||
| 277 | public function matchObjectProperties($name, $schemaArray, $body) |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param string $name |
||
| 343 | * @param array $schemaArray |
||
| 344 | * @param array $body |
||
| 345 | * @return bool |
||
| 346 | * @throws DefinitionNotFoundException |
||
| 347 | * @throws InvalidDefinitionException |
||
| 348 | * @throws GenericSwaggerException |
||
| 349 | * @throws InvalidRequestException |
||
| 350 | * @throws NotMatchedException |
||
| 351 | */ |
||
| 352 | protected function matchSchema($name, $schemaArray, $body) |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * @param string $name |
||
| 418 | * @param string $body |
||
| 419 | * @param string $type |
||
| 420 | * @param bool $nullable |
||
| 421 | * @return bool |
||
| 422 | * @throws NotMatchedException |
||
| 423 | */ |
||
| 424 | protected function matchNull($name, $body, $type, $nullable) |
||
| 440 |