Completed
Push — feature/moveCommonLogicToTrait ( 95b46c...cb499b )
by Andreas
06:31 queued 05:13
created

DateTimeBase::getDateTime()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
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 DateTimeBase::__construct() has too many arguments starting with $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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

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
Bug introduced by
The method format() does not exist on UTCDateTime\DateTimeBase. Did you maybe mean createFromFormat()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
Unused Code introduced by
The call to DateTimeBase::__construct() has too many arguments starting with $this->format('Y-m-d H:i:s').

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.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
61
    }
62
}
63