DateTimeWithTimeZone::now()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace ValueObjects\DateTime;
4
5
use ValueObjects\Util\Util;
6
use ValueObjects\ValueObjectInterface;
7
8
class DateTimeWithTimeZone implements ValueObjectInterface
9
{
10
    /** @var DateTime */
11
    protected $dateTime;
12
13
    /** @var TimeZone */
14
    protected $timeZone;
15
16
    /**
17
     * Returns a new DateTime object from native values
18
     *
19
     * @param int    $year
0 ignored issues
show
Bug introduced by
There is no parameter named $year. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
20
     * @param string $month
0 ignored issues
show
Bug introduced by
There is no parameter named $month. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
21
     * @param int    $day
0 ignored issues
show
Bug introduced by
There is no parameter named $day. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
22
     * @param int    $hour
0 ignored issues
show
Bug introduced by
There is no parameter named $hour. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
23
     * @param int    $minute
0 ignored issues
show
Bug introduced by
There is no parameter named $minute. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
24
     * @param int    $second
0 ignored issues
show
Bug introduced by
There is no parameter named $second. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
25
     * @param string $timezone
0 ignored issues
show
Bug introduced by
There is no parameter named $timezone. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
26
     *
27
     * @return DateTimeWithTimeZone
28
     */
29 1
    public static function fromNative()
30
    {
31 1
        $args = func_get_args();
32
33 1
        $datetime = DateTime::fromNative($args[0], $args[1], $args[2], $args[3], $args[4], $args[5]);
34 1
        $timezone = TimeZone::fromNative($args[6]);
35
36 1
        return new static($datetime, $timezone);
37
    }
38
39
    /**
40
     * Returns a new DateTime from a native PHP \DateTime
41
     *
42
     * @param \DateTime $nativeDatetime
43
     *
44
     * @return DateTimeWithTimeZone
45
     */
46 1
    public static function fromNativeDateTime(\DateTime $nativeDatetime)
47
    {
48 1
        $datetime = DateTime::fromNativeDateTime($nativeDatetime);
49 1
        $timezone = TimeZone::fromNativeDateTimeZone($nativeDatetime->getTimezone());
50
51 1
        return new static($datetime, $timezone);
52
    }
53
54
    /**
55
     * Returns a DateTimeWithTimeZone object using current DateTime and default TimeZone
56
     *
57
     * @return DateTimeWithTimeZone
58
     */
59 1
    public static function now()
60
    {
61 1
        return new static(DateTime::now(), TimeZone::fromDefault());
62
    }
63
64
    /**
65
     * Returns a new DateTimeWithTimeZone object
66
     *
67
     * @param DateTime $datetime
68
     * @param TimeZone $timezone
69
     */
70 9
    public function __construct(DateTime $datetime, TimeZone $timezone = null)
71
    {
72 9
        $this->dateTime = $datetime;
73 9
        $this->timeZone = $timezone;
74 9
    }
75
76
    /**
77
     * Tells whether two DateTimeWithTimeZone are equal by comparing their values
78
     *
79
     * @param ValueObjectInterface $dateTimeWithTimeZone
80
     *
81
     * @return bool
82
     */
83 4
    public function sameValueAs(ValueObjectInterface $dateTimeWithTimeZone)
84
    {
85 4
        if (false === Util::classEquals($this, $dateTimeWithTimeZone)) {
86 1
            return false;
87
        }
88
89 4
        return $this->getDateTime()->sameValueAs($dateTimeWithTimeZone->getDateTime()) &&
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface ValueObjects\ValueObjectInterface as the method getDateTime() does only exist in the following implementations of said interface: ValueObjects\DateTime\DateTimeWithTimeZone.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
90 4
            $this->getTimeZone()->sameValueAs($dateTimeWithTimeZone->getTimeZone());
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface ValueObjects\ValueObjectInterface as the method getTimeZone() does only exist in the following implementations of said interface: ValueObjects\DateTime\DateTimeWithTimeZone.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
91
    }
92
93
    /**
94
     * Tells whether two DateTimeWithTimeZone represents the same timestamp
95
     *
96
     * @param ValueObjectInterface $dateTimeWithTimeZone
97
     *
98
     * @return bool
99
     */
100 1
    public function sameTimestampAs(ValueObjectInterface $dateTimeWithTimeZone)
101
    {
102 1
        if (false === Util::classEquals($this, $dateTimeWithTimeZone)) {
103 1
            return false;
104
        }
105
106 1
        return $this->toNativeDateTime() == $dateTimeWithTimeZone->toNativeDateTime();
0 ignored issues
show
Bug introduced by
It seems like you code against a concrete implementation and not the interface ValueObjects\ValueObjectInterface as the method toNativeDateTime() does only exist in the following implementations of said interface: ValueObjects\DateTime\Date, ValueObjects\DateTime\DateTime, ValueObjects\DateTime\DateTimeWithTimeZone, ValueObjects\DateTime\Time.

Let’s take a look at an example:

interface User
{
    /** @return string */
    public function getPassword();
}

class MyUser implements User
{
    public function getPassword()
    {
        // return something
    }

    public function getDisplayName()
    {
        // return some name.
    }
}

class AuthSystem
{
    public function authenticate(User $user)
    {
        $this->logger->info(sprintf('Authenticating %s.', $user->getDisplayName()));
        // do something.
    }
}

In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.

Available Fixes

  1. Change the type-hint for the parameter:

    class AuthSystem
    {
        public function authenticate(MyUser $user) { /* ... */ }
    }
    
  2. Add an additional type-check:

    class AuthSystem
    {
        public function authenticate(User $user)
        {
            if ($user instanceof MyUser) {
                $this->logger->info(/** ... */);
            }
    
            // or alternatively
            if ( ! $user instanceof MyUser) {
                throw new \LogicException(
                    '$user must be an instance of MyUser, '
                   .'other instances are not supported.'
                );
            }
    
        }
    }
    
Note: PHP Analyzer uses reverse abstract interpretation to narrow down the types inside the if block in such a case.
  1. Add the method to the interface:

    interface User
    {
        /** @return string */
        public function getPassword();
    
        /** @return string */
        public function getDisplayName();
    }
    
Loading history...
107
    }
108
109
    /**
110
     * Returns datetime from current DateTimeWithTimeZone
111
     *
112
     * @return DateTime
113
     */
114 8
    public function getDateTime()
115
    {
116 8
        return clone $this->dateTime;
117
    }
118
119
    /**
120
     * Returns timezone from current DateTimeWithTimeZone
121
     *
122
     * @return TimeZone
123
     */
124 8
    public function getTimeZone()
125
    {
126 8
        return clone $this->timeZone;
127
    }
128
129
    /**
130
     * Returns a native PHP \DateTime version of the current DateTimeWithTimeZone
131
     *
132
     * @return \DateTime
133
     */
134 2
    public function toNativeDateTime()
135
    {
136 2
        $date     = $this->getDateTime()->getDate();
137 2
        $time     = $this->getDateTime()->getTime();
138 2
        $year     = $date->getYear()->toNative();
139 2
        $month    = $date->getMonth()->getNumericValue();
140 2
        $day      = $date->getDay()->toNative();
141 2
        $hour     = $time->getHour()->toNative();
142 2
        $minute   = $time->getMinute()->toNative();
143 2
        $second   = $time->getSecond()->toNative();
144 2
        $timezone = $this->getTimeZone()->toNativeDateTimeZone();
145
146 2
        $dateTime = new \DateTime();
147 2
        $dateTime->setTimezone($timezone);
148 2
        $dateTime->setDate($year, $month, $day);
149 2
        $dateTime->setTime($hour, $minute, $second);
150
151 2
        return $dateTime;
152
    }
153
154
    function jsonSerialize()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
Comprehensibility Best Practice introduced by
It is recommend to declare an explicit visibility for jsonSerialize.

Generally, we recommend to declare visibility for all methods in your source code. This has the advantage of clearly communication to other developers, and also yourself, how this method should be consumed.

If you are not sure which visibility to choose, it is a good idea to start with the most restrictive visibility, and then raise visibility as needed, i.e. start with private, and only raise it to protected if a sub-class needs to have access, or public if an external class needs access.

Loading history...
155
    {
156
        return $this->toNativeDateTime();
157
    }
158
159
    /**
160
     * Returns DateTime as string in format "Y-n-j G:i:s e"
161
     *
162
     * @return string
163
     */
164 2
    public function __toString()
165
    {
166 2
        return \sprintf('%s %s', $this->getDateTime(), $this->getTimeZone());
167
    }
168
}
169