Conditions | 11 |
Paths | 28 |
Total Lines | 33 |
Code Lines | 20 |
Lines | 0 |
Ratio | 0 % |
Tests | 18 |
CRAP Score | 11 |
Changes | 1 | ||
Bugs | 0 | Features | 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 |
||
36 | 82 | private function processDateTime($dateTime, $required = false): ?string |
|
37 | { |
||
38 | 82 | if (is_string($dateTime)) { |
|
39 | 18 | $dateTime = date_create($dateTime); |
|
40 | } |
||
41 | |||
42 | if ( |
||
43 | 82 | ($dateTime instanceof DateTimeInterface |
|
44 | 74 | && (int) $dateTime->format('Y') <= 0) |
|
45 | 82 | || false === $dateTime |
|
46 | ) { |
||
47 | 6 | $dateTime = null; |
|
48 | } |
||
49 | |||
50 | 82 | if ($dateTime instanceof DateTimeInterface) { |
|
51 | if ( |
||
52 | 70 | 0 == $dateTime->format('H') && |
|
53 | 70 | 0 == $dateTime->format('i') && |
|
54 | 70 | 0 == $dateTime->format('s') |
|
55 | ) { |
||
56 | 66 | $dateTime = $dateTime->format('Y-m-d'); |
|
57 | } else { |
||
58 | 70 | $dateTime = $dateTime->format(\DateTime::W3C); |
|
59 | } |
||
60 | 12 | } elseif ($required) { |
|
61 | 4 | throw new InvalidArgumentException('Invalid date parameter.'); |
|
62 | } |
||
63 | |||
64 | 78 | if (!is_string($dateTime)) { |
|
65 | 8 | $dateTime = null; |
|
66 | } |
||
67 | |||
68 | 78 | return $dateTime; |
|
69 | } |
||
71 |