GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TestTime   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 2
dl 0
loc 59
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A freeze() 0 9 1
A freezeAtSecond() 0 6 1
A __call() 0 4 1
A __callStatic() 0 13 2
A getCarbon() 0 20 5
1
<?php
2
3
namespace Spatie\TestTime;
4
5
use Carbon\Carbon;
6
use Carbon\CarbonImmutable;
7
use InvalidArgumentException;
8
9
/**
10
 * @mixin \Carbon\Carbon
11
 *
12
 * @method static self create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null) Create a new Carbon instance from a specific date and time.
13
 * @method static self createFromDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to now.
14
 * @method static self createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today.
15
 * @method static self createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today.
16
 * @method static self createFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format.
17
 * @method static self years() Set current instance year to the given value.
18
 * @method static self year(int $value) Set current instance year to the given value.
19
 * @method static self setYears() Set current instance year to the given value.
20
 * @method static self setYear(int $value) Set current instance year to the given value.
21
 * @method static self months(int $value) Set current instance month to the given value.
22
 * @method static self month(int $value) Set current instance month to the given value.
23
 * @method static self setMonths(int $value) Set current instance month to the given value.
24
 * @method static self setMonth(int $value) Set current instance month to the given value.
25
 * @method static self days(int $value) Set current instance day to the given value.
26
 * @method static self day(int $value) Set current instance day to the given value.
27
 * @method static self setDays(int $value) Set current instance day to the given value.
28
 * @method static self setDay(int $value) Set current instance day to the given value.
29
 * @method static self hours(int $value) Set current instance hour to the given value.
30
 * @method static self hour(int $value) Set current instance hour to the given value.
31
 * @method static self setHours(int $value) Set current instance hour to the given value.
32
 * @method static self setHour(int $value) Set current instance hour to the given value.
33
 * @method static self minutes(int $value) Set current instance minute to the given value.
34
 * @method static self minute(int $value) Set current instance minute to the given value.
35
 * @method static self setMinutes(int $value) Set current instance minute to the given value.
36
 * @method static self setMinute(int $value) Set current instance minute to the given value.
37
 * @method static self seconds(int $value) Set current instance second to the given value.
38
 * @method static self second(int $value) Set current instance second to the given value.
39
 * @method static self setSeconds(int $value) Set current instance second to the given value.
40
 * @method static self setSecond(int $value) Set current instance second to the given value.
41
 * @method static self millis(int $value) Set current instance millisecond to the given value.
42
 * @method static self milli(int $value) Set current instance millisecond to the given value.
43
 * @method static self setMillis(int $value) Set current instance millisecond to the given value.
44
 * @method static self setMilli(int $value) Set current instance millisecond to the given value.
45
 * @method static self milliseconds(int $value) Set current instance millisecond to the given value.
46
 * @method static self millisecond(int $value) Set current instance millisecond to the given value.
47
 * @method static self setMilliseconds(int $value) Set current instance millisecond to the given value.
48
 * @method static self setMillisecond(int $value) Set current instance millisecond to the given value.
49
 * @method static self micros() Set current instance microsecond to the given value.
50
 * @method static self micro(int $value) Set current instance microsecond to the given value.
51
 * @method static self setMicros() Set current instance microsecond to the given value.
52
 * @method static self setMicro(int $value) Set current instance microsecond to the given value.
53
 * @method static self microseconds() Set current instance microsecond to the given value.
54
 * @method static self microsecond(int $value) Set current instance microsecond to the given value.
55
 * @method static self setMicroseconds() Set current instance microsecond to the given value.
56
 * @method static self setMicrosecond(int $value) Set current instance microsecond to the given value.
57
 * @method static self addDays(int $value = 1) Add days (the $value count passed in) to the instance (using date interval).
58
 * @method static self addDay() Add one day to the instance (using date interval).
59
 * @method static self subDays(int $value = 1) Sub days (the $value count passed in) to the instance (using date interval).
60
 * @method static self subDay() Sub one day to the instance (using date interval).
61
 * @method static self addHours(int $value = 1) Add hours (the $value count passed in) to the instance (using date interval).
62
 * @method static self addHour() Add one hour to the instance (using date interval).
63
 * @method static self subHours(int $value = 1) Sub hours (the $value count passed in) to the instance (using date interval).
64
 * @method static self subHour() Sub one hour to the instance (using date interval).
65
 * @method static self addMinutes(int $value = 1) Add minutes (the $value count passed in) to the instance (using date interval).
66
 * @method static self addMinute() Add one minute to the instance (using date interval).
67
 * @method static self subMinutes(int $value = 1) Sub minutes (the $value count passed in) to the instance (using date interval).
68
 * @method static self subMinute() Sub one minute to the instance (using date interval).
69
 * @method static self addSeconds(int $value = 1) Add seconds (the $value count passed in) to the instance (using date interval).
70
 * @method static self addSecond() Add one second to the instance (using date interval).
71
 * @method static self subSeconds(int $value = 1) Sub seconds (the $value count passed in) to the instance (using date interval).
72
 * @method static self subSecond() Sub one second to the instance (using date interval).
73
 * @method static self addMillis(int $value = 1) Add milliseconds (the $value count passed in) to the instance (using date interval).
74
 * @method static self addMilli() Add one millisecond to the instance (using date interval).
75
 * @method static self subMillis(int $value = 1) Sub milliseconds (the $value count passed in) to the instance (using date interval).
76
 * @method static self subMilli() Sub one millisecond to the instance (using date interval).
77
 * @method static self addMilliseconds(int $value = 1) Add milliseconds (the $value count passed in) to the instance (using date interval).
78
 * @method static self addMillisecond() Add one millisecond to the instance (using date interval).
79
 * @method static self subMilliseconds(int $value = 1) Sub milliseconds (the $value count passed in) to the instance (using date interval).
80
 * @method static self subMillisecond() Sub one millisecond to the instance (using date interval).
81
 * @method static self addMicros(int $value = 1) Add microseconds (the $value count passed in) to the instance (using date interval).
82
 * @method static self addMicro() Add one microsecond to the instance (using date interval).
83
 * @method static self subMicros(int $value = 1) Sub microseconds (the $value count passed in) to the instance (using date interval).
84
 * @method static self subMicro() Sub one microsecond to the instance (using date interval).
85
 * @method static self addMicroseconds(int $value = 1) Add microseconds (the $value count passed in) to the instance (using date interval).
86
 * @method static self addMicrosecond() Add one microsecond to the instance (using date interval).
87
 * @method static self subMicroseconds(int $value = 1) Sub microseconds (the $value count passed in) to the instance (using date interval).
88
 * @method static self subMicrosecond() Sub one microsecond to the instance (using date interval).
89
 * @method static self addMonths(int $value = 1) Add months (the $value count passed in) to the instance (using date interval).
90
 * @method static self addMonth() Add one month to the instance (using date interval).
91
 * @method static self subMonths(int $value = 1) Sub months (the $value count passed in) to the instance (using date interval).
92
 * @method static self subMonth() Sub one month to the instance (using date interval).
93
 * @method static self addYears(int $value = 1) Add years (the $value count passed in) to the instance (using date interval).
94
 * @method static self addYear() Add one year to the instance (using date interval).
95
 * @method static self subYears(int $value = 1) Sub years (the $value count passed in) to the instance (using date interval).
96
 * @method static self subYear() Sub one year to the instance (using date interval).
97
 */
