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.
Passed
Push — 3.0 ( 9ca0ec...9f6cf7 )
by Vermeulen
02:15
created

Dates::getModifyNewKeywords()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace BFW\Helpers;
4
5
use \DateTime;
6
use \Exception;
7
8
/**
9
 * Class to have shortcuts to DateTime(Zone) methods and to display a date
10
 * with words and not only numbers (today, yesterday, since... etc).
11
 */
12
class Dates extends DateTime
13
{
14
    /**
15
     * @const ERR_MODIFY_PATTERN_NOT_MATCH Exception code if the pattern used
16
     * into the method modify() not match with the regex.
17
     */
18
    const ERR_MODIFY_PATTERN_NOT_MATCH = 1605001;
19
    
20
    /**
21
     * @const ERR_MODIFY_UNKNOWN_MODIFIER Exception code if the modifier used
22
     * into the method modify() is unknown.
23
     */
24
    const ERR_MODIFY_UNKNOWN_MODIFIER = 1605002;
25
    
26
    /**
27
     * @var string[] $humanReadableI18n Words used in method to transform
28
     *  date difference to human readable.
29
     */
30
    protected static $humanReadableI18n = [
31
        'now'       => 'now',
32
        'since'     => 'since',
33
        'in'        => 'in',
34
        'yesterday' => 'yesterday',
35
        'tomorrow'  => 'tomorrow',
36
        'the'       => 'the',
37
        'at'        => 'at'
38
    ];
39
40
    /**
41
     * @var string[] $humanReadableFormats Date and time formats used in
42
     *  method to transform date difference to human readable.
43
     */
44
    protected static $humanReadableFormats = [
45
        'dateSameYear'      => 'm-d',
46
        'dateDifferentYear' => 'Y-m-d',
47
        'time'              => 'H:i'
48
    ];
49
50
    /**
51
     * Return the value of the humanReadableI18n property
52
     * 
53
     * @return string[]
54
     */
55
    public static function getHumanReadableI18n(): array
56
    {
57
        return self::$humanReadableI18n;
58
    }
59
60
    /**
61
     * Define a new value for a key of the humanReadableI18n property
62
     * 
63
     * @param string $key The key in humanReadableI18n
64
     * @param string $value The new value for the key
65
     * 
66
     * @return void
67
     */
68
    public static function setHumanReadableI18nKey(string $key, string $value)
69
    {
70
        self::$humanReadableI18n[$key] = $value;
71
    }
72
73
    /**
74
     * Define a new value to the property humanReadableI18n
75
     * 
76
     * @param string[] $value The new value for the property
77
     * 
78
     * @return void
79
     */
80
    public static function setHumanReadableI18n(array $value)
81
    {
82
        self::$humanReadableI18n = $value;
83
    }
84
85
    /**
86
     * Return the value of the humanReadableFormats property
87
     * 
88
     * @return string[]
89
     */
90
    public static function getHumanReadableFormats(): array
91
    {
92
        return self::$humanReadableFormats;
93
    }
94
95
    /**
96
     * Define a new value for a key of the humanReadableFormats property
97
     * 
98
     * @param string $key The key in humanReadableFormats
99
     * @param string $value The new value for the key
100
     * 
101
     * @return void
102
     */
103
    public static function setHumanReadableFormatsKey(
104
        string $key,
105
        string $value
106
    ) {
107
        self::$humanReadableFormats[$key] = $value;
108
    }
109
110
    /**
111
     * Define a new value to the property humanReadableFormats
112
     * 
113
     * @param string[] $value The new value for the property
114
     * 
115
     * @return void
116
     */
117
    public static function setHumanReadableFormats(array $value)
118
    {
119
        self::$humanReadableFormats = $value;
120
    }
121
122
    /**
123
     * Return the date. Format is Y-m-d H:i:sO
124
     * 
125
     * @return string
126
     */
127
    public function getDate(): string
128
    {
129
        return parent::format('Y-m-d H:i:sO');
130
    }
131
132
    /**
133
     * Return a numeric representation of a year, 4 digits.
134
     * 
135
     * @return int
136
     */
137
    public function getYear(): int
138
    {
139
        return (int) parent::format('Y');
140
    }
141
142
    /**
143
     * Return the numeric representation of a month, without leading zeros.
144
     * The returned int format can not have leading zeros.
145
     * 
146
     * @return int
147
     */
148
    public function getMonth(): int
149
    {
150
        return (int) parent::format('m');
151
    }
152
153
    /**
154
     * Return the day of the month without leading zeros.
155
     * The returned int format can not have leading zeros.
156
     * 
157
     * @return int
158
     */
159
    public function getDay(): int
160
    {
161
        return (int) parent::format('d');
162
    }
163
164
    /**
165
     * Return 24-hour format without leading zeros.
166
     * The returned int format can not have leading zeros.
167
     * 
168
     * @return int
169
     */
170
    public function getHour(): int
171
    {
172
        return (int) parent::format('H');
173
    }
174
175
    /**
176
     * Return minutes, without leading zeros.
177
     * The returned int format can not have leading zeros.
178
     * 
179
     * @return int
180
     */
181
    public function getMinute(): int
182
    {
183
        return (int) parent::format('i');
184
    }
185
186
    /**
187
     * Return second, without leading zeros.
188
     * The returned int format can not have leading zeros.
189
     * 
190
     * @return int
191
     */
192
    public function getSecond(): int
193
    {
194
        return (int) parent::format('s');
195
    }
196
197
    /**
198
     * Return the difference to Greenwich time (GMT)
199
     * with colon between hours and minutes
200
     * 
201
     * @return string
202
     */
203
    public function getZone(): string
204
    {
205
        return parent::format('P');
206
    }
207
    
208
    /**
209
     * Return date's SQL format (postgresql format).
210
     * The return can be an array or a string.
211
     * 
212
     * @param boolean $returnArray (default false) True to return an array.
213
     * @param boolean $withZone (default false) True to include the timezone
214
     *  into the time returned data.
215
     * 
216
     * @return string[]|string
217
     */
218
    public function getSqlFormat(
219
        bool $returnArray = false,
220
        bool $withZone = false
221
    ) {
222
        $date = $this->format('Y-m-d');
223
        $time = $this->format('H:i:s');
224
        
225
        if ($withZone === true) {
226
            $time .= $this->format('O');
227
        }
228
229
        if ($returnArray) {
230
            return [$date, $time];
231
        }
232
233
        return $date.' '.$time;
234
    }
235
236
    /**
237
     * List all timezone existing in current php version
238
     * 
239
     * @return string[]
240
     */
241
    public function lstTimeZone(): array
242
    {
243
        return parent::getTimezone()->listIdentifiers();
244
    }
245
246
    /**
247
     * List all continent define in php DateTimeZone.
248
     * 
249
     * @return string[]
250
     */
251
    public function lstTimeZoneContinent(): array
252
    {
253
        return [
254
            'Africa',
255
            'America',
256
            'Antartica',
257
            'Arctic',
258
            'Asia',
259
            'Atlantic',
260
            'Australia',
261
            'Europe',
262
            'Indian',
263
            'Pacific'
264
        ];
265
    }
266
267
    /**
268
     * List all available country for a continent
269
     * 
270
     * @param string $continent The continent for which we want
271
     *  the countries list
272
     * 
273
     * @return string[]
274
     */
275
    public function lstTimeZoneCountries(string $continent): array
276
    {
277
        $allCountries = $this->lstTimeZone();
278
        $countries    = [];
279
280
        foreach ($allCountries as $country) {
281
            if (strpos($country, $continent) !== false) {
282
                $countries[] = $country;
283
            }
284
        }
285
286
        return $countries;
287
    }
288
289
    /**
290
     * Transform a date to a human readable format
291
     * 
292
     * @param boolean $returnDateAndTime (default true) True to return date and
293
     *  time concatenated with a space. False to have only date.
294
     * 
295
     * @return string
296
     */
297
    public function humanReadable(bool $returnDateAndTime = true): string
298
    {
299
        $current = new Dates;
300
        $diff    = parent::diff($current);
301
        
302
        $parsedTxt = new class {
303
            public $date = '';
304
            public $time = '';
305
        };
306
307
        if ($current == $this) {
308
            //Now
309
            $this->humanDateNow($parsedTxt);
310
        } elseif ($diff->d === 1 && $diff->m === 0 && $diff->y === 0) {
311
            if ($diff->invert === 0) {
312
                $this->humanDateYesterday($parsedTxt); //Yesterday
313
            } else {
314
                $this->humanDateTomorrow($parsedTxt); //Tomorrow
315
            }
316
        } elseif ($diff->days === 0) {
317
            //Today
318
            $this->humanDateToday($parsedTxt, $diff);
0 ignored issues
show
Bug introduced by
It seems like $diff can also be of type false; however, parameter $diff of BFW\Helpers\Dates::humanDateToday() does only seem to accept DateInterval, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

318
            $this->humanDateToday($parsedTxt, /** @scrutinizer ignore-type */ $diff);
Loading history...
319
        } else {
320
            $this->humanDateOther($parsedTxt, $current);
321
        }
322
323
        $txtReturned = $parsedTxt->date;
324
        if ($returnDateAndTime === true && $parsedTxt->time !== '') {
325
            $txtReturned .= ' '.$parsedTxt->time;
326
        }
327
328
        return $txtReturned;
329
    }
330
    
331
    /**
332
     * Format date to human readable when the date is now
333
     * 
334
     * @param object $parsedTxt Texts returned by humanReadable method
335
     * 
336
     * @return void
337
     */
338
    protected function humanDateNow($parsedTxt)
339
    {
340
        $currentClass    = get_called_class();
341
        $parsedTxt->date = $currentClass::$humanReadableI18n['now'];
0 ignored issues
show
Bug introduced by
The property humanReadableI18n does not exist on string.
Loading history...
342
    }
343
    
344
    /**
345
     * Format date to human readable when date is today
346
     * 
347
     * @param object $parsedTxt Texts returned by humanReadable method
348
     * @param \DateInterval $diff Interval between now and date to read
349
     * 
350
     * @return void
351
     */
352
    protected function humanDateToday($parsedTxt, \DateInterval $diff)
353
    {
354
        $textKey = 'since';
355
        if ($diff->invert === 1) {
356
            $textKey = 'in';
357
        }
358
        
359
        $currentClass    = get_called_class();
360
        $parsedTxt->date = $currentClass::$humanReadableI18n[$textKey].' ';
0 ignored issues
show
Bug introduced by
The property humanReadableI18n does not exist on string.
Loading history...
361
362
        if ($diff->h === 0 && $diff->i === 0) {
363
            $parsedTxt->date .= $diff->s.'s';
364
        } elseif ($diff->h === 0) {
365
            $parsedTxt->date .= $diff->i.'min';
366
        } else {
367
            $parsedTxt->date .= $diff->h.'h';
368
        }
369
    }
370
    
371
    /**
372
     * Format date to human readable when date is yesterday
373
     * 
374
     * @param object $parsedTxt Texts returned by humanReadable method
375
     * 
376
     * @return void
377
     */
378
    protected function humanDateYesterday($parsedTxt)
379
    {
380
        $currentClass    = get_called_class();
381
        $parsedTxt->date = $currentClass::$humanReadableI18n['yesterday'];
0 ignored issues
show
Bug introduced by
The property humanReadableI18n does not exist on string.
Loading history...
382
        $parsedTxt->time = $currentClass::$humanReadableI18n['at']
383
            .' '
384
            .$this->format(
385
                $currentClass::$humanReadableFormats['time']
0 ignored issues
show
Bug introduced by
The property humanReadableFormats does not exist on string.
Loading history...
386
            );
387
    }
388
    
389
    /**
390
     * Format date to human readable when date is tomorrow
391
     * 
392
     * @param object $parsedTxt Texts returned by humanReadable method
393
     * 
394
     * @return void
395
     */
396
    protected function humanDateTomorrow($parsedTxt)
397
    {
398
        $currentClass    = get_called_class();
399
        $parsedTxt->date = $currentClass::$humanReadableI18n['tomorrow'];
0 ignored issues
show
Bug introduced by
The property humanReadableI18n does not exist on string.
Loading history...
400
        $parsedTxt->time = $currentClass::$humanReadableI18n['at']
401
            .' '
402
            .$this->format(
403
                $currentClass::$humanReadableFormats['time']
0 ignored issues
show
Bug introduced by
The property humanReadableFormats does not exist on string.
Loading history...
404
            );
405
    }
406
    
407
    /**
408
     * Format date to human readable when date is not now, today or yesterday
409
     * 
410
     * @param object $parsedTxt Texts returned by humanReadable method
411
     * @param \DateTime $current DateTime object for now
412
     * 
413
     * @return void
414
     */
415
    protected function humanDateOther($parsedTxt, \DateTime $current)
416
    {
417
        $currentClass = get_called_class();
418
        
419
        $dateFormat = $currentClass::$humanReadableFormats['dateDifferentYear'];
0 ignored issues
show
Bug introduced by
The property humanReadableFormats does not exist on string.
Loading history...
420
        if ($current->format('Y') === $this->format('Y')) {
421
            $dateFormat = $currentClass::$humanReadableFormats['dateSameYear'];
422
        }
423
424
        $parsedTxt->date = $currentClass::$humanReadableI18n['the']
0 ignored issues
show
Bug introduced by
The property humanReadableI18n does not exist on string.
Loading history...
425
            .' '
426
            .$this->format($dateFormat);
427
428
        $parsedTxt->time = $currentClass::$humanReadableI18n['at']
429
            .' '
430
            .$this->format(
431
                $currentClass::$humanReadableFormats['time']
432
            );
433
    }
434
}
435