Completed
Pull Request — master (#27)
by
unknown
03:00
created

TimeSpeller::spellInterval()   D

Complexity

Conditions 10
Paths 28

Size

Total Lines 42
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 22
CRAP Score 10.1728

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 29
c 1
b 0
f 0
nc 28
nop 3
dl 0
loc 42
ccs 22
cts 25
cp 0.88
crap 10.1728
rs 4.8196

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace morphos;
3
4
use DateInterval;
5
6
abstract class TimeSpeller
7
{
8
    const YEAR = 'year';
9
    const MONTH = 'month';
10
    const DAY = 'day';
11
    const HOUR = 'hour';
12
    const MINUTE = 'minute';
13
    const SECOND = 'second';
14
15
    const AGO = 'ago';
16
    const IN = 'in';
17
18
    const AND_WORD = 'and';
19
20
    const JUST_NOW = 'just now';
21
22
    const DIRECTION = 1;
23
    const SEPARATE = 2;
24
25
    public static function spellUnit($count, $unit) {}
26
27 16
    public static function spellInterval(DateInterval $interval, $options = 0, $limit = 0)
28
    {
29 16
        $parts = [];
30 16
        $k = 0;
31
        foreach ([
32 16
            'y' => self::YEAR,
33 16
            'm' => self::MONTH,
34 16
            'd' => self::DAY,
35 16
            'h' => self::HOUR,
36 16
            'i' => self::MINUTE,
37 16
            's' => self::SECOND
38
        ] as $interval_field => $unit) {
39 16
            if ($interval->{$interval_field} > 0) {
40 16
                if($limit > 0 && $k >= $limit) {
41
                    break;
42
                }
43 16
                $parts[] = static::spellUnit($interval->{$interval_field}, $unit);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $parts[] is correct as static::spellUnit($inter...interval_field}, $unit) (which targets morphos\TimeSpeller::spellUnit()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
44 16
                $k++;
45
            }
46
        }
47
48 16
        if (empty($parts)) {
49
            return static::JUST_NOW;
50
        }
51
52 16
        if ($options & self::SEPARATE && count($parts) > 1) {
53 6
            $last_part = array_pop($parts);
54 6
            $spelled = implode(', ', $parts).' '.static::AND_WORD.' '.$last_part;
55
        } else {
56 10
            $spelled = implode(' ', $parts);
57
        }
58
59 16
        if ($options & self::DIRECTION) {
60 4
            if ($interval->invert) {
61
                $spelled = static::IN.' '.$spelled;
62
            } else {
63 4
                $spelled .= ' '.static::AGO;
64
            }
65
        }
66
67 16
        return $spelled;
68
    }
69
}
70