Complex classes like Operation 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 Operation, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Operation implements OperationInterface |
||
| 11 | { |
||
| 12 | /** @var string Default command class to use when none is specified */ |
||
| 13 | const DEFAULT_COMMAND_CLASS = 'Guzzle\\Service\\Command\\OperationCommand'; |
||
| 14 | |||
| 15 | /** @var array Hashmap of properties that can be specified. Represented as a hash to speed up constructor. */ |
||
| 16 | protected static $properties = array( |
||
| 17 | 'name' => true, 'httpMethod' => true, 'uri' => true, 'class' => true, 'responseClass' => true, |
||
| 18 | 'responseType' => true, 'responseNotes' => true, 'notes' => true, 'summary' => true, 'documentationUrl' => true, |
||
| 19 | 'deprecated' => true, 'data' => true, 'parameters' => true, 'additionalParameters' => true, |
||
| 20 | 'errorResponses' => true |
||
| 21 | ); |
||
| 22 | |||
| 23 | /** @var array Parameters */ |
||
| 24 | protected $parameters = array(); |
||
| 25 | |||
| 26 | /** @var Parameter Additional parameters schema */ |
||
| 27 | protected $additionalParameters; |
||
| 28 | |||
| 29 | /** @var string Name of the command */ |
||
| 30 | protected $name; |
||
| 31 | |||
| 32 | /** @var string HTTP method */ |
||
| 33 | protected $httpMethod; |
||
| 34 | |||
| 35 | /** @var string This is a short summary of what the operation does */ |
||
| 36 | protected $summary; |
||
| 37 | |||
| 38 | /** @var string A longer text field to explain the behavior of the operation. */ |
||
| 39 | protected $notes; |
||
| 40 | |||
| 41 | /** @var string Reference URL providing more information about the operation */ |
||
| 42 | protected $documentationUrl; |
||
| 43 | |||
| 44 | /** @var string HTTP URI of the command */ |
||
| 45 | protected $uri; |
||
| 46 | |||
| 47 | /** @var string Class of the command object */ |
||
| 48 | protected $class; |
||
| 49 | |||
| 50 | /** @var string This is what is returned from the method */ |
||
| 51 | protected $responseClass; |
||
| 52 | |||
| 53 | /** @var string Type information about the response */ |
||
| 54 | protected $responseType; |
||
| 55 | |||
| 56 | /** @var string Information about the response returned by the operation */ |
||
| 57 | protected $responseNotes; |
||
| 58 | |||
| 59 | /** @var bool Whether or not the command is deprecated */ |
||
| 60 | protected $deprecated; |
||
| 61 | |||
| 62 | /** @var array Array of errors that could occur when running the command */ |
||
| 63 | protected $errorResponses; |
||
| 64 | |||
| 65 | /** @var ServiceDescriptionInterface */ |
||
| 66 | protected $description; |
||
| 67 | |||
| 68 | /** @var array Extra operation information */ |
||
| 69 | protected $data; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Builds an Operation object using an array of configuration data: |
||
| 73 | * - name: (string) Name of the command |
||
| 74 | * - httpMethod: (string) HTTP method of the operation |
||
| 75 | * - uri: (string) URI template that can create a relative or absolute URL |
||
| 76 | * - class: (string) Concrete class that implements this command |
||
| 77 | * - parameters: (array) Associative array of parameters for the command. {@see Parameter} for information. |
||
| 78 | * - summary: (string) This is a short summary of what the operation does |
||
| 79 | * - notes: (string) A longer text field to explain the behavior of the operation. |
||
| 80 | * - documentationUrl: (string) Reference URL providing more information about the operation |
||
| 81 | * - responseClass: (string) This is what is returned from the method. Can be a primitive, PSR-0 compliant |
||
| 82 | * class name, or model. |
||
| 83 | * - responseNotes: (string) Information about the response returned by the operation |
||
| 84 | * - responseType: (string) One of 'primitive', 'class', 'model', or 'documentation'. If not specified, this |
||
| 85 | * value will be automatically inferred based on whether or not there is a model matching the |
||
| 86 | * name, if a matching PSR-0 compliant class name is found, or set to 'primitive' by default. |
||
| 87 | * - deprecated: (bool) Set to true if this is a deprecated command |
||
| 88 | * - errorResponses: (array) Errors that could occur when executing the command. Array of hashes, each with a |
||
| 89 | * 'code' (the HTTP response code), 'reason' (response reason phrase or description of the |
||
| 90 | * error), and 'class' (a custom exception class that would be thrown if the error is |
||
| 91 | * encountered). |
||
| 92 | * - data: (array) Any extra data that might be used to help build or serialize the operation |
||
| 93 | * - additionalParameters: (null|array) Parameter schema to use when an option is passed to the operation that is |
||
| 94 | * not in the schema |
||
| 95 | * |
||
| 96 | * @param array $config Array of configuration data |
||
| 97 | * @param ServiceDescriptionInterface $description Service description used to resolve models if $ref tags are found |
||
| 98 | */ |
||
| 99 | public function __construct(array $config = array(), ServiceDescriptionInterface $description = null) |
||
| 144 | |||
| 145 | public function toArray() |
||
| 168 | |||
| 169 | public function getServiceDescription() |
||
| 173 | |||
| 174 | public function setServiceDescription(ServiceDescriptionInterface $description) |
||
| 180 | |||
| 181 | public function getParams() |
||
| 185 | |||
| 186 | public function getParamNames() |
||
| 190 | |||
| 191 | public function hasParam($name) |
||
| 195 | |||
| 196 | public function getParam($param) |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Add a parameter to the command |
||
| 203 | * |
||
| 204 | * @param Parameter $param Parameter to add |
||
| 205 | * |
||
| 206 | * @return self |
||
| 207 | */ |
||
| 208 | public function addParam(Parameter $param) |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Remove a parameter from the command |
||
| 218 | * |
||
| 219 | * @param string $name Name of the parameter to remove |
||
| 220 | * |
||
| 221 | * @return self |
||
| 222 | */ |
||
| 223 | public function removeParam($name) |
||
| 229 | |||
| 230 | public function getHttpMethod() |
||
| 234 | |||
| 235 | /** |
||
| 236 | * Set the HTTP method of the command |
||
| 237 | * |
||
| 238 | * @param string $httpMethod Method to set |
||
| 239 | * |
||
| 240 | * @return self |
||
| 241 | */ |
||
| 242 | public function setHttpMethod($httpMethod) |
||
| 248 | |||
| 249 | public function getClass() |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Set the concrete class of the command |
||
| 256 | * |
||
| 257 | * @param string $className Concrete class name |
||
| 258 | * |
||
| 259 | * @return self |
||
| 260 | */ |
||
| 261 | public function setClass($className) |
||
| 267 | |||
| 268 | public function getName() |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Set the name of the command |
||
| 275 | * |
||
| 276 | * @param string $name Name of the command |
||
| 277 | * |
||
| 278 | * @return self |
||
| 279 | */ |
||
| 280 | public function setName($name) |
||
| 286 | |||
| 287 | public function getSummary() |
||
| 291 | |||
| 292 | /** |
||
| 293 | * Set a short summary of what the operation does |
||
| 294 | * |
||
| 295 | * @param string $summary Short summary of the operation |
||
| 296 | * |
||
| 297 | * @return self |
||
| 298 | */ |
||
| 299 | public function setSummary($summary) |
||
| 305 | |||
| 306 | public function getNotes() |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Set a longer text field to explain the behavior of the operation. |
||
| 313 | * |
||
| 314 | * @param string $notes Notes on the operation |
||
| 315 | * |
||
| 316 | * @return self |
||
| 317 | */ |
||
| 318 | public function setNotes($notes) |
||
| 324 | |||
| 325 | public function getDocumentationUrl() |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Set the URL pointing to additional documentation on the command |
||
| 332 | * |
||
| 333 | * @param string $docUrl Documentation URL |
||
| 334 | * |
||
| 335 | * @return self |
||
| 336 | */ |
||
| 337 | public function setDocumentationUrl($docUrl) |
||
| 343 | |||
| 344 | public function getResponseClass() |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Set what is returned from the method. Can be a primitive, class name, or model. For example: 'array', |
||
| 351 | * 'Guzzle\\Foo\\Baz', or 'MyModelName' (to reference a model by ID). |
||
| 352 | * |
||
| 353 | * @param string $responseClass Type of response |
||
| 354 | * |
||
| 355 | * @return self |
||
| 356 | */ |
||
| 357 | public function setResponseClass($responseClass) |
||
| 364 | |||
| 365 | public function getResponseType() |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Set qualifying information about the responseClass. One of 'primitive', 'class', 'model', or 'documentation' |
||
| 372 | * |
||
| 373 | * @param string $responseType Response type information |
||
| 374 | * |
||
| 375 | * @return self |
||
| 376 | * @throws InvalidArgumentException |
||
| 377 | */ |
||
| 378 | public function setResponseType($responseType) |
||
| 394 | |||
| 395 | public function getResponseNotes() |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Set notes about the response of the operation |
||
| 402 | * |
||
| 403 | * @param string $notes Response notes |
||
| 404 | * |
||
| 405 | * @return self |
||
| 406 | */ |
||
| 407 | public function setResponseNotes($notes) |
||
| 413 | |||
| 414 | public function getDeprecated() |
||
| 418 | |||
| 419 | /** |
||
| 420 | * Set whether or not the command is deprecated |
||
| 421 | * |
||
| 422 | * @param bool $isDeprecated Set to true to mark as deprecated |
||
| 423 | * |
||
| 424 | * @return self |
||
| 425 | */ |
||
| 426 | public function setDeprecated($isDeprecated) |
||
| 432 | |||
| 433 | public function getUri() |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Set the URI template of the command |
||
| 440 | * |
||
| 441 | * @param string $uri URI template to set |
||
| 442 | * |
||
| 443 | * @return self |
||
| 444 | */ |
||
| 445 | public function setUri($uri) |
||
| 451 | |||
| 452 | public function getErrorResponses() |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Add an error to the command |
||
| 459 | * |
||
| 460 | * @param string $code HTTP response code |
||
| 461 | * @param string $reason HTTP response reason phrase or information about the error |
||
| 462 | * @param string $class Exception class associated with the error |
||
| 463 | * |
||
| 464 | * @return self |
||
| 465 | */ |
||
| 466 | public function addErrorResponse($code, $reason, $class) |
||
| 472 | |||
| 473 | /** |
||
| 474 | * Set all of the error responses of the operation |
||
| 475 | * |
||
| 476 | * @param array $errorResponses Hash of error name to a hash containing a code, reason, class |
||
| 477 | * |
||
| 478 | * @return self |
||
| 479 | */ |
||
| 480 | public function setErrorResponses(array $errorResponses) |
||
| 486 | |||
| 487 | public function getData($name) |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Set a particular data point on the operation |
||
| 494 | * |
||
| 495 | * @param string $name Name of the data value |
||
| 496 | * @param mixed $value Value to set |
||
| 497 | * |
||
| 498 | * @return self |
||
| 499 | */ |
||
| 500 | public function setData($name, $value) |
||
| 506 | |||
| 507 | /** |
||
| 508 | * Get the additionalParameters of the operation |
||
| 509 | * |
||
| 510 | * @return Parameter|null |
||
| 511 | */ |
||
| 512 | public function getAdditionalParameters() |
||
| 516 | |||
| 517 | /** |
||
| 518 | * Set the additionalParameters of the operation |
||
| 519 | * |
||
| 520 | * @param Parameter|null $parameter Parameter to set |
||
| 521 | * |
||
| 522 | * @return self |
||
| 523 | */ |
||
| 524 | public function setAdditionalParameters($parameter) |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Infer the response type from the responseClass value |
||
| 535 | */ |
||
| 536 | protected function inferResponseType() |
||
| 547 | } |
||
| 548 |
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.