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 |
||
9 | class Parameter implements ToArrayInterface |
||
10 | { |
||
11 | private $originalData; |
||
12 | |||
13 | /** @var string $name */ |
||
14 | private $name; |
||
15 | |||
16 | /** @var string $description */ |
||
17 | private $description; |
||
18 | |||
19 | /** @var string|array $type */ |
||
20 | private $type; |
||
21 | |||
22 | /** @var bool $required*/ |
||
23 | private $required; |
||
24 | |||
25 | /** @var array|null $enum */ |
||
26 | private $enum; |
||
27 | |||
28 | /** @var string $pattern */ |
||
29 | private $pattern; |
||
30 | |||
31 | /** @var int $minimum*/ |
||
32 | private $minimum; |
||
33 | |||
34 | /** @var int $maximum */ |
||
35 | private $maximum; |
||
36 | |||
37 | /** @var int $minLength */ |
||
38 | private $minLength; |
||
39 | |||
40 | /** @var int $maxLength */ |
||
41 | private $maxLength; |
||
42 | |||
43 | /** @var int $minItems */ |
||
44 | private $minItems; |
||
45 | |||
46 | /** @var int $maxItems */ |
||
47 | private $maxItems; |
||
48 | |||
49 | /** @var mixed $default */ |
||
50 | private $default; |
||
51 | |||
52 | /** @var bool $static */ |
||
53 | private $static; |
||
54 | |||
55 | /** @var array $filters */ |
||
56 | private $filters; |
||
57 | |||
58 | /** @var string $location */ |
||
59 | private $location; |
||
60 | |||
61 | /** @var string $sentAs */ |
||
62 | private $sentAs; |
||
63 | |||
64 | /** @var array $data */ |
||
65 | private $data; |
||
66 | |||
67 | /** @var array $properties */ |
||
68 | private $properties = []; |
||
69 | |||
70 | /** @var array|bool|Parameter $additionalProperties */ |
||
71 | private $additionalProperties; |
||
72 | |||
73 | /** @var array|Parameter $items */ |
||
74 | private $items; |
||
75 | |||
76 | /** @var string $format */ |
||
77 | private $format; |
||
78 | |||
79 | private $propertiesCache = null; |
||
80 | |||
81 | /** @var Description */ |
||
82 | private $serviceDescription; |
||
83 | |||
84 | /** |
||
85 | * Create a new Parameter using an associative array of data. |
||
86 | * |
||
87 | * The array can contain the following information: |
||
88 | * |
||
89 | * - name: (string) Unique name of the parameter |
||
90 | * |
||
91 | * - type: (string|array) Type of variable (string, number, integer, |
||
92 | * boolean, object, array, numeric, null, any). Types are used for |
||
93 | * validation and determining the structure of a parameter. You can use a |
||
94 | * union type by providing an array of simple types. If one of the union |
||
95 | * types matches the provided value, then the value is valid. |
||
96 | * |
||
97 | * - required: (bool) Whether or not the parameter is required |
||
98 | * |
||
99 | * - default: (mixed) Default value to use if no value is supplied |
||
100 | * |
||
101 | * - static: (bool) Set to true to specify that the parameter value cannot |
||
102 | * be changed from the default. |
||
103 | * |
||
104 | * - description: (string) Documentation of the parameter |
||
105 | * |
||
106 | * - location: (string) The location of a request used to apply a parameter. |
||
107 | * Custom locations can be registered with a command, but the defaults |
||
108 | * are uri, query, header, body, json, xml, formParam, multipart. |
||
109 | * |
||
110 | * - sentAs: (string) Specifies how the data being modeled is sent over the |
||
111 | * wire. For example, you may wish to include certain headers in a |
||
112 | * response model that have a normalized casing of FooBar, but the actual |
||
113 | * header is x-foo-bar. In this case, sentAs would be set to x-foo-bar. |
||
114 | * |
||
115 | * - filters: (array) Array of static method names to run a parameter |
||
116 | * value through. Each value in the array must be a string containing the |
||
117 | * full class path to a static method or an array of complex filter |
||
118 | * information. You can specify static methods of classes using the full |
||
119 | * namespace class name followed by '::' (e.g. Foo\Bar::baz). Some |
||
120 | * filters require arguments in order to properly filter a value. For |
||
121 | * complex filters, use a hash containing a 'method' key pointing to a |
||
122 | * static method, and an 'args' key containing an array of positional |
||
123 | * arguments to pass to the method. Arguments can contain keywords that |
||
124 | * are replaced when filtering a value: '@value' is replaced with the |
||
125 | * value being validated, '@api' is replaced with the Parameter object. |
||
126 | * |
||
127 | * - properties: When the type is an object, you can specify nested parameters |
||
128 | * |
||
129 | * - additionalProperties: (array) This attribute defines a schema for all |
||
130 | * properties that are not explicitly defined in an object type |
||
131 | * definition. If specified, the value MUST be a schema or a boolean. If |
||
132 | * false is provided, no additional properties are allowed beyond the |
||
133 | * properties defined in the schema. The default value is an empty schema |
||
134 | * which allows any value for additional properties. |
||
135 | * |
||
136 | * - items: This attribute defines the allowed items in an instance array, |
||
137 | * and MUST be a schema or an array of schemas. The default value is an |
||
138 | * empty schema which allows any value for items in the instance array. |
||
139 | * When this attribute value is a schema and the instance value is an |
||
140 | * array, then all the items in the array MUST be valid according to the |
||
141 | * schema. |
||
142 | * |
||
143 | * - pattern: When the type is a string, you can specify the regex pattern |
||
144 | * that a value must match |
||
145 | * |
||
146 | * - enum: When the type is a string, you can specify a list of acceptable |
||
147 | * values. |
||
148 | * |
||
149 | * - minItems: (int) Minimum number of items allowed in an array |
||
150 | * |
||
151 | * - maxItems: (int) Maximum number of items allowed in an array |
||
152 | * |
||
153 | * - minLength: (int) Minimum length of a string |
||
154 | * |
||
155 | * - maxLength: (int) Maximum length of a string |
||
156 | * |
||
157 | * - minimum: (int) Minimum value of an integer |
||
158 | * |
||
159 | * - maximum: (int) Maximum value of an integer |
||
160 | * |
||
161 | * - data: (array) Any additional custom data to use when serializing, |
||
162 | * validating, etc |
||
163 | * |
||
164 | * - format: (string) Format used to coax a value into the correct format |
||
165 | * when serializing or unserializing. You may specify either an array of |
||
166 | * filters OR a format, but not both. Supported values: date-time, date, |
||
167 | * time, timestamp, date-time-http, and boolean-string. |
||
168 | * |
||
169 | * - $ref: (string) String referencing a service description model. The |
||
170 | * parameter is replaced by the schema contained in the model. |
||
171 | * |
||
172 | * @param array $data Array of data as seen in service descriptions |
||
173 | * @param array $options Options used when creating the parameter. You can |
||
174 | * specify a Guzzle service description in the 'description' key. |
||
175 | * |
||
176 | * @throws \InvalidArgumentException |
||
177 | */ |
||
178 | 34 | public function __construct(array $data = [], array $options = []) |
|
221 | |||
222 | /** |
||
223 | * Convert the object to an array |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | 7 | public function toArray() |
|
231 | |||
232 | /** |
||
233 | * Get the default or static value of the command based on a value |
||
234 | * |
||
235 | * @param string $value Value that is currently set |
||
236 | * |
||
237 | * @return mixed Returns the value, a static value if one is present, or a default value |
||
238 | */ |
||
239 | 4 | public function getValue($value) |
|
247 | |||
248 | /** |
||
249 | * Run a value through the filters OR format attribute associated with the |
||
250 | * parameter. |
||
251 | * |
||
252 | * @param mixed $value Value to filter |
||
253 | * |
||
254 | * @return mixed Returns the filtered value |
||
255 | * @throws \RuntimeException when trying to format when no service |
||
256 | * description is available. |
||
257 | */ |
||
258 | 8 | public function filter($value) |
|
298 | |||
299 | /** |
||
300 | * Get the name of the parameter |
||
301 | * |
||
302 | * @return string |
||
303 | */ |
||
304 | 1 | public function getName() |
|
308 | |||
309 | /** |
||
310 | * Set the name of the parameter |
||
311 | * |
||
312 | * @param string $name Name to set |
||
313 | */ |
||
314 | 1 | public function setName($name) |
|
318 | |||
319 | /** |
||
320 | * Get the key of the parameter, where sentAs will supersede name if it is |
||
321 | * set. |
||
322 | * |
||
323 | * @return string |
||
324 | */ |
||
325 | 1 | public function getWireName() |
|
329 | |||
330 | /** |
||
331 | * Get the type(s) of the parameter |
||
332 | * |
||
333 | * @return string|array |
||
334 | */ |
||
335 | 2 | public function getType() |
|
339 | |||
340 | /** |
||
341 | * Get if the parameter is required |
||
342 | * |
||
343 | * @return bool |
||
344 | */ |
||
345 | 1 | public function isRequired() |
|
349 | |||
350 | /** |
||
351 | * Get the default value of the parameter |
||
352 | * |
||
353 | * @return string|null |
||
354 | */ |
||
355 | 1 | public function getDefault() |
|
359 | |||
360 | /** |
||
361 | * Get the description of the parameter |
||
362 | * |
||
363 | * @return string|null |
||
364 | */ |
||
365 | 1 | public function getDescription() |
|
369 | |||
370 | /** |
||
371 | * Get the minimum acceptable value for an integer |
||
372 | * |
||
373 | * @return int|null |
||
374 | */ |
||
375 | 1 | public function getMinimum() |
|
379 | |||
380 | /** |
||
381 | * Get the maximum acceptable value for an integer |
||
382 | * |
||
383 | * @return int|null |
||
384 | */ |
||
385 | 1 | public function getMaximum() |
|
389 | |||
390 | /** |
||
391 | * Get the minimum allowed length of a string value |
||
392 | * |
||
393 | * @return int |
||
394 | */ |
||
395 | 1 | public function getMinLength() |
|
399 | |||
400 | /** |
||
401 | * Get the maximum allowed length of a string value |
||
402 | * |
||
403 | * @return int|null |
||
404 | */ |
||
405 | 1 | public function getMaxLength() |
|
409 | |||
410 | /** |
||
411 | * Get the maximum allowed number of items in an array value |
||
412 | * |
||
413 | * @return int|null |
||
414 | */ |
||
415 | 1 | public function getMaxItems() |
|
419 | |||
420 | /** |
||
421 | * Get the minimum allowed number of items in an array value |
||
422 | * |
||
423 | * @return int |
||
424 | */ |
||
425 | 1 | public function getMinItems() |
|
429 | |||
430 | /** |
||
431 | * Get the location of the parameter |
||
432 | * |
||
433 | * @return string|null |
||
434 | */ |
||
435 | 2 | public function getLocation() |
|
439 | |||
440 | /** |
||
441 | * Get the sentAs attribute of the parameter that used with locations to |
||
442 | * sentAs an attribute when it is being applied to a location. |
||
443 | * |
||
444 | * @return string|null |
||
445 | */ |
||
446 | 1 | public function getSentAs() |
|
450 | |||
451 | /** |
||
452 | * Retrieve a known property from the parameter by name or a data property |
||
453 | * by name. When no specific name value is passed, all data properties |
||
454 | * will be returned. |
||
455 | * |
||
456 | * @param string|null $name Specify a particular property name to retrieve |
||
457 | * |
||
458 | * @return array|mixed|null |
||
459 | */ |
||
460 | 1 | public function getData($name = null) |
|
472 | |||
473 | /** |
||
474 | * Get whether or not the default value can be changed |
||
475 | * |
||
476 | * @return bool |
||
477 | */ |
||
478 | 1 | public function isStatic() |
|
482 | |||
483 | /** |
||
484 | * Get an array of filters used by the parameter |
||
485 | * |
||
486 | * @return array |
||
487 | */ |
||
488 | 2 | public function getFilters() |
|
492 | |||
493 | /** |
||
494 | * Get the properties of the parameter |
||
495 | * |
||
496 | * @return Parameter[] |
||
497 | */ |
||
498 | 1 | public function getProperties() |
|
509 | |||
510 | /** |
||
511 | * Get a specific property from the parameter |
||
512 | * |
||
513 | * @param string $name Name of the property to retrieve |
||
514 | * |
||
515 | * @return null|Parameter |
||
516 | */ |
||
517 | 1 | public function getProperty($name) |
|
533 | |||
534 | /** |
||
535 | * Get the additionalProperties value of the parameter |
||
536 | * |
||
537 | * @return bool|Parameter|null |
||
538 | */ |
||
539 | 1 | public function getAdditionalProperties() |
|
550 | |||
551 | /** |
||
552 | * Get the item data of the parameter |
||
553 | * |
||
554 | * @return Parameter |
||
555 | */ |
||
556 | 1 | public function getItems() |
|
567 | |||
568 | /** |
||
569 | * Get the enum of strings that are valid for the parameter |
||
570 | * |
||
571 | * @return array|null |
||
572 | */ |
||
573 | 1 | public function getEnum() |
|
577 | |||
578 | /** |
||
579 | * Get the regex pattern that must match a value when the value is a string |
||
580 | * |
||
581 | * @return string |
||
582 | */ |
||
583 | 1 | public function getPattern() |
|
587 | |||
588 | /** |
||
589 | * Get the format attribute of the schema |
||
590 | * |
||
591 | * @return string |
||
592 | */ |
||
593 | 4 | public function getFormat() |
|
597 | |||
598 | /** |
||
599 | * Set the array of filters used by the parameter |
||
600 | * |
||
601 | * @param array $filters Array of functions to use as filters |
||
602 | * |
||
603 | * @return self |
||
604 | */ |
||
605 | 10 | private function setFilters(array $filters) |
|
614 | |||
615 | /** |
||
616 | * Add a filter to the parameter |
||
617 | * |
||
618 | * @param string|array $filter Method to filter the value through |
||
619 | * |
||
620 | * @return self |
||
621 | * @throws \InvalidArgumentException |
||
622 | */ |
||
623 | 10 | private function addFilter($filter) |
|
641 | |||
642 | /** |
||
643 | * Check if a parameter has a specific variable and if it set. |
||
644 | * |
||
645 | * @param string $var |
||
646 | * @return bool |
||
647 | */ |
||
648 | 3 | public function has($var) |
|
655 | } |
||
656 |
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.