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 | /** |
||
| 169 | * Checks if the value is valid date. |
||
| 170 | * |
||
| 171 | * @param $name |
||
| 172 | * @param $schema |
||
| 173 | * @param $body |
||
| 174 | * @param $type |
||
| 175 | * |
||
| 176 | * @return bool|null |
||
| 177 | * @throws NotMatchedException |
||
| 178 | */ |
||
| 179 | protected function matchDate($name, $schema, $body, $type) |
||
| 191 | |||
| 192 | protected function matchTypes($name, $schema, $body) |
||
| 242 | |||
| 243 | /** |
||
| 244 | * @param $name |
||
| 245 | * @param $schema |
||
| 246 | * @param $body |
||
| 247 | * @return bool|null |
||
| 248 | * @throws DefinitionNotFoundException |
||
| 249 | * @throws GenericSwaggerException |
||
| 250 | * @throws InvalidDefinitionException |
||
| 251 | * @throws InvalidRequestException |
||
| 252 | * @throws NotMatchedException |
||
| 253 | */ |
||
| 254 | public function matchObjectProperties($name, $schema, $body) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param string $name |
||
| 311 | * @param $schema |
||
| 312 | * @param array $body |
||
| 313 | * @return bool |
||
| 314 | * @throws DefinitionNotFoundException |
||
| 315 | * @throws InvalidDefinitionException |
||
| 316 | * @throws GenericSwaggerException |
||
| 317 | * @throws InvalidRequestException |
||
| 318 | * @throws NotMatchedException |
||
| 319 | */ |
||
| 320 | protected function matchSchema($name, $schema, $body) |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param $name |
||
| 357 | * @param $body |
||
| 358 | * @param $type |
||
| 359 | * @param $nullable |
||
| 360 | * @return bool |
||
| 361 | * @throws NotMatchedException |
||
| 362 | */ |
||
| 363 | protected function matchNull($name, $body, $type, $nullable) |
||
| 378 | } |
||
| 379 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.