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 |
||
| 10 | abstract class SwaggerBody |
||
| 11 | { |
||
| 12 | const SWAGGER_PROPERTIES="properties"; |
||
| 13 | const SWAGGER_REQUIRED="required"; |
||
| 14 | |||
| 15 | /** |
||
| 16 | * @var SwaggerSchema |
||
| 17 | */ |
||
| 18 | protected $swaggerSchema; |
||
| 19 | |||
| 20 | protected $structure; |
||
| 21 | |||
| 22 | protected $name; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * OpenApi 2.0 does not describe null values, so this flag defines, |
||
| 26 | * if match is ok when one of property, which has type, is null |
||
| 27 | * |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | protected $allowNullValues; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * SwaggerRequestBody constructor. |
||
| 34 | * |
||
| 35 | * @param SwaggerSchema $swaggerSchema |
||
| 36 | * @param string $name |
||
| 37 | * @param array $structure |
||
| 38 | * @param bool $allowNullValues |
||
| 39 | */ |
||
| 40 | public function __construct(SwaggerSchema $swaggerSchema, $name, $structure, $allowNullValues = false) |
||
| 50 | |||
| 51 | abstract public function match($body); |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @param $name |
||
| 55 | * @param $schema |
||
| 56 | * @param $body |
||
| 57 | * @param $type |
||
| 58 | * @return bool |
||
| 59 | * @throws NotMatchedException |
||
| 60 | */ |
||
| 61 | protected function matchString($name, $schema, $body, $type) |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @param $name |
||
| 76 | * @param $body |
||
| 77 | * @param $type |
||
| 78 | * @return bool |
||
| 79 | * @throws NotMatchedException |
||
| 80 | */ |
||
| 81 | protected function matchNumber($name, $body, $type) |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @param $name |
||
| 96 | * @param $body |
||
| 97 | * @param $type |
||
| 98 | * @return bool |
||
| 99 | * @throws NotMatchedException |
||
| 100 | */ |
||
| 101 | protected function matchBool($name, $body, $type) |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @param $name |
||
| 116 | * @param $schema |
||
| 117 | * @param $body |
||
| 118 | * @param $type |
||
| 119 | * @return bool |
||
| 120 | * @throws Exception\DefinitionNotFoundException |
||
| 121 | * @throws Exception\InvalidDefinitionException |
||
| 122 | * @throws GenericSwaggerException |
||
| 123 | * @throws InvalidRequestException |
||
| 124 | * @throws NotMatchedException |
||
| 125 | */ |
||
| 126 | protected function matchArray($name, $schema, $body, $type) |
||
| 140 | |||
| 141 | protected function matchTypes($name, $schema, $body) |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @param $name |
||
| 188 | * @param $schema |
||
| 189 | * @param $body |
||
| 190 | * @return bool|null |
||
| 191 | * @throws Exception\DefinitionNotFoundException |
||
| 192 | * @throws Exception\InvalidDefinitionException |
||
| 193 | * @throws GenericSwaggerException |
||
| 194 | * @throws InvalidRequestException |
||
| 195 | * @throws NotMatchedException |
||
| 196 | */ |
||
| 197 | public function matchObjectProperties($name, $schema, $body) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param string $name |
||
| 254 | * @param $schema |
||
| 255 | * @param array $body |
||
| 256 | * @return bool |
||
| 257 | * @throws Exception\DefinitionNotFoundException |
||
| 258 | * @throws Exception\InvalidDefinitionException |
||
| 259 | * @throws GenericSwaggerException |
||
| 260 | * @throws InvalidRequestException |
||
| 261 | * @throws NotMatchedException |
||
| 262 | */ |
||
| 263 | protected function matchSchema($name, $schema, $body) |
||
| 297 | |||
| 298 | /** |
||
| 299 | * @param $name |
||
| 300 | * @param $body |
||
| 301 | * @param $type |
||
| 302 | * @return bool |
||
| 303 | * @throws NotMatchedException |
||
| 304 | */ |
||
| 305 | protected function matchNull($name, $body, $type) |
||
| 320 | } |
||
| 321 |