Time::fromNative()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 6
cts 6
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 5
nc 1
nop 1
crap 1
1
<?php
2
3
namespace BestServedCold\PhalueObjects\DateTime;
4
5
use BestServedCold\PhalueObjects\DateTime\Unit\Hour;
6
use BestServedCold\PhalueObjects\DateTime\Unit\Minute;
7
use BestServedCold\PhalueObjects\DateTime\Unit\Second;
8
use BestServedCold\PhalueObjects\Variadic;
9
use BestServedCold\PhalueObjects\Contract\DateTime as DateTimeInterface;
10
11
/**
12
 * Class Time
13
 *
14
 * @package   BestServedCold\PhalueObjects\DateTime
15
 * @author    Adam Lewis <[email protected]>
16
 * @copyright Copyright (c) 2015 Best Served Cold Media Limited
17
 * @license   http://http://opensource.org/licenses/GPL-3.0 GPL License
18
 * @link      http://bestservedcold.com
19
 * @since     0.0.1-alpha
20
 * @version   0.0.2-alpha
21
 */
22
class Time extends Variadic implements DateTimeInterface
23
{
24
    use DateTimeTrait;
25
26
    /**
27
     * @var Hour
28
     */
29
    protected $hour;
30
31
    /**
32
     * @var Minute
33
     */
34
    protected $minute;
35
36
    /**
37
     * @var Second
38
     */
39
    protected $second;
40
41
    /**
42
     * @param Hour $hour
43
     * @param Minute $minute
44
     * @param Second $second
45
     */
46 17
    public function __construct(Hour $hour, Minute $minute, Second $second)
47
    {
48 17
        $this->hour = $hour;
49 17
        $this->minute = $minute;
50 17
        $this->second = $second;
51 17
        $this->timestamp = $hour->getSeconds()->add($minute->getSeconds())->add($second);
0 ignored issues
show
Documentation Bug introduced by
It seems like $hour->getSeconds()->add...econds())->add($second) of type object<BestServedCold\Ph...cts\DateTime\Unit\Hour> is incompatible with the declared type integer of property $timestamp.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
52 17
        $this->native = self::getNowDateTime()
53 17
            ->setTime($hour->getValue(), $minute->getValue(), $minute->getValue()
54 17
        );
55
56 17
        parent::__construct($hour, $minute, $second);
57 17
    }
58
59
    /**
60
     * @return static
61
     */
62 7
    public static function now()
63
    {
64 7
        return new static(Hour::now(), Minute::now(), Second::now());
65
    }
66
67
    /**
68
     * @param \DateTime $dateTime
69
     * @return static
70
     */
71 4
    public static function fromNative(\DateTime $dateTime)
72
    {
73 4
        return new static(
74 4
            new Hour((int) $dateTime->format('G')),
75 4
            new Minute((int) $dateTime->format('i')),
76 4
            new Second((int) $dateTime->format('s'))
77 4
        );
78
    }
79
80
    /**
81
     * @return Hour
82
     */
83 1
    public function getHour()
84
    {
85 1
        return $this->hour;
86
    }
87
88
    /**
89
     * @return Minute
90
     */
91 1
    public function getMinute()
92
    {
93 1
        return $this->minute;
94
    }
95
96
    /**
97
     * @return Second
98
     */
99 1
    public function getSecond()
100
    {
101 1
        return $this->second;
102
    }
103
104
    /**
105
     * @return int
106
     */
107 12
    public function getTimestamp()
108
    {
109 12
        return $this->timestamp->getValue();
0 ignored issues
show
Bug introduced by
The method getValue cannot be called on $this->timestamp (of type integer).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
110
    }
111
112
    /**
113
     * @return int
114
     */
115 1
    public function getValue()
116
    {
117 1
        return $this->getTimestamp();
118
    }
119
120
    /**
121
     * @return string
122
     */
123 12
    public function __toString()
124
    {
125 12
        return $this->hour.':'.$this->minute.':'.$this->second;
126
    }
127
}
128