Completed
Push — master ( 5db245...cdc2ed )
by Markus
17s queued 14s
created

DateUtil::getDateFormat()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 5

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 4
cts 4
cp 1
rs 8.8571
cc 5
eloc 4
nc 4
nop 3
crap 5
1
<?php
2
3
/*
4
 * This file is part of the eluceo/iCal package.
5
 *
6
 * (c) Markus Poerschke <[email protected]>
7
 *
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace Eluceo\iCal\Util;
13
14
class DateUtil
15
{
16 23
    public static function getDefaultParams(\DateTimeInterface $dateTime = null, $noTime = false, $useTimezone = false, $timezoneString = '')
17
    {
18 23
        $params = [];
19
20 23
        if ($useTimezone && $noTime === false) {
21 1
            $timeZone = $timezoneString === '' ? $dateTime->getTimezone()->getName() : $timezoneString;
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...
22 1
            $params['TZID'] = $timeZone;
23
        }
24
25 23
        if ($noTime) {
26 3
            $params['VALUE'] = 'DATE';
27
        }
28
29 23
        return $params;
30
    }
31
32
    /**
33
     * Returns a formatted date string.
34
     *
35
     * @param \DateTimeInterface|null $dateTime    The DateTime object
36
     * @param bool                    $noTime      Indicates if the time will be added
37
     * @param bool                    $useTimezone
38
     * @param bool                    $useUtc
39
     *
40
     * @return mixed
41
     */
42 23
    public static function getDateString(\DateTimeInterface $dateTime = null, $noTime = false, $useTimezone = false, $useUtc = false)
43
    {
44 23
        if (empty($dateTime)) {
45 16
            $dateTime = new \DateTimeImmutable();
46
        }
47
48
        // Only convert the DateTime to UTC if there is a time present. For date-only the
49
        // timezone is meaningless and converting it might shift it to the wrong date.
50 23
        if (!$noTime && $useUtc) {
51 17
            $dateTime = clone $dateTime;
52 17
            $dateTime->setTimezone(new \DateTimeZone('UTC'));
53
        }
54
55 23
        return $dateTime->format(self::getDateFormat($noTime, $useTimezone, $useUtc));
56
    }
57
58
    /**
59
     * Returns the date format that can be passed to DateTime::format().
60
     *
61
     * @param bool $noTime      Indicates if the time will be added
62
     * @param bool $useTimezone
63
     * @param bool $useUtc
64
     *
65
     * @return string
66
     */
67 23
    public static function getDateFormat($noTime = false, $useTimezone = false, $useUtc = false)
68
    {
69
        // Do not use UTC time (Z) if timezone support is enabled.
70 23
        if ($useTimezone || !$useUtc) {
71 5
            return $noTime ? 'Ymd' : 'Ymd\THis';
72
        }
73
74 18
        return $noTime ? 'Ymd' : 'Ymd\THis\Z';
75
    }
76
}
77