Complex classes like SwaggerBody 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. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
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 SwaggerBody, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | abstract class SwaggerBody |
||
| 10 | { |
||
| 11 | const SWAGGER_PROPERTIES="properties"; |
||
| 12 | const SWAGGER_REQUIRED="required"; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * @var \ByJG\Swagger\SwaggerSchema |
||
| 16 | */ |
||
| 17 | protected $swaggerSchema; |
||
| 18 | |||
| 19 | protected $structure; |
||
| 20 | |||
| 21 | protected $name; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * OpenApi 2.0 does not describe null values, so this flag defines, |
||
| 25 | * if match is ok when one of property, which has type, is null |
||
| 26 | * |
||
| 27 | * @var bool |
||
| 28 | */ |
||
| 29 | protected $allowNullValues; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * SwaggerRequestBody constructor. |
||
| 33 | * |
||
| 34 | * @param \ByJG\Swagger\SwaggerSchema $swaggerSchema |
||
| 35 | * @param string $name |
||
| 36 | * @param array $structure |
||
| 37 | * @param bool $allowNullValues |
||
| 38 | */ |
||
| 39 | public function __construct(SwaggerSchema $swaggerSchema, $name, $structure, $allowNullValues = false) |
||
| 40 | { |
||
| 41 | $this->swaggerSchema = $swaggerSchema; |
||
| 42 | $this->name = $name; |
||
| 43 | if (!is_array($structure)) { |
||
| 44 | throw new \InvalidArgumentException('I expected the structure to be an array'); |
||
| 45 | } |
||
| 46 | $this->structure = $structure; |
||
| 47 | $this->allowNullValues = $allowNullValues; |
||
| 48 | } |
||
| 49 | |||
| 50 | abstract public function match($body); |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param $name |
||
| 54 | * @param $schema |
||
| 55 | * @param $body |
||
| 56 | * @param $type |
||
| 57 | * @return bool |
||
| 58 | * @throws NotMatchedException |
||
| 59 | */ |
||
| 60 | protected function matchString($name, $schema, $body, $type) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @param $name |
||
| 75 | * @param $body |
||
| 76 | * @param $type |
||
| 77 | * @return bool |
||
| 78 | * @throws NotMatchedException |
||
| 79 | */ |
||
| 80 | protected function matchNumber($name, $body, $type) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @param $name |
||
| 95 | * @param $body |
||
| 96 | * @param $type |
||
| 97 | * @return bool |
||
| 98 | * @throws NotMatchedException |
||
| 99 | */ |
||
| 100 | protected function matchBool($name, $body, $type) |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param $name |
||
| 115 | * @param $schema |
||
| 116 | * @param $body |
||
| 117 | * @param $type |
||
| 118 | * @return bool |
||
| 119 | * @throws Exception\DefinitionNotFoundException |
||
| 120 | * @throws Exception\InvalidDefinitionException |
||
| 121 | * @throws GenericSwaggerException |
||
| 122 | * @throws InvalidRequestException |
||
| 123 | * @throws NotMatchedException |
||
| 124 | */ |
||
| 125 | protected function matchArray($name, $schema, $body, $type) |
||
| 139 | |||
| 140 | protected function matchTypes($name, $schema, $body) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @param $name |
||
| 187 | * @param $schema |
||
| 188 | * @param $body |
||
| 189 | * @return bool|null |
||
| 190 | * @throws Exception\DefinitionNotFoundException |
||
| 191 | * @throws Exception\InvalidDefinitionException |
||
| 192 | * @throws GenericSwaggerException |
||
| 193 | * @throws InvalidRequestException |
||
| 194 | * @throws NotMatchedException |
||
| 195 | */ |
||
| 196 | public function matchObjectProperties($name, $schema, $body) |
||
| 250 | |||
| 251 | /** |
||
| 252 | * @param string $name |
||
| 253 | * @param $schema |
||
| 254 | * @param array $body |
||
| 255 | * @return bool |
||
| 256 | * @throws Exception\DefinitionNotFoundException |
||
| 257 | * @throws Exception\InvalidDefinitionException |
||
| 258 | * @throws GenericSwaggerException |
||
| 259 | * @throws InvalidRequestException |
||
| 260 | * @throws NotMatchedException |
||
| 261 | */ |
||
| 262 | protected function matchSchema($name, $schema, $body) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param $name |
||
| 299 | * @param $body |
||
| 300 | * @param $type |
||
| 301 | * @return bool |
||
| 302 | * @throws NotMatchedException |
||
| 303 | */ |
||
| 304 | protected function matchNull($name, $body, $type) |
||
| 319 | } |
||
| 320 |