Passed
Push — master ( 796371...61ca61 )
by Andrey
03:07
created

DateTimes::getDateTime()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 5
nc 4
nop 0
dl 0
loc 9
rs 9.6111
c 1
b 0
f 0
1
<?php
2
3
namespace Daaner\NovaPoshta\Traits;
4
5
use Carbon\Carbon;
6
use Exception;
7
use Illuminate\Support\Facades\Date;
8
9
trait DateTimes
10
{
11
    protected $dateTime;
12
    protected $dateTimeFrom;
13
    protected $dateTimeTo;
14
    protected $format = 'd.m.Y';
15
    protected $formatTime = 'd.m.Y H:i:s';
16
17
    /**
18
     * @param  string|Carbon|date  $dateTime
19
     * @return void
20
     */
21
    public function setDateTime($dateTime): void
22
    {
23
        $this->dateTime = $this->checkDate($dateTime);
0 ignored issues
show
Bug introduced by
It seems like $dateTime can also be of type Illuminate\Support\Facades\Date; however, parameter $date of Daaner\NovaPoshta\Traits\DateTimes::checkDate() does only seem to accept Carbon\Carbon|string, maybe add an additional type check? ( Ignorable by Annotation )

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

23
        $this->dateTime = $this->checkDate(/** @scrutinizer ignore-type */ $dateTime);
Loading history...
24
    }
25
26
    /**
27
     * @param  string|Carbon|date  $dateTimeFrom
28
     * @return void
29
     */
30
    public function setDateTimeFrom($dateTimeFrom): void
31
    {
32
        $this->dateTimeFrom = $this->checkDate($dateTimeFrom);
0 ignored issues
show
Bug introduced by
It seems like $dateTimeFrom can also be of type Illuminate\Support\Facades\Date; however, parameter $date of Daaner\NovaPoshta\Traits\DateTimes::checkDate() does only seem to accept Carbon\Carbon|string, maybe add an additional type check? ( Ignorable by Annotation )

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

32
        $this->dateTimeFrom = $this->checkDate(/** @scrutinizer ignore-type */ $dateTimeFrom);
Loading history...
33
    }
34
35
    /**
36
     * @param  string|Carbon|date  $dateTimeTo
37
     * @return void
38
     */
39
    public function setDateTimeTo($dateTimeTo): void
40
    {
41
        $this->dateTimeTo = $this->checkDate($dateTimeTo);
0 ignored issues
show
Bug introduced by
It seems like $dateTimeTo can also be of type Illuminate\Support\Facades\Date; however, parameter $date of Daaner\NovaPoshta\Traits\DateTimes::checkDate() does only seem to accept Carbon\Carbon|string, maybe add an additional type check? ( Ignorable by Annotation )

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

41
        $this->dateTimeTo = $this->checkDate(/** @scrutinizer ignore-type */ $dateTimeTo);
Loading history...
42
    }
43
44
    /**
45
     * @return void
46
     */
47
    public function getDateTime(): void
48
    {
49
        if ($this->dateTime && (! $this->dateTimeFrom || ! $this->dateTimeTo)) {
50
            $this->methodProperties['DateTime'] = $this->dateTime;
51
        }
52
53
        if (! $this->dateTime) {
54
            $this->dateTime = Carbon::now()->format($this->format);
55
            $this->methodProperties['DateTime'] = $this->dateTime;
56
        }
57
    }
58
59
    /**
60
     * @return void
61
     */
62
    public function getDateTimeFromTo(): void
63
    {
64
        if ($this->dateTimeFrom || $this->dateTimeTo) {
65
            if (! $this->dateTimeTo) {
66
                $this->dateTimeTo = Carbon::now()->format($this->format);
67
            }
68
            if (! $this->dateTimeFrom) {
69
                $this->dateTimeFrom = $this->dateTimeTo;
70
            }
71
72
            $this->methodProperties['DateTimeFrom'] = $this->dateTimeFrom;
73
            $this->methodProperties['DateTimeTo'] = $this->dateTimeTo;
74
        }
75
    }
76
77
    /**
78
     * Странно, но тут с минутами и секундами.
79
     *
80
     * @param  string|Carbon|date|null  $from
81
     * @param  string|Carbon|date|null  $to
82
     * @return void
83
     */
84
    public function getDateFromTo($from = null, $to = null): void
85
    {
86
        // DateFrom
87
        if ($from) {
88
            $from = $this->checkDate($from, $this->formatTime);
0 ignored issues
show
Bug introduced by
It seems like $from can also be of type Illuminate\Support\Facades\Date; however, parameter $date of Daaner\NovaPoshta\Traits\DateTimes::checkDate() does only seem to accept Carbon\Carbon|string, maybe add an additional type check? ( Ignorable by Annotation )

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

88
            $from = $this->checkDate(/** @scrutinizer ignore-type */ $from, $this->formatTime);
Loading history...
89
        } else {
90
            $from = Carbon::now()->addMonth(-3)->format($this->formatTime);
0 ignored issues
show
Unused Code introduced by
The call to Carbon\Carbon::addMonth() has too many arguments starting with -3. ( Ignorable by Annotation )

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

90
            $from = Carbon::now()->/** @scrutinizer ignore-call */ addMonth(-3)->format($this->formatTime);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
91
        }
92
93
        // DateTo
94
        if ($to) {
95
            $to = $this->checkDate($to, $this->formatTime);
96
        } else {
97
            $to = Carbon::now()->format($this->formatTime);
98
        }
99
100
        $this->methodProperties['DateFrom'] = $from;
101
        $this->methodProperties['DateTo'] = $to;
102
    }
103
104
    /**
105
     * @param  string|Carbon  $date
106
     * @return string $date
107
     */
108
    public function checkDate($date, ?string $format = null): string
109
    {
110
        if(! $format) {
111
            $format = $this->format;
112
        }
113
114
        if ($date instanceof Carbon) {
115
            $date = $date->format($format);
116
        } else {
117
            try {
118
                $date = Carbon::parse($date)->format($format);
119
            } catch (Exception $e) {
120
                $date = Carbon::now()->format($format);
121
            }
122
        }
123
124
        return $date;
125
    }
126
}
127