DateTime   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 138
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 9

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 12
lcom 2
cbo 9
dl 0
loc 138
ccs 40
cts 40
cp 1
rs 10
c 2
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A fromNative() 0 9 1
A fromNativeDateTime() 0 7 1
A now() 0 6 1
A __construct() 0 10 2
A sameValueAs() 0 8 3
A getDate() 0 4 1
A getTime() 0 4 1
A toNativeDateTime() 0 15 1
A __toString() 0 4 1
1
<?php
2
3
namespace ValueObjects\DateTime;
4
5
use ValueObjects\Util\Util;
6
use ValueObjects\ValueObjectInterface;
7
8
class DateTime implements ValueObjectInterface
9
{
10
    /** @var Date */
11
    protected $date;
12
13
    /** @var Time */
14
    protected $time;
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
     * @return DateTime
26
     */
27 2
    public static function fromNative()
28
    {
29 2
        $args = func_get_args();
30
31 2
        $date = Date::fromNative($args[0], $args[1], $args[2]);
32 2
        $time = Time::fromNative($args[3], $args[4], $args[5]);
33
34 2
        return new static($date, $time);
35
    }
36
37
    /**
38
     * Returns a new DateTime from a native PHP \DateTime
39
     *
40
     * @param  \DateTime $date_time
41
     * @return DateTime
42
     */
43 2
    public static function fromNativeDateTime(\DateTime $date_time)
44
    {
45 2
        $date = Date::fromNativeDateTime($date_time);
46 2
        $time = Time::fromNativeDateTime($date_time);
47
48 2
        return new static($date, $time);
49
    }
50
51
    /**
52
     * Returns current DateTime
53
     *
54
     * @return DateTime
55
     */
56 2
    public static function now()
57
    {
58 2
        $dateTime = new static(Date::now(), Time::now());
59
60 2
        return $dateTime;
61
    }
62
63
    /**
64
     * Returns a new DateTime object
65
     *
66
     * @param Date $date
67
     * @param Time $time
68
     */
69 18
    public function __construct(Date $date, Time $time = null)
70
    {
71 18
        $this->date = $date;
72
73 18
        if (null === $time) {
74 1
            $time = Time::zero();
75 1
        }
76
77 18
        $this->time = $time;
78 18
    }
79
80
    /**
81
     * Tells whether two DateTime are equal by comparing their values
82
     *
83
     * @param  ValueObjectInterface $date_time
84
     * @return bool
85
     */
86 8
    public function sameValueAs(ValueObjectInterface $date_time)
87
    {
88 8
        if (false === Util::classEquals($this, $date_time)) {
89 1
            return false;
90
        }
91
92 8
        return $this->getDate()->sameValueAs($date_time->getDate()) && $this->getTime()->sameValueAs($date_time->getTime());
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 getDate() does only exist in the following implementations of said interface: ValueObjects\DateTime\DateTime.

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...
Bug introduced by
It seems like you code against a concrete implementation and not the interface ValueObjects\ValueObjectInterface as the method getTime() does only exist in the following implementations of said interface: ValueObjects\DateTime\DateTime.

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...
93
    }
94
95
    /**
96
     * Returns date from current DateTime
97
     *
98
     * @return Date
99
     */
100 15
    public function getDate()
101
    {
102 15
        return clone $this->date;
103
    }
104
105
    /**
106
     * Returns time from current DateTime
107
     *
108
     * @return Time
109
     */
110 16
    public function getTime()
111
    {
112 16
        return clone $this->time;
113
    }
114
115
    /**
116
     * Returns a native PHP \DateTime version of the current DateTime.
117
     *
118
     * @return \DateTime
119
     */
120 1
    public function toNativeDateTime()
121
    {
122 1
        $year   = $this->getDate()->getYear()->toNative();
123 1
        $month  = $this->getDate()->getMonth()->getNumericValue();
124 1
        $day    = $this->getDate()->getDay()->toNative();
125 1
        $hour   = $this->getTime()->getHour()->toNative();
126 1
        $minute = $this->getTime()->getMinute()->toNative();
127 1
        $second = $this->getTime()->getSecond()->toNative();
128
129 1
        $dateTime = new \DateTime();
130 1
        $dateTime->setDate($year, $month, $day);
131 1
        $dateTime->setTime($hour, $minute, $second);
132
133 1
        return $dateTime;
134
    }
135
136
    /**
137
     * Returns DateTime as string in format "Y-n-j G:i:s"
138
     *
139
     * @return string
140
     */
141 4
    public function __toString()
142
    {
143 4
        return \sprintf('%s %s', $this->getDate(), $this->getTime());
144
    }
145
}
146