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 | ||
| 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) | ||
| 115 | |||
| 116 | private function checkPattern($name, $body, $pattern) | ||
| 124 | |||
| 125 | |||
| 126 | /** | ||
| 127 | * @param string $name | ||
| 128 | * @param array $schemaArray | ||
| 129 | * @param string $body | ||
| 130 | * @param string $type | ||
| 131 | * @return bool | ||
| 132 | */ | ||
| 133 | protected function matchFile($name, $schemaArray, $body, $type) | ||
| 141 | |||
| 142 | /** | ||
| 143 | * @param string $name | ||
| 144 | * @param string $body | ||
| 145 | * @param string $type | ||
| 146 | * @return bool | ||
| 147 | * @throws NotMatchedException | ||
| 148 | */ | ||
| 149 | protected function matchNumber($name, $body, $type) | ||
| 165 | |||
| 166 | /** | ||
| 167 | * @param string $name | ||
| 168 | * @param string $body | ||
| 169 | * @param string $type | ||
| 170 | * @return bool | ||
| 171 | * @throws NotMatchedException | ||
| 172 | */ | ||
| 173 | protected function matchBool($name, $body, $type) | ||
| 185 | |||
| 186 | /** | ||
| 187 | * @param string $name | ||
| 188 | * @param array $schemaArray | ||
| 189 | * @param string $body | ||
| 190 | * @param string $type | ||
| 191 | * @return bool | ||
| 192 | * @throws DefinitionNotFoundException | ||
| 193 | * @throws GenericSwaggerException | ||
| 194 | * @throws InvalidDefinitionException | ||
| 195 | * @throws InvalidRequestException | ||
| 196 | * @throws NotMatchedException | ||
| 197 | */ | ||
| 198 | protected function matchArray($name, $schemaArray, $body, $type) | ||
| 212 | |||
| 213 | /** | ||
| 214 | * @param string $name | ||
| 215 | * @param array $schemaArray | ||
| 216 | * @param string $body | ||
| 217 | * @return mixed|null | ||
| 218 | */ | ||
| 219 | protected function matchTypes($name, $schemaArray, $body) | ||
| 269 | |||
| 270 | /** | ||
| 271 | * @param string $name | ||
| 272 | * @param array $schemaArray | ||
| 273 | * @param string $body | ||
| 274 | * @return bool|null | ||
| 275 | * @throws DefinitionNotFoundException | ||
| 276 | * @throws GenericSwaggerException | ||
| 277 | * @throws InvalidDefinitionException | ||
| 278 | * @throws InvalidRequestException | ||
| 279 | * @throws NotMatchedException | ||
| 280 | */ | ||
| 281 | public function matchObjectProperties($name, $schemaArray, $body) | ||
| 344 | |||
| 345 | /** | ||
| 346 | * @param string $name | ||
| 347 | * @param array $schemaArray | ||
| 348 | * @param array $body | ||
| 349 | * @return bool | ||
| 350 | * @throws DefinitionNotFoundException | ||
| 351 | * @throws InvalidDefinitionException | ||
| 352 | * @throws GenericSwaggerException | ||
| 353 | * @throws InvalidRequestException | ||
| 354 | * @throws NotMatchedException | ||
| 355 | */ | ||
| 356 | protected function matchSchema($name, $schemaArray, $body) | ||
| 419 | |||
| 420 | /** | ||
| 421 | * @param string $name | ||
| 422 | * @param string $body | ||
| 423 | * @param string $type | ||
| 424 | * @param bool $nullable | ||
| 425 | * @return bool | ||
| 426 | * @throws NotMatchedException | ||
| 427 | */ | ||
| 428 | protected function matchNull($name, $body, $type, $nullable) | ||
| 443 | } | ||
| 444 | 
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.