Complex classes like Parameter 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 Parameter, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare (strict_types=1); |
||
| 34 | class Parameter |
||
| 35 | { |
||
| 36 | use HydratorStrategyTrait; |
||
| 37 | |||
| 38 | const DEFAULT_LOCATION = 'json'; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * The human-friendly name of the parameter. This is what the user will input. |
||
| 42 | * |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $name = ''; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * The alias for this parameter. Although the user will always interact with the human-friendly $name property, |
||
| 49 | * the $sentAs is what's used over the wire. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $sentAs = ''; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * For array parameters (for example, an array of security group names when creating a server), each array element |
||
| 57 | * will need to adhere to a common schema. For the aforementioned example, each element will need to be a string. |
||
| 58 | * For more complicated parameters, you might be validated an array of complicated objects. |
||
| 59 | * |
||
| 60 | * @var Parameter |
||
| 61 | */ |
||
| 62 | private $itemSchema; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * For object parameters, each property will need to adhere to a specific schema. For every property in the |
||
| 66 | * object, it has its own schema - meaning that this property is a hash of name/schema pairs. |
||
| 67 | * |
||
| 68 | * The *only* exception to this rule is for metadata parameters, which are arbitrary key/value pairs. Since it does |
||
| 69 | * not make sense to have a schema for each metadata key, a common schema is use for every one. So instead of this |
||
| 70 | * property being a hash of schemas, it is a single Parameter object instead. This single Parameter schema will |
||
| 71 | * then be applied to each metadata key provided. |
||
| 72 | * |
||
| 73 | * @var []Parameter|Parameter |
||
| 74 | */ |
||
| 75 | private $properties; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * The value's PHP type which this parameter represents; either "string", "bool", "object", "array", "NULL". |
||
| 79 | * |
||
| 80 | * @var string |
||
| 81 | */ |
||
| 82 | private $type = ''; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Indicates whether this parameter requires a value from the user. |
||
| 86 | * |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | private $required; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * The location in the HTTP request where this parameter will populate; either "header", "url", "query", "raw" or |
||
| 93 | * "json". |
||
| 94 | * |
||
| 95 | * @var string |
||
| 96 | */ |
||
| 97 | private $location = ''; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Relevant to "json" location parameters only. This property allows for deep nesting through the use of |
||
| 101 | * {@see OpenCloud\Common\JsonPath}. |
||
| 102 | * |
||
| 103 | * @var string |
||
| 104 | */ |
||
| 105 | private $path = ''; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Allows for the prefixing of parameter names. |
||
| 109 | * |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | private $prefix = ''; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * The enum values for which this param is restricted. |
||
| 116 | * |
||
| 117 | * @var array |
||
| 118 | */ |
||
| 119 | private $enum; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @param array $data |
||
| 123 | */ |
||
| 124 | 220 | public function __construct(array $data) |
|
| 134 | 220 | ||
| 135 | 220 | private function stockLocation(array $data) |
|
| 143 | 220 | ||
| 144 | 1 | private function stockItemSchema(array $data) |
|
| 150 | 220 | ||
| 151 | 29 | private function stockProperties(array $data) |
|
| 163 | 30 | ||
| 164 | /** |
||
| 165 | 42 | * Retrieve the name that will be used over the wire. |
|
| 166 | 220 | * |
|
| 167 | * @return string |
||
| 168 | */ |
||
| 169 | public function getName(): string |
||
| 173 | 97 | ||
| 174 | /** |
||
| 175 | 97 | * Indicates whether the user must provide a value for this parameter. |
|
| 176 | * |
||
| 177 | * @return bool |
||
| 178 | */ |
||
| 179 | public function isRequired(): bool |
||
| 183 | 60 | ||
| 184 | /** |
||
| 185 | 60 | * Validates a given user value and checks whether it passes basic sanity checking, such as types. |
|
| 186 | * |
||
| 187 | * @param $userValues The value provided by the user |
||
| 188 | * |
||
| 189 | * @return bool TRUE if the validation passes |
||
| 190 | * @throws \Exception If validation fails |
||
| 191 | */ |
||
| 192 | public function validate($userValues): bool |
||
| 205 | 22 | ||
| 206 | private function validateEnums($userValues) |
||
| 214 | 1 | ||
| 215 | 1 | private function validateType($userValues) |
|
| 224 | 3 | ||
| 225 | 3 | private function validateArray($userValues) |
|
| 231 | 13 | ||
| 232 | 13 | private function validateObject($userValues) |
|
| 239 | 23 | ||
| 240 | 22 | /** |
|
| 241 | 22 | * Internal method which retrieves a nested property for object parameters. |
|
| 242 | 22 | * |
|
| 243 | * @param string $key The name of the child parameter |
||
| 244 | * |
||
| 245 | * @returns Parameter |
||
| 246 | * @throws \Exception |
||
| 247 | */ |
||
| 248 | private function getNestedProperty($key): Parameter |
||
| 258 | |||
| 259 | 1 | /** |
|
| 260 | * Internal method which indicates whether the user value is of the same type as the one expected |
||
| 261 | * by this parameter. |
||
| 262 | * |
||
| 263 | * @param $userValue The value being checked |
||
| 264 | * |
||
| 265 | * @return bool |
||
| 266 | */ |
||
| 267 | private function hasCorrectType($userValue): bool |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Indicates whether this parameter represents an array type |
||
| 293 | * |
||
| 294 | * @return bool |
||
| 295 | */ |
||
| 296 | 193 | public function isArray(): bool |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Indicates whether this parameter represents an object type |
||
| 303 | * |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | 192 | public function isObject(): bool |
|
| 310 | |||
| 311 | 185 | public function getLocation(): string |
|
| 315 | |||
| 316 | /** |
||
| 317 | * Verifies whether the given location matches the parameter's location. |
||
| 318 | * |
||
| 319 | * @param $value |
||
| 320 | * |
||
| 321 | * @return bool |
||
| 322 | */ |
||
| 323 | 1 | public function hasLocation($value): bool |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Retrieves the parameter's path. |
||
| 330 | * |
||
| 331 | * @return string|null |
||
| 332 | */ |
||
| 333 | 68 | public function getPath(): string |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Retrieves the common schema that an array parameter applies to all its child elements. |
||
| 340 | * |
||
| 341 | * @return Parameter|null |
||
| 342 | */ |
||
| 343 | 20 | public function getItemSchema() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Sets the name of the parameter to a new value |
||
| 350 | * |
||
| 351 | * @param string $name |
||
| 352 | */ |
||
| 353 | 13 | public function setName(string $name) |
|
| 357 | |||
| 358 | /** |
||
| 359 | * Retrieves the child parameter for an object parameter. |
||
| 360 | * |
||
| 361 | * @param string $name The name of the child property |
||
| 362 | * |
||
| 363 | * @return null|Parameter |
||
| 364 | */ |
||
| 365 | 22 | public function getProperty(string $name) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Retrieves the prefix for a parameter, if any. |
||
| 377 | * |
||
| 378 | * @return string|null |
||
| 379 | */ |
||
| 380 | 9 | public function getPrefix(): string |
|
| 384 | |||
| 385 | 16 | public function getPrefixedName(): string |
|
| 389 | } |
||
| 390 |