Conditions | 24 |
Paths | 33 |
Total Lines | 79 |
Code Lines | 55 |
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 |
||
20 | public function buildDateTimeRangeFromTwoInputs($fromMonth, $toMonth = null, $defaultInterval = 'P1M'): array |
||
21 | { |
||
22 | if (preg_match('/^([0-9]{1,2})\.([0-9]{1,2})\.?$/', $fromMonth, $matches)) { |
||
23 | $fromDateTime = new \DateTime(date('Y-') . $matches[2] . '-' . $matches[1]); |
||
24 | if (!isset($toMonth)) { |
||
25 | $toDateTime = clone $fromDateTime; |
||
26 | $toDateTime->add(new DateInterval($defaultInterval)); |
||
27 | } |
||
28 | if (isset($toDateTime)) { |
||
29 | return [$fromDateTime, $toDateTime]; |
||
30 | } |
||
31 | if (preg_match('/^([0-9]{1,2})\.([0-9]{1,2})\.?$/', $toMonth, $matches2)) { |
||
32 | return [ |
||
33 | $fromDateTime, |
||
34 | new \DateTime(($matches2[2] < $matches[2] ? 1 + date('Y') : date('Y')) . '-' . $matches2[2] . '-' . $matches2[1]), |
||
35 | ]; |
||
36 | } |
||
37 | return [$fromDateTime, new \DateTime($toMonth)]; |
||
38 | } |
||
39 | if (preg_match('/^([0-9]{1,2})-[0-9]{1,2}$/', $fromMonth, $matches)) { |
||
40 | $fromDateTime = new \DateTime(date('Y-') . $fromMonth); |
||
41 | if (!isset($toMonth)) { |
||
42 | $toDateTime = clone $fromDateTime; |
||
43 | $toDateTime->add(new DateInterval($defaultInterval)); |
||
44 | } |
||
45 | if (isset($toDateTime)) { |
||
46 | return [$fromDateTime, $toDateTime]; |
||
47 | } |
||
48 | if (preg_match('/^([0-9]{1,2})-[0-9]{1,2}$/', $toMonth, $matches2)) { |
||
49 | return [ |
||
50 | $fromDateTime, |
||
51 | new \DateTime(($matches2[1] < $matches[1] ? 1 + date('Y') : date('Y')) . '-' . $toMonth), |
||
52 | ]; |
||
53 | } |
||
54 | if (preg_match('/^([0-9]{1,2})\.([0-9]{1,2})\.?$/', $toMonth, $matches2)) { |
||
55 | return [ |
||
56 | $fromDateTime, |
||
57 | new \DateTime(($matches2[2] < $matches[1] ? 1 + date('Y') : date('Y')) . '-' . $matches2[2] . '-' . $matches2[1]), |
||
58 | ]; |
||
59 | } |
||
60 | return [$fromDateTime, new \DateTime($toMonth)]; |
||
61 | } |
||
62 | if (preg_match('/^([0-9]{1,2})$/', $fromMonth, $matches)) { |
||
63 | if (!isset($toMonth)) { |
||
64 | $toMonth = (string)($fromMonth >= 12 ? 1 : $fromMonth + 1); |
||
65 | } |
||
66 | $fromYear = date('Y'); |
||
67 | $fromDateTime = new \DateTime($fromYear . '-' . str_pad($fromMonth, 2, 0, STR_PAD_LEFT) . '-01'); |
||
68 | if (preg_match('/^[0-9]{1,2}$/', $toMonth)) { |
||
69 | $toYear = $fromYear; |
||
70 | if ((int)$toMonth < (int)$fromMonth) { |
||
71 | $toYear += 1; |
||
72 | } |
||
73 | $toDateTime = new \DateTime($toYear . '-' . str_pad($toMonth, 2, 0, STR_PAD_LEFT) . '-01'); |
||
74 | return [$fromDateTime, $toDateTime]; |
||
75 | } |
||
76 | if (preg_match('/^([0-9]{1,2})-[0-9]{1,2}$/', $toMonth, $matches2)) { |
||
77 | return [ |
||
78 | $fromDateTime, |
||
79 | new \DateTime(($matches2[1] < $matches[1] ? 1 + date('Y') : date('Y')) . '-' . $toMonth), |
||
80 | ]; |
||
81 | } |
||
82 | if (preg_match('/^([0-9]{1,2})\.([0-9]{1,2})\.?$/', $toMonth, $matches2)) { |
||
83 | return [ |
||
84 | $fromDateTime, |
||
85 | new \DateTime(($matches2[2] < $matches[1] ? 1 + date('Y') : date('Y')) . '-' . $matches2[2] . '-' . $matches2[1]), |
||
86 | ]; |
||
87 | } |
||
88 | return [$fromDateTime, new \DateTime($toMonth)]; |
||
89 | } |
||
90 | $fromDateTime = new \DateTime($fromMonth); |
||
91 | if (!isset($toMonth)) { |
||
92 | $toDateTime = clone $fromDateTime; |
||
93 | $toDateTime->add(new DateInterval($defaultInterval)); |
||
94 | } |
||
95 | if (isset($toDateTime)) { |
||
96 | return [$fromDateTime, $toDateTime]; |
||
97 | } |
||
98 | return [$fromDateTime, new \DateTime($toMonth)]; |
||
99 | } |
||
140 |