Conditions | 12 |
Paths | 31 |
Total 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 |
||
16 | protected function getDateTimeFromString($date, $direction = 'start') |
||
17 | { |
||
18 | try { |
||
19 | $datetime = \DateTime::createFromFormat('Y-m-d H:i:s', $date); |
||
20 | if (!empty($datetime) && $datetime instanceof \DateTime) { |
||
21 | return $datetime; |
||
22 | } |
||
23 | } catch (\Exception $e) { |
||
|
|||
24 | } |
||
25 | |||
26 | // try without time |
||
27 | try { |
||
28 | $datetime = \DateTime::createFromFormat('Y-m-d', $date); |
||
29 | if (!empty($datetime) && $datetime instanceof \DateTime) { |
||
30 | if ($direction === 'start') { |
||
31 | $datetime->setTime(0, 0, 0); |
||
32 | } elseif ($direction === 'end') { |
||
33 | $datetime->setTime(23, 59, 59); |
||
34 | } |
||
35 | |||
36 | return $datetime; |
||
37 | } |
||
38 | } catch (\Exception $e) { |
||
39 | } |
||
40 | |||
41 | // try using timestamp |
||
42 | try { |
||
43 | $datetime = \DateTime::createFromFormat('U', $date); |
||
44 | if (!empty($datetime) && $datetime instanceof \DateTime) { |
||
45 | return $datetime; |
||
46 | } |
||
47 | } catch (\Exception $e) { |
||
48 | } |
||
49 | |||
50 | throw new \InvalidArgumentException('Unable to parse date'); |
||
51 | } |
||
52 | } |
||
53 |