Passed
Push — master ( 6a26fb...6b2ced )
by Darío
01:50
created

DateTime   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 129
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 129
rs 10
c 0
b 0
f 0
wmc 15

2 Methods

Rating   Name   Duplication   Size   Complexity  
F create() 0 70 13
A getMonth() 0 6 2
1
<?php
2
/**
3
 * DronePHP (http://www.dronephp.com)
4
 *
5
 * @link      http://github.com/Pleets/DronePHP
6
 * @copyright Copyright (c) 2016-2018 Pleets. (http://www.pleets.org)
7
 * @license   http://www.dronephp.com/license
8
 * @author    Darío Rivera <[email protected]>
9
 */
10
11
namespace Drone\Util;
12
13
/**
14
 * DateTime layer
15
 *
16
 * This class creates a \DateTime object
17
 */
18
class DateTime
19
{
20
    /**
21
     * Constants for days
22
     *
23
     * @var integer
24
     */
25
    const TODAY      = 0;
26
    const FIRST_DAY  = -1;
27
    const LAST_DAY   = -2;
28
29
    /**
30
     * Constants for months
31
     *
32
     * @var integer
33
     */
34
    const THIS_MONTH = -10;
35
    const LAST_MONTH = -11;
36
37
    /**
38
     * Constants for years
39
     *
40
     * @var integer
41
     */
42
    const THIS_YEAR  = -20;
43
    const LAST_YEAR  = -21;
44
45
    /**
46
     * Array of valid months
47
     *
48
     * @var string[]
49
     */
50
    const MONTHS = [
51
        1 => 'January', 'February', 'March', 'April', 'May', 'June',
52
        'July', 'August', 'September', 'October', 'November', 'December'
53
    ];
54
55
    /**
56
     * Returns the name of a month
57
     *
58
     * @param string $month_number
59
     *
60
     * @return string
61
     */
62
    public static function getMonth($month_number)
63
    {
64
        if (!array_key_exists($month_number, self::MONTHS))
65
            throw new MonthOutOfRange("Invalid Month");
0 ignored issues
show
Bug introduced by
The type Drone\Util\MonthOutOfRange was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
66
67
        return self::MONTHS[$month_number];
68
    }
69
70
    /**
71
     * Creates a \DateTime instance
72
     *
73
     * @param array $date
74
     *
75
     * @return \DateTime
76
     */
77
    public static function create(array $date = [])
78
    {
79
        if (!array_key_exists('day', $date))
80
            $date["day"] = self::TODAY;
81
82
        if (!array_key_exists('month', $date))
83
            $date["month"] = self::THIS_MONTH;
84
85
        if (!array_key_exists('year', $date))
86
            $date["year"] = self::THIS_YEAR;
87
88
        switch ($date['year'])
89
        {
90
            case self::THIS_YEAR:
91
                $year = (int) date('Y');
92
                break;
93
94
            case self::LAST_YEAR:
95
                $year = ((int) date('Y'))- 1;
96
                break;
97
98
            default:
99
                $year = (int) $date["year"];
100
                break;
101
        }
102
103
        $month = ($date['month'] == self::THIS_MONTH)
104
            ? self::MONTHS[(int) date('m')]
105
            : (
106
                ($date['month'] == self::LAST_MONTH)
107
                ? '-1 month'
108
                : self::getMonth($date['month'])
109
            )
110
        ;
111
112
        switch ($date['day'])
113
        {
114
            case self::TODAY:
115
                $day = (int) date('d');
116
                break;
117
118
            case self::FIRST_DAY:
119
                $day = 'first day of';
120
                break;
121
122
            case self::LAST_DAY:
123
                $day = 'last day of';
124
                break;
125
126
            default:
127
                $day = (int) $date["day"];
128
                break;
129
        }
130
131
        if (is_string($day))
132
            $d = new \DateTime("$day $month $year");
133
        else
134
        {
135
            if (in_array($month, self::MONTHS))
136
                $d = new \DateTime("first day of $month $year");
137
            else
138
            {
139
                $d = new \DateTime("first day of $month");
140
                $d->setDate($year, $d->format('m'), $d->format('d'));
0 ignored issues
show
Bug introduced by
$d->format('m') of type string is incompatible with the type integer expected by parameter $month of DateTime::setDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

140
                $d->setDate($year, /** @scrutinizer ignore-type */ $d->format('m'), $d->format('d'));
Loading history...
Bug introduced by
$d->format('d') of type string is incompatible with the type integer expected by parameter $day of DateTime::setDate(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

140
                $d->setDate($year, $d->format('m'), /** @scrutinizer ignore-type */ $d->format('d'));
Loading history...
141
            }
142
143
            $d->add(new \DateInterval('P'.($day - 1).'D'));
144
        }
145
146
        return $d;
147
    }
148
}