Completed
Pull Request — master (#268)
by
unknown
01:16
created

Xhgui_Storage_Abstract::getDateTimeFromString()   C

Complexity

Conditions 12
Paths 31

Size

Total Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 37
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
31
                if ($direction === 'start') {
32
                    $datetime->setTime(0, 0, 0);
33
                } elseif ($direction === 'end') {
34
                    $datetime->setTime(23, 59, 59);
35
                }
36
37
                return $datetime;
38
            }
39
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
40
        }
41
42
        // try using timestamp
43
        try {
44
            $datetime = \DateTime::createFromFormat('U', $date);
45
            if (!empty($datetime) && $datetime instanceof \DateTime) {
46
                return $datetime;
47
            }
48
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
49
        }
50
51
        throw new \InvalidArgumentException('Unable to parse date');
52
    }
53
}
54