Test Failed
Push — master ( c78782...4767ee )
by Davis
05:32
created

Dates::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
namespace DavisPeixoto\ReportDates;
3
4
use DateInterval;
5
use DateTime;
6
use Exception;
7
8
class Dates
9
{
10
    /**
11
     * @var DatesConfig $config
12
     */
13
    private $config;
14
15
    /**
16
     * Dates constructor.
17
     *
18
     * @param DatesConfig $config
19
     */
20
    public function __construct(DatesConfig $config)
21
    {
22
        $this->config = $config;
23
    }
24
25
    /**
26
     * @param DateTime $startDate
27
     * @param DateTime $endDate
28
     * @param bool $full
29
     * @param bool $inclusive
30
     *
31
     * @return WeekInterval[]
32
     * @throws Exception
33
     */
34
    public function get_weeks_break(DateTime $startDate, DateTime $endDate, $full = false, $inclusive = false)
0 ignored issues
show
Coding Style introduced by
function get_weeks_break() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
35
    {
36
        $output = [];
37
        $innerArray = [];
38
        $isOpen = false;
39
        $i = 0;
40
41
        $interval = new DateInterval('P1D');
42
        $daylightSavingsTime = new DateInterval('PT1H');
43
44
        if ($inclusive) {
45
            $oldStart = clone $startDate;
46
            $oldEnd = clone $endDate;
47
48
            $aux = new DateTime();
49
50
            if ($oldStart->format('w') !== '0') {
51
                $aux->setISODate($oldStart->format('o'), $oldStart->format('W'), 0);
52
                $startDate = clone $aux;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $startDate. This often makes code more readable.
Loading history...
53
            }
54
55
            if ($oldEnd->format('w') !== '6') {
56
                $aux->setISODate($oldEnd->format('o'), $oldEnd->format('W'), 6);
57
                $endDate = clone $aux;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $endDate. This often makes code more readable.
Loading history...
58
            }
59
        }
60
61
        $start = clone $startDate;
62
        $end = clone $endDate;
63
64
        while ($start <= $end) {
65
            $str = (int)$start->format('w');
66
67
            if (!$full) {
68
                if (in_array($str, range(1, 5)) && !$isOpen) {
69
                    $innerArray[$i]['start'] = clone $start;
70
                    $isOpen = true;
71
                }
72
73
                if (!in_array($str, range(1, 5)) && $isOpen) {
74
                    $x = clone $start;
75
                    $x->sub($interval);
76
                    $innerArray[$i]['end'] = clone $x;
77
                    $isOpen = false;
78
                    $i++;
79
                }
80
81 View Code Duplication
                if ($start == $end && $isOpen) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
82
                    $innerArray[$i]['end'] = clone $start;
83
                    $isOpen = false;
84
                }
85
            } else {
86 View Code Duplication
                if (($str !== 6) && !$isOpen) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
87
                    $innerArray[$i]['start'] = clone $start;
88
                    $isOpen = true;
89
                }
90
91
                if (($str === 6) && $isOpen) {
92
                    $x = clone $start;
93
                    $innerArray[$i]['end'] = clone $x;
94
                    $isOpen = false;
95
                    $i++;
96
                }
97
            }
98
99 View Code Duplication
            if (($start == $end) && $isOpen) {
1 ignored issue
show
Duplication introduced by
This code seems to be duplicated across 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...
100
                $innerArray[$i]['end'] = clone $start;
101
                $isOpen = false;
102
            }
103
104
            $start->add($interval);
105
106
            // fix for day light savings time
107
            if ($start->format('h') === "01") {
108
                $start->sub($daylightSavingsTime);
109
            }
110
111
            if ($start->format('h') === "23") {
112
                $start->add($daylightSavingsTime);
113
            }
114
        }
115
116
        foreach ($innerArray as $key => $value) {
117
            $weekInterval = new WeekInterval($value['start'], $value['end']);
118
            $output[$key] = $weekInterval;
119
        }
120
121
        return $output;
122
    }
123
124
    public function unwrap_week($yearweek, $yearmonth)
0 ignored issues
show
Coding Style introduced by
function unwrap_week() does not seem to conform to the naming convention (^(?:[a-z]|__)[a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
125
    {
126
        $output = array();
127
128
        $x = str_split($yearmonth, 4);
129
        $x[] = "01";
130
        $obj_start = DateTime::createFromFormat('Y-m-d', implode('-', $x));
0 ignored issues
show
Coding Style introduced by
$obj_start does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
131
132
        $start_date = date('Y-m-01', $obj_start->getTimeStamp());
0 ignored issues
show
Coding Style introduced by
$start_date does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
133
        $end_date = date('Y-m-t', $obj_start->getTimeStamp());
0 ignored issues
show
Coding Style introduced by
$end_date does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
134
135
        $weeks = $this->get_weeks_break($start_date, $end_date);
0 ignored issues
show
Coding Style introduced by
$start_date does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Documentation introduced by
$start_date is of type string, but the function expects a object<DateTime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$end_date is of type string, but the function expects a object<DateTime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
136
        $weeks_full = $this->get_weeks_break($start_date, $end_date, true);
0 ignored issues
show
Coding Style introduced by
$weeks_full does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Documentation introduced by
$start_date is of type string, but the function expects a object<DateTime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
Documentation introduced by
$end_date is of type string, but the function expects a object<DateTime>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
137
        $today = new DateTime();
138
139
        foreach ($weeks as $key => $value) {
140
            if ($value['yearweek'] == $yearweek) {
141
                $output['start'] = $value['start_date'];
142
                $output['end'] = $value['end_date'];
143
144
                $endDateObj = DateTime::createFromFormat('Y-m-d', $output['end']);
145
146
                if ($today > $endDateObj) {
147
                    foreach ($weeks_full as $innerWeek) {
0 ignored issues
show
Coding Style introduced by
$weeks_full does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
148
                        if ($value['yearweek'] == $innerWeek['yearweek']) {
149
                            $output['start'] = $innerWeek['start_date'];
150
                            $output['end'] = $innerWeek['end_date'];
151
                            break;
152
                        }
153
                    }
154
                }
155
                break;
156
            }
157
        }
158
159
        return $output;
160
    }
161
}
162