Completed
Push — master ( 699dc6...d8f60e )
by Changwan
05:50
created

Date::fromText()   A

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
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Wandu\DateTime;
3
4
use Carbon\Carbon;
5
use DateTime;
6
use RuntimeException;
7
8
class Date
9
{
10
    /**
11
     * @param \Carbon\Carbon $carbon
12
     * @return \Wandu\DateTime\Date
13
     */
14 1
    public static function fromCarbon(Carbon $carbon)
15
    {
16 1
        if (!class_exists(Carbon::class)) {
17
            throw new RuntimeException('Unable to fromCarbon. the Carbon is not installed.');
18
        }
19 1
        return static::fromDates($carbon->year, $carbon->month, $carbon->day);
20
    }
21
22
    /**
23
     * @param string $text
24
     * @param \DateTimeZone|string|int $timezone
25
     * @return \Wandu\DateTime\Date
26
     */
27 1
    public static function fromText(string $text, $timezone = null)
28
    {
29 1
        return new Date($text, $timezone);
0 ignored issues
show
Unused Code introduced by
The call to Date::__construct() has too many arguments starting with $timezone.

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...
30
    }
31
32
    /**
33
     * @param int $year
34
     * @param int $month
35
     * @param int $day
36
     * @return \Wandu\DateTime\Date
37
     */
38 12
    public static function fromDates(int $year, int $month, int $day)
39
    {
40 12
        $datetime = new DateTime();
41 12
        $datetime->setDate($year, $month, $day);
42 12
        $datetime->setTime(0, 0, 0);
43 12
        return new Date($datetime);
44
    }
45
46
    /** @var \DateTime */
47
    protected $datetime;
48
49
    /**
50
     * @param string|\DateTime $time
51
     */
52 14
    public function __construct($time)
53
    {
54 14
        if ($time instanceof DateTime) {
55 12
            $this->datetime = $time;
56
        } else {
57 2
            $this->datetime = new DateTime($time);
58
        }
59 14
    }
60
    
61
    /**
62
     * @return string
63
     */
64 3
    public function __toString()
65
    {
66 3
        return $this->datetime->format('Y-m-d');
67
    }
68
69
    /**
70
     * @return int
71
     */
72 10
    public function year(): int
73
    {
74 10
        return (int)($this->datetime->format('Y'));
75
    }
76
77
    /**
78
     * @return int
79
     */
80 10
    public function month(): int
81
    {
82 10
        return (int)($this->datetime->format('n'));
83
    }
84
85
    /**
86
     * @return int
87
     */
88 10
    public function day(): int
89
    {
90 10
        return (int)($this->datetime->format('j'));
91
    }
92
}
93