DateTime::diffAsText()   D
last analyzed

Complexity

Conditions 10
Paths 512

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
dl 0
loc 20
rs 4.1777
c 0
b 0
f 0
nc 512
nop 3

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
/**
3
 * @author  Vasily Zorin <[email protected]>
4
 */
5
6
namespace PHPDaemon\Utils;
7
8
class DateTime extends \DateTime
9
{
10
    /**
11
     * Support timestamp and available date format
12
     * @param string $time
13
     * @param \DateTimeZone $timezone
0 ignored issues
show
Documentation introduced by
Should the type for parameter $timezone not be null|\DateTimeZone?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
14
     * @link http://php.net/manual/en/datetime.construct.php
15
     */
16
    public function __construct($time = 'now', \DateTimeZone $timezone = null)
17
    {
18
        if (is_int($time)) {
19
            parent::__construct('now', $timezone);
20
            $this->setTimestamp($time);
21
        } else {
22
            parent::__construct($time, $timezone);
23
        }
24
    }
25
26
    /**
27
     * Calculates a difference between two dates
28
     * @see http://www.php.net/manual/en/datetime.diff.php
29
     * @param  integer|string|\DateTimeInterface $datetime1
30
     * @param  integer|string|\DateTimeInterface $datetime2
31
     * @param  boolean $absolute
32
     * @return string Something like this: 1 year. 2 mon. 6 day. 4 hours. 21 min. 10 sec.
33
     */
34
    public static function diffAsText($datetime1, $datetime2, $absolute = false)
35
    {
36
        if (!($datetime1 instanceof \DateTimeInterface)) {
37
            $datetime1 = new static($datetime1);
38
        }
39
        if (!($datetime2 instanceof \DateTimeInterface)) {
40
            $datetime2 = new static($datetime2);
41
        }
42
43
        $interval = $datetime1->diff($datetime2, $absolute);
44
        $str = '';
45
        $str .= $interval->y ? $interval->y . ' year. ' : '';
46
        $str .= $interval->m ? $interval->m . ' mon. ' : '';
47
        $str .= $interval->d ? $interval->d . ' day. ' : '';
48
        $str .= $interval->h ? $interval->h . ' hour. ' : '';
49
        $str .= $interval->i ? $interval->i . ' min. ' : '';
50
        $str .= $interval->s || $str === '' ? $interval->s . ' sec. ' : '';
51
52
        return rtrim($str);
53
    }
54
}
55