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_REQUIRED="required"; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var Schema |
||
| 23 | */ |
||
| 24 | protected $schema; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $structure; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | protected $name; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * OpenApi 2.0 does not describe null values, so this flag defines, |
||
| 38 | * if match is ok when one of property, which has type, is null |
||
| 39 | * |
||
| 40 | * @var bool |
||
| 41 | */ |
||
| 42 | protected $allowNullValues; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Body constructor. |
||
| 46 | * |
||
| 47 | * @param Schema $schema |
||
| 48 | * @param string $name |
||
| 49 | * @param array $structure |
||
| 50 | * @param bool $allowNullValues |
||
| 51 | */ |
||
| 52 | public function __construct(Schema $schema, $name, $structure, $allowNullValues = false) |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @param Schema $schema |
||
| 65 | * @param string $name |
||
| 66 | * @param array $structure |
||
| 67 | * @param bool $allowNullValues |
||
| 68 | * @return OpenApiResponseBody|SwaggerResponseBody |
||
| 69 | * @throws GenericSwaggerException |
||
| 70 | */ |
||
| 71 | public static function getInstance(Schema $schema, $name, $structure, $allowNullValues = false) |
||
| 83 | |||
| 84 | abstract public function match($body); |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @param string $name |
||
| 88 | * @param array $schemaArray |
||
| 89 | * @param string $body |
||
| 90 | * @param string $type |
||
| 91 | * @return bool |
||
| 92 | * @throws NotMatchedException |
||
| 93 | */ |
||
| 94 | protected function matchString($name, $schemaArray, $body, $type) |
||
| 110 | |||
| 111 | private function checkPattern($name, $body, $pattern) |
||
| 119 | |||
| 120 | |||
| 121 | /** |
||
| 122 | * @param string $name |
||
| 123 | * @param array $schemaArray |
||
| 124 | * @param string $body |
||
| 125 | * @param string $type |
||
| 126 | * @return bool |
||
| 127 | */ |
||
| 128 | protected function matchFile($name, $schemaArray, $body, $type) |
||
| 136 | |||
| 137 | /** |
||
| 138 | * @param string $name |
||
| 139 | * @param string $body |
||
| 140 | * @param string $type |
||
| 141 | * @return bool |
||
| 142 | * @throws NotMatchedException |
||
| 143 | */ |
||
| 144 | protected function matchNumber($name, $body, $type) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param string $name |
||
| 163 | * @param string $body |
||
| 164 | * @param string $type |
||
| 165 | * @return bool |
||
| 166 | * @throws NotMatchedException |
||
| 167 | */ |
||
| 168 | protected function matchBool($name, $body, $type) |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @param string $name |
||
| 183 | * @param array $schemaArray |
||
| 184 | * @param string $body |
||
| 185 | * @param string $type |
||
| 186 | * @return bool |
||
| 187 | * @throws DefinitionNotFoundException |
||
| 188 | * @throws GenericSwaggerException |
||
| 189 | * @throws InvalidDefinitionException |
||
| 190 | * @throws InvalidRequestException |
||
| 191 | * @throws NotMatchedException |
||
| 192 | */ |
||
| 193 | protected function matchArray($name, $schemaArray, $body, $type) |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @param string $name |
||
| 210 | * @param array $schemaArray |
||
| 211 | * @param string $body |
||
| 212 | * @return mixed|null |
||
| 213 | */ |
||
| 214 | protected function matchTypes($name, $schemaArray, $body) |
||
| 264 | |||
| 265 | /** |
||
| 266 | * @param string $name |
||
| 267 | * @param array $schemaArray |
||
| 268 | * @param string $body |
||
| 269 | * @return bool|null |
||
| 270 | * @throws DefinitionNotFoundException |
||
| 271 | * @throws GenericSwaggerException |
||
| 272 | * @throws InvalidDefinitionException |
||
| 273 | * @throws InvalidRequestException |
||
| 274 | * @throws NotMatchedException |
||
| 275 | */ |
||
| 276 | public function matchObjectProperties($name, $schemaArray, $body) |
||
| 330 | |||
| 331 | /** |
||
| 332 | * @param string $name |
||
| 333 | * @param array $schemaArray |
||
| 334 | * @param array $body |
||
| 335 | * @return bool |
||
| 336 | * @throws DefinitionNotFoundException |
||
| 337 | * @throws InvalidDefinitionException |
||
| 338 | * @throws GenericSwaggerException |
||
| 339 | * @throws InvalidRequestException |
||
| 340 | * @throws NotMatchedException |
||
| 341 | */ |
||
| 342 | protected function matchSchema($name, $schemaArray, $body) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @param string $name |
||
| 379 | * @param string $body |
||
| 380 | * @param string $type |
||
| 381 | * @param bool $nullable |
||
| 382 | * @return bool |
||
| 383 | * @throws NotMatchedException |
||
| 384 | */ |
||
| 385 | protected function matchNull($name, $body, $type, $nullable) |
||
| 400 | } |
||
| 401 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.