Failed Conditions
Push — newinternal ( 216d62...410e59 )
by Simon
05:28 queued 13s
created

modifier.relativedate.php ➔ smarty_modifier_relativedate()   D

Complexity

Conditions 21
Paths 54

Size

Total Lines 67

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 21
nc 54
nop 1
dl 0
loc 67
rs 4.1666
c 0
b 0
f 0

How to fix   Long Method    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
 * Wikipedia Account Creation Assistance tool                                 *
4
 *                                                                            *
5
 * All code in this file is released into the public domain by the ACC        *
6
 * Development Team. Please see team.json for a list of contributors.         *
7
 ******************************************************************************/
8
9
/**
10
 * Transforms a date string into a relative representation of the date ("2 weeks ago").
11
 *
12
 * @param string $input A string representing a date
13
 *
14
 * @return string
15
 * @example {$variable|relativedate} from Smarty
16
 */
17
function smarty_modifier_relativedate($input)
18
{
19
    $now = new DateTime();
20
21
    if (gettype($input) === 'object'
22
        && (get_class($input) === DateTime::class || get_class($input) === DateTimeImmutable::class)
23
    ) {
24
        $then = $input;
25
    }
26
    else {
27
        $then = new DateTime($input);
28
    }
29
30
    $secs = $now->getTimestamp() - $then->getTimestamp();
31
32
    $second = 1;
33
    $minute = 60 * $second;
34
    $minuteCut = 60 * $second;
35
    $hour = 60 * $minute;
36
    $hourCut = 90 * $minute;
37
    $day = 24 * $hour;
38
    $dayCut = 48 * $hour;
39
    $week = 7 * $day;
40
    $weekCut = 14 * $day;
41
    $month = 30 * $day;
42
    $monthCut = 60 * $day;
43
    $year = 365 * $day;
44
    $yearCut = $year * 2;
45
46
    $pluralise = true;
47
48
    if ($secs <= 10) {
49
        $output = "just now";
50
        $pluralise = false;
51
    }
52
    elseif ($secs > 10 && $secs < $minuteCut) {
53
        $output = round($secs / $second) . " second";
54
    }
55
    elseif ($secs >= $minuteCut && $secs < $hourCut) {
56
        $output = round($secs / $minute) . " minute";
57
    }
58
    elseif ($secs >= $hourCut && $secs < $dayCut) {
59
        $output = round($secs / $hour) . " hour";
60
    }
61
    elseif ($secs >= $dayCut && $secs < $weekCut) {
62
        $output = round($secs / $day) . " day";
63
    }
64
    elseif ($secs >= $weekCut && $secs < $monthCut) {
65
        $output = round($secs / $week) . " week";
66
    }
67
    elseif ($secs >= $monthCut && $secs < $yearCut) {
68
        $output = round($secs / $month) . " month";
69
    }
70
    elseif ($secs >= $yearCut && $secs < $year * 10) {
71
        $output = round($secs / $year) . " year";
72
    }
73
    else {
74
        $output = "a long time ago";
75
        $pluralise = false;
76
    }
77
78
    if ($pluralise) {
79
        $output = (substr($output, 0, 2) <> "1 ") ? $output . "s ago" : $output . " ago";
80
    }
81
82
    return $output;
83
}