Completed
Push — master ( d503c3...0a0843 )
by Chad
13s queued 10s
created

DateTimeUtil   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 120
Duplicated Lines 100 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 0
Metric Value
wmc 13
lcom 0
cbo 0
dl 120
loc 120
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A isWeekendDay() 5 5 1
A isWeekDay() 5 5 1
A isSameDay() 5 5 3
A isDaylightSavings() 4 4 1
A isInRange() 14 14 3
A asAgoString() 29 29 4

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace SubjectivePHP\DateTime\Utilities;
4
5
use DateTimeInterface;
6
7
/**
8
 * Utility class for DateTimeInterface objects.
9
 */
10 View Code Duplication
abstract class DateTimeUtil
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
11
{
12
    /**
13
     * Returns true if the given date time is a Saturday or Sunday.
14
     *
15
     * @param DateTimeInterface $dateTime The date/time object.
16
     *
17
     * @return bool
18
     */
19
    final public static function isWeekendDay(DateTimeInterface $dateTime) : bool
20
    {
21
        //ISO-8601 numeric representation of the day of the week, 1 (for Monday) through 7 (for Sunday)
22
        return $dateTime->format('N') > 5;
23
    }
24
25
    /**
26
     * Returns true if the given date time is not a Saturday or Sunday.
27
     *
28
     * @param DateTimeInterface $dateTime The date/time object.
29
     *
30
     * @return bool
31
     */
32
    final public static function isWeekDay(DateTimeInterface $dateTime) : bool
33
    {
34
        //ISO-8601 numeric representation of the day of the week, 1 (for Monday) through 7 (for Sunday)
35
        return $dateTime->format('N') < 6;
36
    }
37
38
    /**
39
     * Returns true if the given dates occur on the same year, month and day.
40
     *
41
     * @param DateTimeInterface $thisDate A date to compare.
42
     * @param DateTimeInterface $thatDate A date to compare.
43
     *
44
     * @return bool
45
     */
46
    final public static function isSameDay(DateTimeInterface $thisDate, DateTimeInterface $thatDate) : bool
47
    {
48
        $interval = $thisDate->diff($thatDate);
49
        return !$interval->y && !$interval->m && !$interval->d;
50
    }
51
52
    /**
53
     * Indicates whether the given instance of DateTime is within the daylight saving time range for the current time
54
     * zone.
55
     *
56
     * @param DateTimeInterface $dateTime The date/time object.
57
     *
58
     * @return bool
59
     */
60
    final public static function isDaylightSavings(DateTimeInterface $dateTime) : bool
61
    {
62
        return (bool)$dateTime->format('I');
63
    }
64
65
    /**
66
     * Returns true if the given date is between the provided date range.
67
     *
68
     * @param DateTimeInterface $subjectDate The date/time object being checked.
69
     * @param DateTimeInterface $startDate   The start date/time object.
70
     * @param DateTimeInterface $endDate     The end date/time object.
71
     *
72
     * @return bool
73
     *
74
     * @throws \DomainException Thrown when an invalid date range is provided.
75
     */
76
    final public static function isInRange(
77
        DateTimeInterface $subjectDate,
78
        DateTimeInterface $startDate,
79
        DateTimeInterface $endDate
80
    ) : bool {
81
        $subjectTimestamp = $subjectDate->getTimestamp();
82
        $startDateTimestamp = $startDate->getTimestamp();
83
        $endDateTimestamp = $endDate->getTimestamp();
84
        if ($startDateTimestamp > $endDateTimestamp) {
85
            throw new \DomainException('Invalid date range provided.');
86
        }
87
88
        return ($subjectTimestamp >= $startDateTimestamp && $subjectTimestamp <= $endDateTimestamp);
89
    }
90
91
    /**
92
     * Returns the given DateTime instance as a "time ago" string.
93
     *
94
     * @param DateTimeInterface $dateTime The DateTime object to present as an ago string.
95
     *
96
     * @return string
97
     *
98
     * @throws \DomainException Thrown if the given $dateTime is in the future.
99
     */
100
    final public static function asAgoString(DateTimeInterface $dateTime) : string
101
    {
102
        $numHours = round((time() - $dateTime->getTimestamp())/3600, 2);
103
        if ($numHours < 0) {
104
            throw new \DomainException('Cannot create "time ago" string for a date in the future.');
105
        }
106
107
        $formulas = [
108
            '.025' => ['just now', 1],
109
            '.5' => ['%d minutes ago', 60],
110
            '1.5' => ['about an hour ago', 1],
111
            '24' => ['about %d hours ago', 1],
112
            '48' => ['yesterday', 1],
113
            '168' => ['about %d days ago', 1/24],
114
            '252' => ['last week', 1],
115
            '672' => ['about %d weeks ago', 1/168],
116
            '1080' => ['last month', 1],
117
            '8760' => ['about %d months ago', 1/672],
118
            '13140' => ['last year', 1],
119
        ];
120
121
        foreach ($formulas as $maxHours => list($format, $multiplier)) {
122
            if ($numHours < $maxHours) {
123
                return sprintf($format, round($numHours * $multiplier));
124
            }
125
        }
126
127
        return sprintf('about %s years ago', round($numHours/8064));
128
    }
129
}
130