Time::sameValueAs()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.2
c 0
b 0
f 0
cc 4
eloc 4
nc 4
nop 1
crap 4
1
<?php
2
3
namespace ValueObjects\DateTime;
4
5
use ValueObjects\Util\Util;
6
use ValueObjects\ValueObjectInterface;
7
8
class Time implements ValueObjectInterface
9
{
10
    /** @var Hour */
11
    protected $hour;
12
13
    /** @var Minute */
14
    protected $minute;
15
16
    /** @var Second */
17
    protected $second;
18
19
    /**
20
     * Returns a nee Time object from native int hour, minute and second
21
     *
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 self
26
     */
27 6
    public static function fromNative()
28
    {
29 6
        $args = func_get_args();
30
31 6
        $hour   = new Hour($args[0]);
32 6
        $minute = new Minute($args[1]);
33 6
        $second = new Second($args[2]);
34
35 6
        return new static($hour, $minute, $second);
36
    }
37
38
    /**
39
     * Returns a new Time from a native PHP \DateTime
40
     *
41
     * @param  \DateTime $time
42
     * @return self
43
     */
44 3
    public static function fromNativeDateTime(\DateTime $time)
45
    {
46 3
        $hour   = \intval($time->format('G'));
47 3
        $minute = \intval($time->format('i'));
48 3
        $second = \intval($time->format('s'));
49
50 3
        return static::fromNative($hour, $minute, $second);
51
    }
52
53
    /**
54
     * Returns current Time
55
     *
56
     * @return self
57
     */
58 3
    public static function now()
59
    {
60 3
        $time = new static(Hour::now(), Minute::now(), Second::now());
61
62 3
        return $time;
63
    }
64
65
    /**
66
     * Return zero time
67
     *
68
     * @return static
69
     */
70 2
    public static function zero()
71
    {
72 2
        $time = new static(new Hour(0), new Minute(0), new Second(0));
73
74 2
        return $time;
75
    }
76
77
    /**
78
     * Returns a new Time objects
79
     *
80
     * @param Hour   $hour
81
     * @param Minute $minute
82
     * @param Second $second
83
     */
84 28
    public function __construct(Hour $hour, Minute $minute, Second $second)
85
    {
86 28
        $this->hour   = $hour;
87 28
        $this->minute = $minute;
88 28
        $this->second = $second;
89 28
    }
90
91
    /**
92
     * Tells whether two Time are equal by comparing their values
93
     *
94
     * @param  ValueObjectInterface $time
95
     * @return bool
96
     */
97 13
    public function sameValueAs(ValueObjectInterface $time)
98
    {
99 13
        if (false === Util::classEquals($this, $time)) {
100 1
            return false;
101
        }
102
103 13
        return $this->getHour()->sameValueAs($time->getHour()) && $this->getMinute()->sameValueAs($time->getMinute()) && $this->getSecond()->sameValueAs($time->getSecond());
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 getHour() does only exist in the following implementations of said interface: 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...
Bug introduced by
It seems like you code against a concrete implementation and not the interface ValueObjects\ValueObjectInterface as the method getMinute() does only exist in the following implementations of said interface: 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...
Bug introduced by
It seems like you code against a concrete implementation and not the interface ValueObjects\ValueObjectInterface as the method getSecond() does only exist in the following implementations of said interface: 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...
104
    }
105
106
    /**
107
     * Get hour
108
     *
109
     * @return Hour
110
     */
111 24
    public function getHour()
112
    {
113 24
        return $this->hour;
114
    }
115
116
    /**
117
     * Get minute
118
     *
119
     * @return Minute
120
     */
121 24
    public function getMinute()
122
    {
123 24
        return $this->minute;
124
    }
125
126
    /**
127
     * Get second
128
     *
129
     * @return Second
130
     */
131 24
    public function getSecond()
132
    {
133 24
        return $this->second;
134
    }
135
136
    /**
137
     * Returns a native PHP \DateTime version of the current Time.
138
     * Date is set to current.
139
     *
140
     * @return \DateTime
141
     */
142 8
    public function toNativeDateTime()
143
    {
144 8
        $hour   = $this->getHour()->toNative();
145 8
        $minute = $this->getMinute()->toNative();
146 8
        $second = $this->getSecond()->toNative();
147
148 8
        $time = new \DateTime('now');
149 8
        $time->setTime($hour, $minute, $second);
150
151 8
        return $time;
152
    }
153
154
    /**
155
     * Returns time as string in format G:i:s
156
     *
157
     * @return string
158
     */
159 7
    public function __toString()
160
    {
161 7
        return $this->toNativeDateTime()->format('G:i:s');
162
    }
163
}
164