time_helper::time_elapsed_string()   C
last analyzed

Complexity

Conditions 14
Paths 128

Size

Total Lines 55
Code Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 14
eloc 42
nc 128
nop 2
dl 0
loc 55
rs 6.0333
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
/**
4
* @Auther: bhief
5
* @Version: 1.0
6
*
7
* Time manipulation class. Used for converting seconds -> time and getting x seconds ago, eg.
8
*
9
**/
10
class time_helper {
11
    function time_elapsed_string($datetime, $full = false) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
12
        global $cLang;
13
        $now = new DateTime;
14
        $ago = new DateTime($datetime);
15
        $diff = $now->diff($ago);
16
    
17
        $diff->w = floor($diff->d / 7);
0 ignored issues
show
Bug introduced by
The property w does not seem to exist on DateInterval.
Loading history...
18
        $diff->d -= $diff->w * 7;
19
    
20
        if(!isset($cLang)) {
21
            $string = array(
22
                'y' => 'year',
23
                'm' => 'month',
24
                'w' => 'week',
25
                'd' => 'day',
26
                'h' => 'hour',
27
                'i' => 'minute',
28
                's' => 'second',
29
            );
30
        } else {
31
            $string = array(
32
                'y' => $cLang['year'],
33
                'm' => $cLang['month'],
34
                'w' => $cLang['week'],
35
                'd' => $cLang['day'],
36
                'h' => $cLang['hour'],
37
                'i' => $cLang['minute'],
38
                's' => $cLang['second'],
39
            );
40
        }
41
    
42
        if(!isset($cLang)) {
43
            foreach ($string as $k => &$v) {
44
                if ($diff->$k) {
45
                    $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
46
                } else {
47
                    unset($string[$k]);
48
                }
49
            }
50
        } else {
51
            foreach ($string as $k => &$v) {
52
                if ($diff->$k) {
53
                    $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? $cLang['pluralTimeRight'] : '');
54
                } else {
55
                    unset($string[$k]);
56
                }
57
            }
58
        }
59
    
60
        if(!isset($cLang)) {
61
            if (!$full) $string = array_slice($string, 0, 1);
62
            return $string ? implode(', ', $string) . ' ago' : 'just now';
63
        } else {
64
            if (!$full) $string = array_slice($string, 0, 1);
65
            return $string ? implode($cLang['agoLeft'], $string) . " " . " " . $cLang['agoRight'] : $cLang['justNow'];
66
        }
67
    }
68
69
    
70
    function timestamp(int $seconds) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
71
        if ($seconds > 60*60*24) {
72
            // over a day
73
            return sprintf("%d:%s:%s:%s",
74
                floor($seconds/60/60/24),                                 // Days
75
                str_pad( floor($seconds/60/60%24), 2, "0", STR_PAD_LEFT), // Hours
76
                str_pad( floor($seconds/60%60),    2, "0", STR_PAD_LEFT), // Minutes
77
                str_pad(       $seconds%60,        2, "0", STR_PAD_LEFT)  // Seconds
78
            );
79
        } else if ($seconds > 60*60) {
80
            // over an hour
81
            return sprintf("%d:%s:%s",
82
                        floor($seconds/60/60),                        // Hours
83
                str_pad(floor($seconds/60%60), 2, "0", STR_PAD_LEFT), // Minutes
84
                str_pad(      $seconds%60,     2, "0", STR_PAD_LEFT)  // Seconds
85
            );
86
        } else {
87
            // less than an hour
88
            return sprintf("%d:%s",
89
                        floor($seconds/60),                       // Minutes
90
                str_pad(      $seconds%60,  2, "0", STR_PAD_LEFT) // Seconds
91
            );
92
        }
93
    }
94
}
95
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...