Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ClientResolver 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 ClientResolver, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class ClientResolver |
||
| 23 | { |
||
| 24 | /** @var array */ |
||
| 25 | private $argDefinitions; |
||
| 26 | |||
| 27 | /** @var array Map of types to a corresponding function */ |
||
| 28 | private static $typeMap = [ |
||
| 29 | 'resource' => 'is_resource', |
||
| 30 | 'callable' => 'is_callable', |
||
| 31 | 'int' => 'is_int', |
||
| 32 | 'bool' => 'is_bool', |
||
| 33 | 'string' => 'is_string', |
||
| 34 | 'object' => 'is_object', |
||
| 35 | 'array' => 'is_array', |
||
| 36 | ]; |
||
| 37 | |||
| 38 | private static $defaultArgs = [ |
||
| 39 | 'service' => [ |
||
| 40 | 'type' => 'value', |
||
| 41 | 'valid' => ['string'], |
||
| 42 | 'doc' => 'Name of the service to utilize. This value will be supplied by default when using one of the SDK clients (e.g., Aws\\S3\\S3Client).', |
||
| 43 | 'required' => true, |
||
| 44 | 'internal' => true |
||
| 45 | ], |
||
| 46 | 'exception_class' => [ |
||
| 47 | 'type' => 'value', |
||
| 48 | 'valid' => ['string'], |
||
| 49 | 'doc' => 'Exception class to create when an error occurs.', |
||
| 50 | 'default' => 'Aws\Exception\AwsException', |
||
| 51 | 'internal' => true |
||
| 52 | ], |
||
| 53 | 'scheme' => [ |
||
| 54 | 'type' => 'value', |
||
| 55 | 'valid' => ['string'], |
||
| 56 | 'default' => 'https', |
||
| 57 | 'doc' => 'URI scheme to use when connecting connect. The SDK will utilize "https" endpoints (i.e., utilize SSL/TLS connections) by default. You can attempt to connect to a service over an unencrypted "http" endpoint by setting ``scheme`` to "http".', |
||
| 58 | ], |
||
| 59 | 'endpoint' => [ |
||
| 60 | 'type' => 'value', |
||
| 61 | 'valid' => ['string'], |
||
| 62 | 'doc' => 'The full URI of the webservice. This is only required when connecting to a custom endpoint (e.g., a local version of S3).', |
||
| 63 | 'fn' => [__CLASS__, '_apply_endpoint'], |
||
| 64 | ], |
||
| 65 | 'region' => [ |
||
| 66 | 'type' => 'value', |
||
| 67 | 'valid' => ['string'], |
||
| 68 | 'required' => [__CLASS__, '_missing_region'], |
||
| 69 | 'doc' => 'Region to connect to. See http://docs.aws.amazon.com/general/latest/gr/rande.html for a list of available regions.', |
||
| 70 | ], |
||
| 71 | 'version' => [ |
||
| 72 | 'type' => 'value', |
||
| 73 | 'valid' => ['string'], |
||
| 74 | 'required' => [__CLASS__, '_missing_version'], |
||
| 75 | 'doc' => 'The version of the webservice to utilize (e.g., 2006-03-01).', |
||
| 76 | ], |
||
| 77 | 'signature_provider' => [ |
||
| 78 | 'type' => 'value', |
||
| 79 | 'valid' => ['callable'], |
||
| 80 | 'doc' => 'A callable that accepts a signature version name (e.g., "v4"), a service name, and region, and returns a SignatureInterface object or null. This provider is used to create signers utilized by the client. See Aws\\Signature\\SignatureProvider for a list of built-in providers', |
||
| 81 | 'default' => [__CLASS__, '_default_signature_provider'], |
||
| 82 | ], |
||
| 83 | 'api_provider' => [ |
||
| 84 | 'type' => 'value', |
||
| 85 | 'valid' => ['callable'], |
||
| 86 | 'doc' => 'An optional PHP callable that accepts a type, service, and version argument, and returns an array of corresponding configuration data. The type value can be one of api, waiter, or paginator.', |
||
| 87 | 'fn' => [__CLASS__, '_apply_api_provider'], |
||
| 88 | 'default' => [ApiProvider::class, 'defaultProvider'], |
||
| 89 | ], |
||
| 90 | 'endpoint_provider' => [ |
||
| 91 | 'type' => 'value', |
||
| 92 | 'valid' => ['callable'], |
||
| 93 | 'fn' => [__CLASS__, '_apply_endpoint_provider'], |
||
| 94 | 'doc' => 'An optional PHP callable that accepts a hash of options including a "service" and "region" key and returns NULL or a hash of endpoint data, of which the "endpoint" key is required. See Aws\\Endpoint\\EndpointProvider for a list of built-in providers.', |
||
| 95 | 'default' => [__CLASS__, '_default_endpoint_provider'], |
||
| 96 | ], |
||
| 97 | 'serializer' => [ |
||
| 98 | 'default' => [__CLASS__, '_default_serializer'], |
||
| 99 | 'fn' => [__CLASS__, '_apply_serializer'], |
||
| 100 | 'internal' => true, |
||
| 101 | 'type' => 'value', |
||
| 102 | 'valid' => ['callable'], |
||
| 103 | ], |
||
| 104 | 'signature_version' => [ |
||
| 105 | 'type' => 'config', |
||
| 106 | 'valid' => ['string'], |
||
| 107 | 'doc' => 'A string representing a custom signature version to use with a service (e.g., v4). Note that per/operation signature version MAY override this requested signature version.', |
||
| 108 | 'default' => [__CLASS__, '_default_signature_version'], |
||
| 109 | ], |
||
| 110 | 'signing_name' => [ |
||
| 111 | 'type' => 'config', |
||
| 112 | 'valid' => ['string'], |
||
| 113 | 'doc' => 'A string representing a custom service name to be used when calculating a request signature.', |
||
| 114 | 'default' => [__CLASS__, '_default_signing_name'], |
||
| 115 | ], |
||
| 116 | 'signing_region' => [ |
||
| 117 | 'type' => 'config', |
||
| 118 | 'valid' => ['string'], |
||
| 119 | 'doc' => 'A string representing a custom region name to be used when calculating a request signature.', |
||
| 120 | 'default' => [__CLASS__, '_default_signing_region'], |
||
| 121 | ], |
||
| 122 | 'profile' => [ |
||
| 123 | 'type' => 'config', |
||
| 124 | 'valid' => ['string'], |
||
| 125 | 'doc' => 'Allows you to specify which profile to use when credentials are created from the AWS credentials file in your HOME directory. This setting overrides the AWS_PROFILE environment variable. Note: Specifying "profile" will cause the "credentials" key to be ignored.', |
||
| 126 | 'fn' => [__CLASS__, '_apply_profile'], |
||
| 127 | ], |
||
| 128 | 'credentials' => [ |
||
| 129 | 'type' => 'value', |
||
| 130 | 'valid' => [CredentialsInterface::class, CacheInterface::class, 'array', 'bool', 'callable'], |
||
| 131 | 'doc' => 'Specifies the credentials used to sign requests. Provide an Aws\Credentials\CredentialsInterface object, an associative array of "key", "secret", and an optional "token" key, `false` to use null credentials, or a callable credentials provider used to create credentials or return null. See Aws\\Credentials\\CredentialProvider for a list of built-in credentials providers. If no credentials are provided, the SDK will attempt to load them from the environment.', |
||
| 132 | 'fn' => [__CLASS__, '_apply_credentials'], |
||
| 133 | 'default' => [CredentialProvider::class, 'defaultProvider'], |
||
| 134 | ], |
||
| 135 | 'stats' => [ |
||
| 136 | 'type' => 'value', |
||
| 137 | 'valid' => ['bool', 'array'], |
||
| 138 | 'default' => false, |
||
| 139 | 'doc' => 'Set to true to gather transfer statistics on requests sent. Alternatively, you can provide an associative array with the following keys: retries: (bool) Set to false to disable reporting on retries attempted; http: (bool) Set to true to enable collecting statistics from lower level HTTP adapters (e.g., values returned in GuzzleHttp\TransferStats). HTTP handlers must support an http_stats_receiver option for this to have an effect; timer: (bool) Set to true to enable a command timer that reports the total wall clock time spent on an operation in seconds.', |
||
| 140 | 'fn' => [__CLASS__, '_apply_stats'], |
||
| 141 | ], |
||
| 142 | 'retries' => [ |
||
| 143 | 'type' => 'value', |
||
| 144 | 'valid' => ['int'], |
||
| 145 | 'doc' => 'Configures the maximum number of allowed retries for a client (pass 0 to disable retries). ', |
||
| 146 | 'fn' => [__CLASS__, '_apply_retries'], |
||
| 147 | 'default' => 3, |
||
| 148 | ], |
||
| 149 | 'validate' => [ |
||
| 150 | 'type' => 'value', |
||
| 151 | 'valid' => ['bool', 'array'], |
||
| 152 | 'default' => true, |
||
| 153 | 'doc' => 'Set to false to disable client-side parameter validation. Set to true to utilize default validation constraints. Set to an associative array of validation options to enable specific validation constraints.', |
||
| 154 | 'fn' => [__CLASS__, '_apply_validate'], |
||
| 155 | ], |
||
| 156 | 'debug' => [ |
||
| 157 | 'type' => 'value', |
||
| 158 | 'valid' => ['bool', 'array'], |
||
| 159 | 'doc' => 'Set to true to display debug information when sending requests. Alternatively, you can provide an associative array with the following keys: logfn: (callable) Function that is invoked with log messages; stream_size: (int) When the size of a stream is greater than this number, the stream data will not be logged (set to "0" to not log any stream data); scrub_auth: (bool) Set to false to disable the scrubbing of auth data from the logged messages; http: (bool) Set to false to disable the "debug" feature of lower level HTTP adapters (e.g., verbose curl output).', |
||
| 160 | 'fn' => [__CLASS__, '_apply_debug'], |
||
| 161 | ], |
||
| 162 | 'http' => [ |
||
| 163 | 'type' => 'value', |
||
| 164 | 'valid' => ['array'], |
||
| 165 | 'default' => [], |
||
| 166 | 'doc' => 'Set to an array of SDK request options to apply to each request (e.g., proxy, verify, etc.).', |
||
| 167 | ], |
||
| 168 | 'http_handler' => [ |
||
| 169 | 'type' => 'value', |
||
| 170 | 'valid' => ['callable'], |
||
| 171 | 'doc' => 'An HTTP handler is a function that accepts a PSR-7 request object and returns a promise that is fulfilled with a PSR-7 response object or rejected with an array of exception data. NOTE: This option supersedes any provided "handler" option.', |
||
| 172 | 'fn' => [__CLASS__, '_apply_http_handler'] |
||
| 173 | ], |
||
| 174 | 'handler' => [ |
||
| 175 | 'type' => 'value', |
||
| 176 | 'valid' => ['callable'], |
||
| 177 | 'doc' => 'A handler that accepts a command object, request object and returns a promise that is fulfilled with an Aws\ResultInterface object or rejected with an Aws\Exception\AwsException. A handler does not accept a next handler as it is terminal and expected to fulfill a command. If no handler is provided, a default Guzzle handler will be utilized.', |
||
| 178 | 'fn' => [__CLASS__, '_apply_handler'], |
||
| 179 | 'default' => [__CLASS__, '_default_handler'] |
||
| 180 | ], |
||
| 181 | 'ua_append' => [ |
||
| 182 | 'type' => 'value', |
||
| 183 | 'valid' => ['string', 'array'], |
||
| 184 | 'doc' => 'Provide a string or array of strings to send in the User-Agent header.', |
||
| 185 | 'fn' => [__CLASS__, '_apply_user_agent'], |
||
| 186 | 'default' => [], |
||
| 187 | ], |
||
| 188 | 'idempotency_auto_fill' => [ |
||
| 189 | 'type' => 'value', |
||
| 190 | 'valid' => ['bool', 'callable'], |
||
| 191 | 'doc' => 'Set to false to disable SDK to populate parameters that enabled \'idempotencyToken\' trait with a random UUID v4 value on your behalf. Using default value \'true\' still allows parameter value to be overwritten when provided. Note: auto-fill only works when cryptographically secure random bytes generator functions(random_bytes, openssl_random_pseudo_bytes or mcrypt_create_iv) can be found. You may also provide a callable source of random bytes.', |
||
| 192 | 'default' => true, |
||
| 193 | 'fn' => [__CLASS__, '_apply_idempotency_auto_fill'] |
||
| 194 | ], |
||
| 195 | ]; |
||
| 196 | |||
| 197 | /** |
||
| 198 | * Gets an array of default client arguments, each argument containing a |
||
| 199 | * hash of the following: |
||
| 200 | * |
||
| 201 | * - type: (string, required) option type described as follows: |
||
| 202 | * - value: The default option type. |
||
| 203 | * - config: The provided value is made available in the client's |
||
| 204 | * getConfig() method. |
||
| 205 | * - valid: (array, required) Valid PHP types or class names. Note: null |
||
| 206 | * is not an allowed type. |
||
| 207 | * - required: (bool, callable) Whether or not the argument is required. |
||
| 208 | * Provide a function that accepts an array of arguments and returns a |
||
| 209 | * string to provide a custom error message. |
||
| 210 | * - default: (mixed) The default value of the argument if not provided. If |
||
| 211 | * a function is provided, then it will be invoked to provide a default |
||
| 212 | * value. The function is provided the array of options and is expected |
||
| 213 | * to return the default value of the option. The default value can be a |
||
| 214 | * closure and can not be a callable string that is not part of the |
||
| 215 | * defaultArgs array. |
||
| 216 | * - doc: (string) The argument documentation string. |
||
| 217 | * - fn: (callable) Function used to apply the argument. The function |
||
| 218 | * accepts the provided value, array of arguments by reference, and an |
||
| 219 | * event emitter. |
||
| 220 | * |
||
| 221 | * Note: Order is honored and important when applying arguments. |
||
| 222 | * |
||
| 223 | * @return array |
||
| 224 | */ |
||
| 225 | public static function getDefaultArguments() |
||
| 229 | |||
| 230 | /** |
||
| 231 | * @param array $argDefinitions Client arguments. |
||
| 232 | */ |
||
| 233 | public function __construct(array $argDefinitions) |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Resolves client configuration options and attached event listeners. |
||
| 240 | * Check for missing keys in passed arguments |
||
| 241 | * |
||
| 242 | * @param array $args Provided constructor arguments. |
||
| 243 | * @param HandlerList $list Handler list to augment. |
||
| 244 | * |
||
| 245 | * @return array Returns the array of provided options. |
||
| 246 | * @throws \InvalidArgumentException |
||
| 247 | * @see Aws\AwsClient::__construct for a list of available options. |
||
| 248 | */ |
||
| 249 | public function resolve(array $args, HandlerList $list) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Creates a verbose error message for an invalid argument. |
||
| 304 | * |
||
| 305 | * @param string $name Name of the argument that is missing. |
||
| 306 | * @param array $args Provided arguments |
||
| 307 | * @param bool $useRequired Set to true to show the required fn text if |
||
| 308 | * available instead of the documentation. |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | private function getArgMessage($name, $args = [], $useRequired = false) |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Throw when an invalid type is encountered. |
||
| 339 | * |
||
| 340 | * @param string $name Name of the value being validated. |
||
| 341 | * @param mixed $provided The provided value. |
||
| 342 | * @throws \InvalidArgumentException |
||
| 343 | */ |
||
| 344 | private function invalidType($name, $provided) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Throws an exception for missing required arguments. |
||
| 356 | * |
||
| 357 | * @param array $args Passed in arguments. |
||
| 358 | * @throws \InvalidArgumentException |
||
| 359 | */ |
||
| 360 | private function throwRequired(array $args) |
||
| 376 | |||
| 377 | public static function _apply_retries($value, array &$args, HandlerList $list) |
||
| 387 | |||
| 388 | public static function _apply_credentials($value, array &$args) |
||
| 420 | |||
| 421 | public static function _apply_api_provider(callable $value, array &$args) |
||
| 444 | |||
| 445 | public static function _apply_endpoint_provider(callable $value, array &$args) |
||
| 484 | |||
| 485 | public static function _apply_serializer($value, array &$args, HandlerList $list) |
||
| 489 | |||
| 490 | public static function _apply_debug($value, array &$args, HandlerList $list) |
||
| 496 | |||
| 497 | public static function _apply_stats($value, array &$args, HandlerList $list) |
||
| 514 | |||
| 515 | public static function _apply_profile($_, array &$args) |
||
| 519 | |||
| 520 | public static function _apply_validate($value, array &$args, HandlerList $list) |
||
| 534 | |||
| 535 | public static function _apply_handler($value, array &$args, HandlerList $list) |
||
| 539 | |||
| 540 | public static function _default_handler(array &$args) |
||
| 550 | |||
| 551 | public static function _apply_http_handler($value, array &$args, HandlerList $list) |
||
| 561 | |||
| 562 | public static function _apply_user_agent($value, array &$args, HandlerList $list) |
||
| 588 | |||
| 589 | public static function _apply_endpoint($value, array &$args, HandlerList $list) |
||
| 600 | |||
| 601 | public static function _apply_idempotency_auto_fill( |
||
| 624 | |||
| 625 | public static function _default_endpoint_provider(array $args) |
||
| 630 | |||
| 631 | public static function _default_serializer(array $args) |
||
| 638 | |||
| 639 | public static function _default_signature_provider() |
||
| 643 | |||
| 644 | View Code Duplication | public static function _default_signature_version(array &$args) |
|
| 661 | |||
| 662 | public static function _default_signing_name(array &$args) |
||
| 685 | |||
| 686 | View Code Duplication | public static function _default_signing_region(array &$args) |
|
| 703 | |||
| 704 | public static function _missing_version(array $args) |
||
| 730 | |||
| 731 | public static function _missing_region(array $args) |
||
| 741 | } |
||
| 742 |
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.