smarty_modifier_relativedate()   D
last analyzed

Complexity

Conditions 22
Paths 55

Size

Total Lines 71
Code Lines 47

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 47
dl 0
loc 71
rs 4.1666
c 1
b 0
f 0
cc 22
nc 55
nop 1

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
        try {
28
            $then = new DateTime($input);
29
        }
30
        catch (Exception $ex) {
31
            return $input;
32
        }
33
    }
34
35
    $secs = $now->getTimestamp() - $then->getTimestamp();
36
37
    $second = 1;
38
    $minute = 60 * $second;
39
    $minuteCut = 60 * $second;
40
    $hour = 60 * $minute;
41
    $hourCut = 90 * $minute;
42
    $day = 24 * $hour;
43
    $dayCut = 48 * $hour;
44
    $week = 7 * $day;
45
    $weekCut = 14 * $day;
46
    $month = 30 * $day;
47
    $monthCut = 60 * $day;
48
    $year = 365 * $day;
49
    $yearCut = $year * 2;
50
51
    $pluralise = true;
52
53
    if ($secs <= 10) {
54
        $output = "just now";
55
        $pluralise = false;
56
    }
57
    elseif ($secs > 10 && $secs < $minuteCut) {
58
        $output = round($secs / $second) . " second";
59
    }
60
    elseif ($secs >= $minuteCut && $secs < $hourCut) {
61
        $output = round($secs / $minute) . " minute";
62
    }
63
    elseif ($secs >= $hourCut && $secs < $dayCut) {
64
        $output = round($secs / $hour) . " hour";
65
    }
66
    elseif ($secs >= $dayCut && $secs < $weekCut) {
67
        $output = round($secs / $day) . " day";
68
    }
69
    elseif ($secs >= $weekCut && $secs < $monthCut) {
70
        $output = round($secs / $week) . " week";
71
    }
72
    elseif ($secs >= $monthCut && $secs < $yearCut) {
73
        $output = round($secs / $month) . " month";
74
    }
75
    elseif ($secs >= $yearCut && $secs < $year * 10) {
76
        $output = round($secs / $year) . " year";
77
    }
78
    else {
79
        $output = "a long time ago";
80
        $pluralise = false;
81
    }
82
83
    if ($pluralise) {
84
        $output = (substr($output, 0, 2) <> "1 ") ? $output . "s ago" : $output . " ago";
85
    }
86
87
    return $output;
88
}
89