| Conditions | 12 |
| Paths | 12 |
| Total Lines | 42 |
| 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 |
||
| 47 | public static function convert(?string $value, int $type, string $dateFormat = null) { |
||
| 48 | |||
| 49 | switch ($type) { |
||
| 50 | |||
| 51 | case self::ARGUMENT_BOOLEAN: |
||
| 52 | return BooleanHelper::parseString($value); |
||
| 53 | |||
| 54 | case self::ARGUMENT_DATE: |
||
| 55 | if (null === $dateFormat) { |
||
| 56 | throw new InvalidArgumentException("The date format is null"); |
||
| 57 | } |
||
| 58 | $datetime = DateTime::createFromFormat($dateFormat, $value); |
||
| 59 | if (false === $datetime) { |
||
| 60 | throw new DateArgumentException($value); |
||
| 61 | } |
||
| 62 | return $datetime; |
||
| 63 | |||
| 64 | case self::ARGUMENT_DOUBLE: |
||
| 65 | return DoubleHelper::parseString($value); |
||
| 66 | |||
| 67 | case self::ARGUMENT_FLOAT: |
||
| 68 | return FloatHelper::parseString($value); |
||
| 69 | |||
| 70 | case self::ARGUMENT_INTEGER: |
||
| 71 | return IntegerHelper::parseString($value); |
||
| 72 | |||
| 73 | case self::ARGUMENT_STRING: |
||
| 74 | return $value; |
||
| 75 | |||
| 76 | case self::ARGUMENT_TIMESTAMP: |
||
| 77 | if (null === $dateFormat) { |
||
| 78 | throw new InvalidArgumentException("The datetime format is null"); |
||
| 79 | } |
||
| 80 | $datetime = DateTime::createFromFormat($dateFormat, $value); |
||
| 81 | if (false === $datetime) { |
||
| 82 | throw new TimestampArgumentException($value); |
||
| 83 | } |
||
| 84 | return $datetime; |
||
| 85 | } |
||
| 86 | |||
| 87 | throw new InvalidArgumentException("The type \"{$type}\" is not implemented"); |
||
| 88 | } |
||
| 89 | |||
| 150 |