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