smarty_modifier_timespan()   F
last analyzed

Complexity

Conditions 14
Paths 322

Size

Total Lines 76
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 42
dl 0
loc 76
rs 3.9083
c 1
b 0
f 0
cc 14
nc 322
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 an integer number of seconds in to human-readable timespan
12
 *
13
 * @param int $input Number of seconds
14
 *
15
 * @return string
16
 * @example {$variable|timespan} from Smarty
17
 */
18
function smarty_modifier_timespan($input)
19
{
20
    $remaining = abs(floor($input));
21
22
    $seconds = $remaining % 60;
23
    $remaining = $remaining - $seconds;
24
25
    $minutes = $remaining % (60 * 60);
26
    $remaining = $remaining - $minutes;
27
    $minutes /= 60;
28
29
    $hours = $remaining % (60 * 60 * 24);
30
    $remaining = $remaining - $hours;
31
    $hours /= (60 * 60);
32
33
    $days = $remaining % (60 * 60 * 24 * 7);
34
    $weeks = $remaining - $days;
35
    $days /= (60 * 60 * 24);
36
    $weeks /= (60 * 60 * 24 * 7);
37
38
    $stringval = '';
39
    $trip = false;
40
41
    if ($weeks > 0) {
42
        $stringval .= "${weeks}w ";
43
    }
44
45
    if ($days > 0) {
46
        if ($stringval !== '') {
47
            $trip = true;
48
        }
49
50
        $stringval .= "${days}d ";
51
52
        if ($trip) {
53
            return trim($stringval);
54
        }
55
    }
56
57
    if ($hours > 0) {
58
        if ($stringval !== '') {
59
            $trip = true;
60
        }
61
62
        $stringval .= "${hours}h ";
63
64
        if ($trip) {
65
            return trim($stringval);
66
        }
67
    }
68
69
    if ($minutes > 0) {
70
        if ($stringval !== '') {
71
            $trip = true;
72
        }
73
74
        $stringval .= "${minutes}m ";
75
76
        if ($trip) {
77
            return trim($stringval);
78
        }
79
    }
80
81
    if ($seconds > 0) {
82
        if ($stringval !== '') {
83
            $trip = true;
84
        }
85
86
        $stringval .= "${seconds}s ";
87
88
        if ($trip) {
89
            return trim($stringval);
90
        }
91
    }
92
93
    return trim($stringval);
94
}