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); |
|
|
|
|
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
|
|
|
|
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.