DateIntervalHelper   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 3
eloc 16
c 1
b 0
f 1
dl 0
loc 31
ccs 0
cts 18
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A toString() 0 25 3
1
<?php
2
3
namespace rhertogh\Yii2Oauth2Server\helpers;
4
5
class DateIntervalHelper
6
{
7
    /**
8
     * @param \DateInterval|null $interval
9
     * @return string|null
10
     */
11
    public static function toString($interval)
12
    {
13
14
        if ($interval === null) {
15
            return null;
16
        }
17
18
        $dateParts = array_filter([
19
            'Y' => $interval->y,
20
            'M' => $interval->m,
21
            'D' => $interval->d,
22
        ]);
23
        $result = 'P' . implode(array_map(fn($v, $k) => $v . $k, $dateParts, array_keys($dateParts)));
24
25
        $timeParts = array_filter([
26
            'H' => $interval->h,
27
            'M' => $interval->i,
28
            'S' => $interval->s,
29
            'F' => $interval->f,
30
        ]);
31
        if ($timeParts) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $timeParts of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
32
            $result .= 'T' . implode(array_map(fn($v, $k) => $v . $k, $timeParts, array_keys($timeParts)));
33
        }
34
35
        return $result;
36
    }
37
}
38