Passed
Push — statistics ( 40505d )
by Simon
11:52
created

smarty_modifier_timespan()   F

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