98
class TestTime
99
{
100
    public static function freeze(): Carbon
101
    {
102
        $frozenTime = static::getCarbon(func_get_args());
103
104
        Carbon::setTestNow($frozenTime);
105
        CarbonImmutable::setTestNow($frozenTime);
106
107
        return $frozenTime;
108
    }
109
110
    public static function freezeAtSecond(): Carbon
111
    {
112
        $frozenTime = static::getCarbon(func_get_args())->startOfSecond();
113
114
        return static::freeze($frozenTime);
115
    }
116
117
    public function __call($name, $arguments)
118
    {
119
        return $this->__callStatic($name, $arguments);
120
    }
121
122
    public static function __callStatic($name, $arguments)
123
    {
124
        $result = (new Carbon)->$name(...$arguments);
125
126
        if (! $result instanceof Carbon) {
127
            return $result;
128
        }
129
130
        Carbon::setTestNow($result);
131
        CarbonImmutable::setTestNow($result);
132
133
        return new TestTime();
134
    }
135
136
    protected static function getCarbon(array $args): Carbon
137
    {
138
        if (count($args) === 0) {
139
            return Carbon::now();
140
        }
141
142
        if (count($args) === 1) {
143
            if (! $args[0] instanceof Carbon) {
144
                throw new InvalidArgumentException('You must pass a Carbon instance to `freeze`');
145
            }
146
147
            return $args[0];
148
        }
149
150
        if (count($args) === 2) {
151
            return Carbon::createFromFormat($args[0], $args[1]);
152
        }
153
154
        throw new InvalidArgumentException('You can only pass a maximum of two arguments to `freeze`');
155
    }
156
}
157