| Conditions | 10 |
| Paths | 10 |
| Total Lines | 48 |
| Lines | 0 |
| Ratio | 0 % |
| Tests | 0 |
| CRAP Score | 110 |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 22 | public static function validate(array $resource): array |
||
| 23 | { |
||
| 24 | $defaults = [ |
||
| 25 | 'storage' => [ |
||
| 26 | 'kind' => 'StreamStorage', |
||
| 27 | ], |
||
| 28 | 'resource' => [ |
||
| 29 | 'format' => null, |
||
| 30 | 'min_width' => null, |
||
| 31 | 'max_width' => null, |
||
| 32 | ], |
||
| 33 | ]; |
||
| 34 | |||
| 35 | $resource = array_replace_recursive($defaults, $resource); |
||
| 36 | |||
| 37 | if (!isset($resource['file']) || !is_string($resource['file'])) { |
||
| 38 | throw new InvalidArgumentException('file is required and must be a string'); |
||
| 39 | } |
||
| 40 | |||
| 41 | foreach ($resource['resource'] as $key => $value) { |
||
| 42 | if (is_null($value)) { |
||
| 43 | continue; |
||
| 44 | } |
||
| 45 | |||
| 46 | switch ($key) { |
||
| 47 | case 'format': |
||
| 48 | if (!is_string($value)) { |
||
| 49 | throw new InvalidArgumentException("resource.$key must be a string"); |
||
| 50 | } |
||
| 51 | |||
| 52 | break; |
||
| 53 | case 'min_width': |
||
| 54 | case 'max_width': |
||
| 55 | if (!is_int($value)) { |
||
| 56 | throw new InvalidArgumentException("resource.$key must be an integer"); |
||
| 57 | } |
||
| 58 | |||
| 59 | break; |
||
| 60 | default: |
||
| 61 | throw new InvalidArgumentException('unknown option resource.'.$key.' provided'); |
||
| 62 | } |
||
| 63 | } |
||
| 64 | |||
| 65 | $resource = array_replace_recursive($defaults, $resource); |
||
| 66 | $resource['storage'] = StorageValidator::validate($resource['storage']); |
||
| 67 | |||
| 68 | return $resource; |
||
| 69 | } |
||
| 70 | } |
||
| 71 |