Completed
Push — master ( ab261b...860b57 )
by Andreas
03:32 queued 02:14
created

DateTimeBase::getDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Copyright Andrea Heigl <[email protected]>
7
 *
8
 * Licenses under the MIT-license. For details see the included file LICENSE.md
9
 */
10
11
namespace UTCDateTime;
12
13
use function date_default_timezone_get;
14
use DateTimeZone;
15
use function trigger_error;
16
17
trait DateTimeBase
18
{
19
    /**
20
     * @param DateTimeZone $timezone
21
     *
22
     * @return self
23
     */
24 4
    public function setTimezone($timezone)
25
    {
26 4
        trigger_error('Setting a timezone on a UTCDateTime-object doesn\'t make sense');
27
28 2
        return $this;
29
    }
30
31
    /**
32
     * Parse a string into a new DateTime object according to the specified format
33
     *
34
     * @param string $format Format accepted by date().
35
     * @param string $time String representing the time.
36
     * @param DateTimeZone $timezone A DateTimeZone object representing the desired time zone.
37
     *
38
     * @link http://php.net/manual/en/datetime.createfromformat.php
39
     * @return self
40
     */
41 4
    public static function createFromFormat($format, $time, DateTimeZone $timezone = null) : self
42
    {
43 4
        if (! $timezone) {
44 2
            $timezone = new DateTimeZone(date_default_timezone_get());
45
        }
46
47 4
        $dateTimeObject = \DateTimeImmutable::createFromFormat($format, $time, $timezone);
48 4
        $dateTimeObject->setTimezone(new DateTimeZone('UTC'));
49
50 4
        return new self($dateTimeObject->format(\DateTime::RFC2822));
0 ignored issues
show
Unused Code introduced by
The call to UTCDateTime\DateTimeBase::__construct() has too many arguments starting with $dateTimeObject->format(DateTime::RFC2822). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
        return /** @scrutinizer ignore-call */ new self($dateTimeObject->format(\DateTime::RFC2822));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
51
    }
52
53
    /**
54
     * Get a PHP-DateTime-Object from this class for further handling
55
     *
56
     * @return self
57
     */
58 1
    public function getDateTime()
59
    {
60 1
        return new static($this->format('Y-m-d H:i:s'), new DateTimeZone('UTC'));
0 ignored issues
show
Unused Code introduced by
The call to UTCDateTime\DateTimeBase::__construct() has too many arguments starting with $this->format('Y-m-d H:i:s'). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        return /** @scrutinizer ignore-call */ new static($this->format('Y-m-d H:i:s'), new DateTimeZone('UTC'));

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
It seems like format() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

60
        return new static($this->/** @scrutinizer ignore-call */ format('Y-m-d H:i:s'), new DateTimeZone('UTC'));
Loading history...
61
    }
62
}
63