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.
Completed
Push — 3.0 ( 1625ef...3e6a09 )
by Vermeulen
02:21
created

Dates::humainReadable()   C

Complexity

Conditions 8
Paths 8

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 30
rs 5.3846
cc 8
eloc 18
nc 8
nop 1
1
<?php
2
3
namespace BFW;
4
5
use \DateTime;
6
use \Exception;
7
8
/**
9
 * Class for use easier DateTime
10
 */
11
class Dates extends DateTime
12
{
13
    /**
14
     * @var string[] $humainReadableI18n Words used in method to transform
15
     *      date difference to humain readable.
16
     */
17
    protected static $humainReadableI18n = [
18
        'now'       => 'now',
19
        'since'     => 'since',
20
        'yesterday' => 'yesterday',
21
        'the'       => 'the',
22
        'at'        => 'at'
23
    ];
24
25
    /**
26
     * @var string[] $humainReadableFormats Date and time format used in
27
     *      method to transform date difference to humain readable.
28
     */
29
    protected static $humainReadableFormats = [
30
        'dateSameYear'      => 'm-d',
31
        'dateDifferentYear' => 'Y-m-d',
32
        'time'              => 'H:i'
33
    ];
34
35
    /**
36
     * Return attribute humainReadableI18n value
37
     * 
38
     * @return string[]
39
     */
40
    public static function getHumainReadableI18n()
41
    {
42
        return self::$humainReadableI18n;
43
    }
44
45
    /**
46
     * Define new value to a key in attribute humainReadableI18n
47
     * 
48
     * @param string $key The key in humainReadableI18n
49
     * @param string $value The new value for key
50
     * 
51
     * @return void
52
     */
53
    public static function setHumainReadableI18nKey($key, $value)
54
    {
55
        self::$humainReadableI18n[$key] = $value;
56
    }
57
58
    /**
59
     * Define new value to the attribute humainReadableI18n
60
     * 
61
     * @param string[] $value The new value for attribute
62
     * 
63
     * @return void
64
     */
65
    public static function setHumainReadableI18n($value)
66
    {
67
        self::$humainReadableI18n = $value;
68
    }
69
70
    /**
71
     * Return attribute humainReadableFormats value
72
     * 
73
     * @return string[]
74
     */
75
    public static function getHumainReadableFormats()
76
    {
77
        return self::$humainReadableFormats;
78
    }
79
80
    /**
81
     * Define new value to a key in attribute humainReadableFormats
82
     * 
83
     * @param string $key The key in humainReadableFormats
84
     * @param string $value The new value for key
85
     * 
86
     * @return void
87
     */
88
    public static function setHumainReadableFormatsKey($key, $value)
89
    {
90
        self::$humainReadableFormats[$key] = $value;
91
    }
92
93
    /**
94
     * Define new value to the attribute humainReadableFormats
95
     * 
96
     * @param string[] $value The new value for attribute
97
     * 
98
     * @return void
99
     */
100
    public static function setHumainReadableFormats($value)
101
    {
102
        self::$humainReadableFormats = $value;
103
    }
104
105
    /**
106
     * Return the date. Format is Y-m-d H:i:sO
107
     * 
108
     * @return string
109
     */
110
    public function getDate()
111
    {
112
        return parent::format('Y-m-d H:i:sO');
113
    }
114
115
    /**
116
     * Return a full numeric representation of a year, 4 digits
117
     * 
118
     * @return int
119
     */
120
    public function getYear()
121
    {
122
        return (int) parent::format('Y');
123
    }
124
125
    /**
126
     * Return the numeric representation of a month, with leading zeros
127
     * 
128
     * @return int
129
     */
130
    public function getMonth()
131
    {
132
        return (int) parent::format('m');
133
    }
134
135
    /**
136
     * Return the day of the month, 2 digits with leading zeros
137
     * 
138
     * @return int
139
     */
140
    public function getDay()
141
    {
142
        return (int) parent::format('d');
143
    }
144
145
    /**
146
     * Return 24-hour format with leading zeros. 2 digits
147
     * 
148
     * @return int
149
     */
150
    public function getHour()
151
    {
152
        return (int) parent::format('H');
153
    }
154
155
    /**
156
     * Return minutes, with leading zeros. 2 digits
157
     * 
158
     * @return int
159
     */
160
    public function getMinute()
161
    {
162
        return (int) parent::format('i');
163
    }
164
165
    /**
166
     * Return second, with leading zeros. 2 digits
167
     * 
168
     * @return int
169
     */
170
    public function getSecond()
171
    {
172
        return (int) parent::format('s');
173
    }
174
175
    /**
176
     * Return the difference to Greenwich time (GMT)
177
     * with colon between hours and minutes
178
     * 
179
     * @return string
180
     */
181
    public function getZone()
182
    {
183
        return parent::format('P');
184
    }
185
186
    /**
187
     * Override modify DateTime method to allow personnal keyword
188
     * 
189
     * @param string $modify A date/time string
190
     * 
191
     * @return \BFW\Dates
192
     */
193
    public function modify($modify)
194
    {
195
        $dateDepart = clone $this;
196
        @parent::modify($modify); //Yeurk, but for personnal pattern, no choice
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
197
198
        if ($dateDepart != $this) {
199
            return $this;
200
        }
201
202
        $this->modifyOthersKeywords($modify);
203
204
        return $this;
205
    }
206
207
    /**
208
     * Get DateTime equivalent keyword for a personal keyword
209
     * 
210
     * @return \stdClass
211
     */
212
    protected function getModifyOthersKeywors()
213
    {
214
        //Liste des possibilités qu'on permet
215
        $search = [
216
            'an', 'ans',
217
            'mois',
218
            'jour', 'jours',
219
            'heure', 'heures',
220
            'minutes',
221
            'seconde', 'secondes'
222
        ];
223
224
        //Liste des équivalent pour la fonction modify de DateTime
225
        $replace = [
226
            'year', 'year',
227
            'month',
228
            'day', 'day',
229
            'hour', 'hour',
230
            'minute',
231
            'second', 'second'
232
        ];
233
        
234
        return (object) [
235
            'search' => $search,
236
            'replace' => $replace
237
        ];
238
    }
239
    
240
    /**
241
     * Use personal keyword on modify method
242
     * 
243
     * @param string $modify A date/time string
244
     * 
245
     * @throws Exception If bad pattern or unknown keyword
246
     */
247
    protected function modifyOthersKeywords($modify)
248
    {
249
        $keywords = $this->getModifyOthersKeywors();
250
        $match    = [];
251
        
252
        //Regex sur le paramètre pour récupéré le type de modification
253
        if (preg_match('#(\+|\-)([0-9]+) ([a-z]+)#i', $modify, $match) !== 1) {
254
            throw new Exception('Dates::modify pattern not match.');
255
        }
256
        
257
        $keyword = str_replace(
258
            $keywords->search,
259
            $keywords->replace,
260
            strtolower($match[3])
261
        );
262
        
263
        $dateDepart = clone $this;
264
        //Yeurk, but I preferer sends an Exception, not an error
265
        @parent::modify($match[1].$match[2].' '.$keyword);
1 ignored issue
show
Comprehensibility Bug introduced by
It seems like you call parent on a different method (modify() instead of modifyOthersKeywords()). Are you sure this is correct? If so, you might want to change this to $this->modify().

This check looks for a call to a parent method whose name is different than the method from which it is called.

Consider the following code:

class Daddy
{
    protected function getFirstName()
    {
        return "Eidur";
    }

    protected function getSurName()
    {
        return "Gudjohnsen";
    }
}

class Son
{
    public function getFirstName()
    {
        return parent::getSurname();
    }
}

The getFirstName() method in the Son calls the wrong method in the parent class.

Loading history...
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
266
        
267
        if ($dateDepart == $this) {
268
            throw new Exception(
269
                'Dates::modify Parameter '.$match[3].' is unknown.'
270
            );
271
        }
272
    }
273
    
274
    /**
275
     * Return date's SQL format (postgresql format).
276
     * The return Must be an array or a string.
277
     * 
278
     * @param boolean $returnArray (default false) True to return an array.
279
     * 
280
     * @return string[]|string
281
     */
282
    public function getSqlFormat($returnArray = false)
283
    {
284
        $date  = $this->format('Y-m-d');
285
        $heure = $this->format('H:i:s');
286
287
        if ($returnArray) {
288
            return [$date, $heure];
289
        }
290
291
        return $date.' '.$heure;
292
    }
293
294
    /**
295
     * List all timezone existing in current php version
296
     * 
297
     * @return string[]
298
     */
299
    public function lstTimeZone()
300
    {
301
        return parent::getTimezone()->listIdentifiers();
302
    }
303
304
    /**
305
     * List all continent define in php DateTimeZone.
306
     * 
307
     * @return string[]
308
     */
309
    public function lstTimeZoneContinent()
310
    {
311
        return [
312
            'africa',
313
            'america',
314
            'antartica',
315
            'arctic',
316
            'asia',
317
            'atlantic',
318
            'australia',
319
            'europe',
320
            'indian',
321
            'pacific'
322
        ];
323
    }
324
325
    /**
326
     * Liste des pays possible pour un continent donné
327
     * 
328
     * @param string $continent Le continent dans lequel on veux la liste des pays
329
     * 
330
     * @return array La liste des pays pour le continent donné
331
     */
332
    
333
    /**
334
     * List all available country for a continent
335
     * 
336
     * @param string $continent
337
     * 
338
     * @return string[]
339
     */
340
    public function lstTimeZonePays($continent)
341
    {
342
        $lst_all = $this->lstTimeZone();
343
        $return  = [];
344
345
        foreach ($lst_all as $val) {
346
            $pos = strpos($val, $continent);
347
348
            if ($pos !== false) {
349
                $return[] = $val;
350
            }
351
        }
352
353
        return $return;
354
    }
355
356
    /**
357
     * Transform a date to a format humain readable
358
     * 
359
     * @param boolean $returnDateAndTime (default true) True to return date and
360
     *      time concat with a space. False to have only date.
361
     * @param boolean $toLower (default false) True to have return a lower text
0 ignored issues
show
Bug introduced by
There is no parameter named $toLower. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
362
     * 
363
     * @return string
364
     */
365
    public function humainReadable($returnDateAndTime = true)
366
    {
367
        $actual = new Dates;
368
        $diff   = parent::diff($actual);
369
370
        $returnTxt = (object) [
371
            'date' => '',
372
            'time' => ''
373
        ];
374
375
        if ($actual == $this) {
376
            //A l'instant
377
            $this->humainDateNow($returnTxt);
378
        } elseif ($diff->d === 1 && $diff->m === 0 && $diff->y === 0) {
379
            //Hier
380
            $this->humainDateYesterday($returnTxt);
381
        } elseif ($diff->days === 0) {
382
            //Aujourd'hui
383
            $this->humainDateToday($returnTxt, $diff);
384
        } else {
385
            $this->humainDateOther($returnTxt, $actual);
386
        }
387
388
        $txtReturn = $returnTxt->date;
389
        if ($returnDateAndTime === true && $returnTxt->time !== '') {
390
            $txtReturn .= ' '.$returnTxt->time;
391
        }
392
393
        return $txtReturn;
394
    }
395
    
396
    /**
397
     * Format date to humain readable when date is now
398
     * 
399
     * @param \stdClas $returnTxt Text returned by humainReadable
400
     * 
401
     * @return void
402
     */
403
    protected function humainDateNow(&$returnTxt)
404
    {
405
        $currentClass    = get_called_class();
406
        $returnTxt->date = $currentClass::$humainReadableI18n['now'];
407
    }
408
    
409
    /**
410
     * Format date to humain readable when date is today
411
     * 
412
     * @param \stdClas $returnTxt Text returned by humainReadable
413
     * @param \DateInterval $diff Interval between now and date to read
414
     * 
415
     * @return void
416
     */
417
    protected function humainDateToday(&$returnTxt, $diff)
418
    {
419
        $currentClass    = get_called_class();
420
        $returnTxt->date = $currentClass::$humainReadableI18n['since'].' ';
421
422
        if ($diff->h === 0 && $diff->i === 0) {
423
            $returnTxt->date .= $diff->s.'s';
424
        } elseif ($diff->h === 0) {
425
            $returnTxt->date .= $diff->i.'min';
426
        } else {
427
            $returnTxt->date .= $diff->h.'h';
428
        }
429
    }
430
    
431
    /**
432
     * Format date to humain readable when date is yesterday
433
     * 
434
     * @param \stdClas $returnTxt Text returned by humainReadable
435
     * 
436
     * @return void
437
     */
438
    protected function humainDateYesterday(&$returnTxt)
439
    {
440
        $currentClass    = get_called_class();
441
        $returnTxt->date = $currentClass::$humainReadableI18n['yesterday'];
442
        $returnTxt->time = $currentClass::$humainReadableI18n['at']
443
            .' '
444
            .$this->format(
445
                $currentClass::$humainReadableFormats['time']
446
            );
447
    }
448
    
449
    /**
450
     * Format date to humain readable when date is not now, today or yesterday
451
     * 
452
     * @param \stdClas $returnTxt Text returned by humainReadable
453
     * @param \DateTime $actual DateTime object for now
454
     * 
455
     * @return void
456
     */
457
    protected function humainDateOther(&$returnTxt, $actual)
458
    {
459
        $currentClass = get_called_class();
460
        
461
        $dateFormat = $currentClass::$humainReadableFormats['dateDifferentYear'];
462
        if ($actual->format('Y') === $this->format('Y')) {
463
            $dateFormat = $currentClass::$humainReadableFormats['dateSameYear'];
464
        }
465
466
        $returnTxt->date = $currentClass::$humainReadableI18n['the']
467
            .' '
468
            .$this->format($dateFormat);
469
470
        $returnTxt->time = $currentClass::$humainReadableI18n['at']
471
            .' '
472
            .$this->format(
473
                $currentClass::$humainReadableFormats['time']
474
            );
475
    }
476
}
477