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 $schema |
||
| 104 | * @param $body |
||
| 105 | * @param $type |
||
| 106 | * @return bool |
||
| 107 | * @throws NotMatchedException |
||
| 108 | */ |
||
| 109 | protected function matchFile($name, $schema, $body, $type) |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @param $name |
||
| 120 | * @param $body |
||
| 121 | * @param $type |
||
| 122 | * @return bool |
||
| 123 | * @throws NotMatchedException |
||
| 124 | */ |
||
| 125 | protected function matchNumber($name, $body, $type) |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param $name |
||
| 140 | * @param $body |
||
| 141 | * @param $type |
||
| 142 | * @return bool |
||
| 143 | * @throws NotMatchedException |
||
| 144 | */ |
||
| 145 | protected function matchBool($name, $body, $type) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param $name |
||
| 160 | * @param $schema |
||
| 161 | * @param $body |
||
| 162 | * @param $type |
||
| 163 | * @return bool |
||
| 164 | * @throws DefinitionNotFoundException |
||
| 165 | * @throws GenericSwaggerException |
||
| 166 | * @throws InvalidDefinitionException |
||
| 167 | * @throws InvalidRequestException |
||
| 168 | * @throws NotMatchedException |
||
| 169 | */ |
||
| 170 | protected function matchArray($name, $schema, $body, $type) |
||
| 184 | |||
| 185 | protected function matchTypes($name, $schema, $body) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @param $name |
||
| 238 | * @param $schema |
||
| 239 | * @param $body |
||
| 240 | * @return bool|null |
||
| 241 | * @throws DefinitionNotFoundException |
||
| 242 | * @throws GenericSwaggerException |
||
| 243 | * @throws InvalidDefinitionException |
||
| 244 | * @throws InvalidRequestException |
||
| 245 | * @throws NotMatchedException |
||
| 246 | */ |
||
| 247 | public function matchObjectProperties($name, $schema, $body) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param string $name |
||
| 304 | * @param $schema |
||
| 305 | * @param array $body |
||
| 306 | * @return bool |
||
| 307 | * @throws DefinitionNotFoundException |
||
| 308 | * @throws InvalidDefinitionException |
||
| 309 | * @throws GenericSwaggerException |
||
| 310 | * @throws InvalidRequestException |
||
| 311 | * @throws NotMatchedException |
||
| 312 | */ |
||
| 313 | protected function matchSchema($name, $schema, $body) |
||
| 347 | |||
| 348 | /** |
||
| 349 | * @param $name |
||
| 350 | * @param $body |
||
| 351 | * @param $type |
||
| 352 | * @param $nullable |
||
| 353 | * @return bool |
||
| 354 | * @throws NotMatchedException |
||
| 355 | */ |
||
| 356 | protected function matchNull($name, $body, $type, $nullable) |
||
| 371 | } |
||
| 372 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.