| Conditions | 21 |
| Paths | 18 |
| Total Lines | 54 |
| Code Lines | 36 |
| Lines | 0 |
| Ratio | 0 % |
| 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 declare(strict_types = 1); |
||
| 30 | public function coerce(Parameter $parameter, $value) |
||
| 31 | { |
||
| 32 | $schema = $parameter->getSchema(); |
||
| 33 | |||
| 34 | switch ($schema->getType()) { |
||
| 35 | case Schema::TYPE_STRING: |
||
| 36 | return (string)$value; |
||
| 37 | case Schema::TYPE_BOOL: |
||
| 38 | if (!is_scalar($value)) { |
||
| 39 | return $value; |
||
| 40 | } |
||
| 41 | $bool = $this->coerceBooleanValue($value); |
||
| 42 | |||
| 43 | return $bool === null ? $value : $bool; |
||
| 44 | case Schema::TYPE_NUMBER: |
||
| 45 | if (!is_numeric($value)) { |
||
| 46 | return $value; |
||
| 47 | } |
||
| 48 | |||
| 49 | return ctype_digit($value) ? (int)$value : (float)$value; |
||
| 50 | case Schema::TYPE_OBJECT: |
||
| 51 | if (!is_array($value)) { |
||
| 52 | return $value == '' ? null : $value; |
||
| 53 | } |
||
| 54 | if (count($value) && is_numeric(key($value))) { |
||
| 55 | return $value; |
||
| 56 | } |
||
| 57 | |||
| 58 | return (object)$value; |
||
| 59 | case Schema::TYPE_ARRAY: |
||
| 60 | if (is_array($value) || !is_string($value)) { |
||
| 61 | return $value; |
||
| 62 | } |
||
| 63 | |||
| 64 | return $this->coerceArrayValue( |
||
| 65 | $parameter->getCollectionFormat() ? $parameter->getCollectionFormat() : 'csv', |
||
| 66 | $value |
||
| 67 | ); |
||
| 68 | case Schema::TYPE_INT: |
||
| 69 | if (!ctype_digit($value)) { |
||
| 70 | return $value; |
||
| 71 | } |
||
| 72 | |||
| 73 | return (integer)$value; |
||
| 74 | case Schema::TYPE_NULL: |
||
| 75 | if ($value !== '') { |
||
| 76 | return $value; |
||
| 77 | } |
||
| 78 | |||
| 79 | return null; |
||
| 80 | default: |
||
| 81 | return $value; |
||
| 82 | } |
||
| 83 | } |
||
| 84 | |||
| 131 |