Completed
Push — master ( 7fad79...963e22 )
by Markus
05:05 queued 02:55
created

DateUtil::getDateString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 4
crap 2
1
<?php
2
3
namespace Eluceo\iCal\Util;
4
5
class DateUtil
6
{
7 4
    public static function getDefaultParams(\DateTime $dateTime = null, $noTime = false, $useTimezone = false)
8
    {
9 4
        $params = array();
10
11 4
        if ($useTimezone) {
12
            $timeZone       = $dateTime->getTimezone()->getName();
0 ignored issues
show
Bug introduced by
It seems like $dateTime is not always an object, but can also be of type null. Maybe add an additional type check?

If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe:

function someFunction(A $objectMaybe = null)
{
    if ($objectMaybe instanceof A) {
        $objectMaybe->doSomething();
    }
}
Loading history...
13
            $params['TZID'] = $timeZone;
14
        }
15
16 4
        if ($noTime) {
17
            $params['VALUE'] = 'DATE';
18
        }
19
20 4
        return $params;
21
    }
22
23
    /**
24
     * Returns a formatted date string.
25
     *
26
     * @param \DateTime|null $dateTime    The DateTime object
27
     * @param bool           $noTime      Indicates if the time will be added
28
     * @param bool           $useTimezone
29
     * @param bool           $useUtc
30
     *
31
     * @return mixed
32
     */
33 4
    public static function getDateString(\DateTime $dateTime = null, $noTime = false, $useTimezone = false, $useUtc = false)
34
    {
35 4
        if (empty($dateTime)) {
36 2
            $dateTime = new \DateTime();
37
        }
38
39 4
        return $dateTime->format(self::getDateFormat($noTime, $useTimezone, $useUtc));
40
    }
41
42
    /**
43
     * Returns the date format that can be passed to DateTime::format().
44
     *
45
     * @param bool $noTime      Indicates if the time will be added
46
     * @param bool $useTimezone
47
     * @param bool $useUtc
48
     *
49
     * @return string
50
     */
51 4
    public static function getDateFormat($noTime = false, $useTimezone = false, $useUtc = false)
52
    {
53
        // Do not use UTC time (Z) if timezone support is enabled.
54 4
        if ($useTimezone || !$useUtc) {
55
            return $noTime ? 'Ymd' : 'Ymd\THis';
56
        }
57
58 4
        return $noTime ? 'Ymd' : 'Ymd\THis\Z';
59
    }
60
}
61