Completed
Branch newinternal (cdd491)
by Simon
04:39
created

modifier.relativedate.php ➔ smarty_modifier_relativedate()   C

Complexity

Conditions 21
Paths 54

Size

Total Lines 67
Code Lines 45

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 21
eloc 45
c 2
b 0
f 0
nc 54
nop 1
dl 0
loc 67
rs 5.7177

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
 * Transforms a date string into a relative representation of the date ("2 weeks ago").
5
 *
6
 * @param string $input A string representing a date
7
 *
8
 * @return string
9
 * @example {$variable|relativedate} from Smarty
10
 */
11
function smarty_modifier_relativedate($input)
1 ignored issue
show
Coding Style introduced by
As per coding-style, this function should be in camelCase.

CamelCase (...) is the practice of writing compound words or phrases such that
each word or abbreviation begins with a capital letter.

Learn more about camelCase.

Loading history...
12
{
13
	$now = new DateTime();
14
15
	if (gettype($input) === 'object'
16
		&& (get_class($input) === DateTime::class || get_class($input) === DateTimeImmutable::class)
17
	) {
18
		$then = $input;
19
	}
20
	else {
21
		$then = new DateTime($input);
22
	}
23
24
	$secs = $now->getTimestamp() - $then->getTimestamp();
25
26
	$second = 1;
27
	$minute = 60 * $second;
28
	$minuteCut = 60 * $second;
29
	$hour = 60 * $minute;
30
	$hourCut = 90 * $minute;
31
	$day = 24 * $hour;
32
	$dayCut = 48 * $hour;
33
	$week = 7 * $day;
34
	$weekCut = 14 * $day;
35
	$month = 30 * $day;
36
	$monthCut = 60 * $day;
37
	$year = 365 * $day;
38
	$yearCut = $year * 2;
39
40
	$pluralise = true;
41
42
	if ($secs <= 10) {
43
		$output = "just now";
44
		$pluralise = false;
45
	}
46
	elseif ($secs > 10 && $secs < $minuteCut) {
47
		$output = round($secs / $second) . " second";
48
	}
49
	elseif ($secs >= $minuteCut && $secs < $hourCut) {
50
		$output = round($secs / $minute) . " minute";
51
	}
52
	elseif ($secs >= $hourCut && $secs < $dayCut) {
53
		$output = round($secs / $hour) . " hour";
54
	}
55
	elseif ($secs >= $dayCut && $secs < $weekCut) {
56
		$output = round($secs / $day) . " day";
57
	}
58
	elseif ($secs >= $weekCut && $secs < $monthCut) {
59
		$output = round($secs / $week) . " week";
60
	}
61
	elseif ($secs >= $monthCut && $secs < $yearCut) {
62
		$output = round($secs / $month) . " month";
63
	}
64
	elseif ($secs >= $yearCut && $secs < $year * 10) {
65
		$output = round($secs / $year) . " year";
66
	}
67
	else {
68
		$output = "a long time ago";
69
		$pluralise = false;
70
	}
71
72
	if ($pluralise) {
73
		$output = (substr($output, 0, 2) <> "1 ") ? $output . "s ago" : $output . " ago";
74
	}
75
76
	return $output;
77
}