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 | protected $structure; |
||
| 27 | |||
| 28 | protected $name; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * OpenApi 2.0 does not describe null values, so this flag defines, |
||
| 32 | * if match is ok when one of property, which has type, is null |
||
| 33 | * |
||
| 34 | * @var bool |
||
| 35 | */ |
||
| 36 | protected $allowNullValues; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Body constructor. |
||
| 40 | * |
||
| 41 | * @param Schema $schema |
||
| 42 | * @param string $name |
||
| 43 | * @param array $structure |
||
| 44 | * @param bool $allowNullValues |
||
| 45 | */ |
||
| 46 | public function __construct(Schema $schema, $name, $structure, $allowNullValues = false) |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @param Schema $schema |
||
| 59 | * @param $name |
||
| 60 | * @param $structure |
||
| 61 | * @param bool $allowNullValues |
||
| 62 | * @return OpenApiResponseBody|SwaggerResponseBody |
||
| 63 | * @throws GenericSwaggerException |
||
| 64 | */ |
||
| 65 | public static function getInstance(Schema $schema, $name, $structure, $allowNullValues = false) |
||
| 77 | |||
| 78 | abstract public function match($body); |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @param $name |
||
| 82 | * @param $schema |
||
| 83 | * @param $body |
||
| 84 | * @param $type |
||
| 85 | * @return bool |
||
| 86 | * @throws NotMatchedException |
||
| 87 | */ |
||
| 88 | protected function matchString($name, $schema, $body, $type) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @param $name |
||
| 103 | * @param $body |
||
| 104 | * @param $type |
||
| 105 | * @return bool |
||
| 106 | * @throws NotMatchedException |
||
| 107 | */ |
||
| 108 | protected function matchNumber($name, $body, $type) |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param $name |
||
| 123 | * @param $body |
||
| 124 | * @param $type |
||
| 125 | * @return bool |
||
| 126 | * @throws NotMatchedException |
||
| 127 | */ |
||
| 128 | protected function matchBool($name, $body, $type) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @param $name |
||
| 143 | * @param $schema |
||
| 144 | * @param $body |
||
| 145 | * @param $type |
||
| 146 | * @return bool |
||
| 147 | * @throws DefinitionNotFoundException |
||
| 148 | * @throws GenericSwaggerException |
||
| 149 | * @throws InvalidDefinitionException |
||
| 150 | * @throws InvalidRequestException |
||
| 151 | * @throws NotMatchedException |
||
| 152 | */ |
||
| 153 | protected function matchArray($name, $schema, $body, $type) |
||
| 167 | |||
| 168 | protected function matchTypes($name, $schema, $body) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param $name |
||
| 216 | * @param $schema |
||
| 217 | * @param $body |
||
| 218 | * @return bool|null |
||
| 219 | * @throws DefinitionNotFoundException |
||
| 220 | * @throws GenericSwaggerException |
||
| 221 | * @throws InvalidDefinitionException |
||
| 222 | * @throws InvalidRequestException |
||
| 223 | * @throws NotMatchedException |
||
| 224 | */ |
||
| 225 | public function matchObjectProperties($name, $schema, $body) |
||
| 279 | |||
| 280 | /** |
||
| 281 | * @param string $name |
||
| 282 | * @param $schema |
||
| 283 | * @param array $body |
||
| 284 | * @return bool |
||
| 285 | * @throws DefinitionNotFoundException |
||
| 286 | * @throws InvalidDefinitionException |
||
| 287 | * @throws GenericSwaggerException |
||
| 288 | * @throws InvalidRequestException |
||
| 289 | * @throws NotMatchedException |
||
| 290 | */ |
||
| 291 | protected function matchSchema($name, $schema, $body) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @param $name |
||
| 328 | * @param $body |
||
| 329 | * @param $type |
||
| 330 | * @param $nullable |
||
| 331 | * @return bool |
||
| 332 | * @throws NotMatchedException |
||
| 333 | */ |
||
| 334 | protected function matchNull($name, $body, $type, $nullable) |
||
| 349 | } |
||
| 350 |