| Conditions | 11 |
| Paths | 15 |
| Total Lines | 41 |
| Code Lines | 22 |
| 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 |
||
| 29 | public static function filter($value, bool $allowNull = false, int $minLength = 1, int $maxLength = PHP_INT_MAX) |
||
| 30 | { |
||
| 31 | if ($minLength < 0) { |
||
| 32 | throw new \InvalidArgumentException('$minLength was not a positive integer value'); |
||
| 33 | } |
||
| 34 | |||
| 35 | if ($maxLength < 0) { |
||
| 36 | throw new \InvalidArgumentException('$maxLength was not a positive integer value'); |
||
| 37 | } |
||
| 38 | |||
| 39 | if ($allowNull === true && $value === null) { |
||
| 40 | return null; |
||
| 41 | } |
||
| 42 | |||
| 43 | if (is_scalar($value)) { |
||
| 44 | $value = "{$value}"; |
||
| 45 | } |
||
| 46 | |||
| 47 | if (is_object($value) && method_exists($value, '__toString')) { |
||
| 48 | $value = (string)$value; |
||
| 49 | } |
||
| 50 | |||
| 51 | if (!is_string($value)) { |
||
| 52 | throw new FilterException("Value '" . var_export($value, true) . "' is not a string"); |
||
| 53 | } |
||
| 54 | |||
| 55 | $valueLength = strlen($value); |
||
| 56 | |||
| 57 | if ($valueLength < $minLength || $valueLength > $maxLength) { |
||
| 58 | throw new FilterException( |
||
| 59 | sprintf( |
||
| 60 | "Value '%s' with length '%d' is less than '%d' or greater than '%d'", |
||
| 61 | $value, |
||
| 62 | $valueLength, |
||
| 63 | $minLength, |
||
| 64 | $maxLength |
||
| 65 | ) |
||
| 66 | ); |
||
| 67 | } |
||
| 68 | |||
| 69 | return $value; |
||
| 70 | } |
||
| 94 |