DateTimeImmutable::createFromFormat()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 9
Ratio 100 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 9
loc 9
rs 9.6666
cc 2
eloc 6
nc 2
nop 3
1
<?php
2
/**
3
 * spindle/types
4
 *
5
 * @license CC0-1.0 (Public Domain) https://creativecommons.org/publicdomain/zero/1.0/
6
 */
7
namespace Spindle\Types\Polyfill;
8
9
//@codeCoverageIgnoreStart
10
if (class_exists('DateTimeImmutable', false)) {
11 View Code Duplication
    class DateTimeImmutable extends \DateTimeImmutable implements DateTimeInterface
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...
12
    {
13
        static function createFromFormat($format, $time, /* \DateTimeZone */ $timezone=null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
14
        {
15
            if ($timezone) {
16
                $dateTime = \date_create_from_format($format, $time, $timezone);
17
            } else {
18
                $dateTime = \date_create_from_format($format, $time);
19
            }
20
            return new static($dateTime->format('c'));
21
        }
22
23
        static function __set_state(array $array)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
24
        {
25
            $dateTime = parent::__set_state($array);
26
            return new static($dateTime->format('c'));
27
        }
28
    }
29
} else {
30
    class DateTimeImmutable extends \DateTime implements DateTimeInterface
0 ignored issues
show
Comprehensibility Best Practice introduced by
The type Spindle\Types\Polyfill\DateTimeImmutable has been defined more than once; this definition is ignored, only the first definition in this file (L11-28) is considered.

This check looks for classes that have been defined more than once in the same file.

If you can, we would recommend to use standard object-oriented programming techniques. For example, to avoid multiple types, it might make sense to create a common interface, and then multiple, different implementations for that interface.

This also has the side-effect of providing you with better IDE auto-completion, static analysis and also better OPCode caching from PHP.

Loading history...
31
    {
32
        function add(/* \DateInterval */ $interval)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
33
        {
34
            $new = clone $this;
35
            return \date_add($new, $interval);
36
        }
37
38
        static function createFromFormat($format, $time, /* \DateTimeZone */ $timezone=null)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39
        {
40
            if ($timezone) {
41
                $dateTime = \date_create_from_format($format, $time, $timezone);
42
            } else {
43
                $dateTime = \date_create_from_format($format, $time);
44
            }
45
            return new static($dateTime->format('c'));
46
        }
47
48
        function modify($modify)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
49
        {
50
            $new = clone $this;
51
            return \date_modify($new, $modify);
52
        }
53
54
        function setDate($year, $month, $day)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
55
        {
56
            $new = clone $this;
57
            return \date_date_set($new, $year, $month, $day);
58
        }
59
60
        function setISODate($year, $month, $day = 1)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
61
        {
62
            $new = clone $this;
63
            return \date_isodate_set($new, $year, $month, $day);
64
        }
65
66
        function setTime($hour, $minute, $second = 0)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
67
        {
68
            $new = clone $this;
69
            return \date_time_set($new, $hour, $second);
70
        }
71
72
        function setTimestamp($unixtimestamp)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
73
        {
74
            $new = clone $this;
75
            return \date_timestamp_set($new, $unixtimestamp);
76
        }
77
78
        function setTimezone(/* \DateTimeZone */ $timezone)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
79
        {
80
            $new = clone $this;
81
            return \date_timezone_set($new, $timezone);
82
        }
83
84
        static function __set_state(array $array)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
85
        {
86
            $dateTime = parent::__set_state($array);
87
            return new static($dateTime->format('c'));
88
        }
89
    }
90
}
91
//@codeCoverageIgnoreEnd
92