Completed
Pull Request — master (#268)
by
unknown
02:52 queued 01:29
created

Xhgui_Storage_Abstract::getDateTimeFromString()   C

Complexity

Conditions 12
Paths 31

Size

Total Lines 36

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 6.9666
c 0
b 0
f 0
cc 12
nc 31
nop 2

How to fix   Complexity   

Long Method

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:

1
<?php
2
/**
3
 * Abstract class for all storage drivers.
4
 *
5
 * CAUTION: please use interface as a typehint!
6
 */
7
class Xhgui_Storage_Abstract
8
{
9
    /**
10
     * Try to get date from Y-m-d H:i:s or from timestamp
11
     *
12
     * @param string|int $date
13
     * @param string $direction
14
     * @return \DateTime
15
     */
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) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
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) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
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) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
48
        }
49
50
        throw new \InvalidArgumentException('Unable to parse date');
51
    }
52
}
53