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. 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 Body, and based on these observations, apply Extract Interface, too.
| 1 | <?php | ||
| 12 | abstract class Body | ||
| 13 | { | ||
| 14 | const SWAGGER_PROPERTIES="properties"; | ||
| 15 | const SWAGGER_REQUIRED="required"; | ||
| 16 | |||
| 17 | /** | ||
| 18 | * @var Schema | ||
| 19 | */ | ||
| 20 | protected $swaggerSchema; | ||
| 21 | |||
| 22 | protected $structure; | ||
| 23 | |||
| 24 | protected $name; | ||
| 25 | |||
| 26 | /** | ||
| 27 | * OpenApi 2.0 does not describe null values, so this flag defines, | ||
| 28 | * if match is ok when one of property, which has type, is null | ||
| 29 | * | ||
| 30 | * @var bool | ||
| 31 | */ | ||
| 32 | protected $allowNullValues; | ||
| 33 | |||
| 34 | /** | ||
| 35 | * SwaggerRequestBody constructor. | ||
| 36 | * | ||
| 37 | * @param Schema $swaggerSchema | ||
| 38 | * @param string $name | ||
| 39 | * @param array $structure | ||
| 40 | * @param bool $allowNullValues | ||
| 41 | */ | ||
| 42 | public function __construct(Schema $swaggerSchema, $name, $structure, $allowNullValues = false) | ||
| 52 | |||
| 53 | abstract public function match($body); | ||
| 54 | |||
| 55 | /** | ||
| 56 | * @param $name | ||
| 57 | * @param $schema | ||
| 58 | * @param $body | ||
| 59 | * @param $type | ||
| 60 | * @return bool | ||
| 61 | * @throws NotMatchedException | ||
| 62 | */ | ||
| 63 | protected function matchString($name, $schema, $body, $type) | ||
| 75 | |||
| 76 | /** | ||
| 77 | * @param $name | ||
| 78 | * @param $body | ||
| 79 | * @param $type | ||
| 80 | * @return bool | ||
| 81 | * @throws NotMatchedException | ||
| 82 | */ | ||
| 83 | protected function matchNumber($name, $body, $type) | ||
| 95 | |||
| 96 | /** | ||
| 97 | * @param $name | ||
| 98 | * @param $body | ||
| 99 | * @param $type | ||
| 100 | * @return bool | ||
| 101 | * @throws NotMatchedException | ||
| 102 | */ | ||
| 103 | protected function matchBool($name, $body, $type) | ||
| 115 | |||
| 116 | /** | ||
| 117 | * @param $name | ||
| 118 | * @param $schema | ||
| 119 | * @param $body | ||
| 120 | * @param $type | ||
| 121 | * @return bool | ||
| 122 | * @throws DefinitionNotFoundException | ||
| 123 | * @throws GenericSwaggerException | ||
| 124 | * @throws InvalidDefinitionException | ||
| 125 | * @throws InvalidRequestException | ||
| 126 | * @throws NotMatchedException | ||
| 127 | */ | ||
| 128 | protected function matchArray($name, $schema, $body, $type) | ||
| 142 | |||
| 143 | protected function matchTypes($name, $schema, $body) | ||
| 188 | |||
| 189 | /** | ||
| 190 | * @param $name | ||
| 191 | * @param $schema | ||
| 192 | * @param $body | ||
| 193 | * @return bool|null | ||
| 194 | * @throws DefinitionNotFoundException | ||
| 195 | * @throws GenericSwaggerException | ||
| 196 | * @throws InvalidDefinitionException | ||
| 197 | * @throws InvalidRequestException | ||
| 198 | * @throws NotMatchedException | ||
| 199 | */ | ||
| 200 | public function matchObjectProperties($name, $schema, $body) | ||
| 254 | |||
| 255 | /** | ||
| 256 | * @param string $name | ||
| 257 | * @param $schema | ||
| 258 | * @param array $body | ||
| 259 | * @return bool | ||
| 260 | * @throws DefinitionNotFoundException | ||
| 261 | * @throws InvalidDefinitionException | ||
| 262 | * @throws GenericSwaggerException | ||
| 263 | * @throws InvalidRequestException | ||
| 264 | * @throws NotMatchedException | ||
| 265 | */ | ||
| 266 | protected function matchSchema($name, $schema, $body) | ||
| 300 | |||
| 301 | /** | ||
| 302 | * @param $name | ||
| 303 | * @param $body | ||
| 304 | * @param $type | ||
| 305 | * @param $nullable | ||
| 306 | * @return bool | ||
| 307 | * @throws NotMatchedException | ||
| 308 | */ | ||
| 309 | protected function matchNull($name, $body, $type, $nullable) | ||
| 324 | } | ||
| 325 |