DateTimeZoneTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 173
Duplicated Lines 6.36 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 11
loc 173
c 0
b 0
f 0
wmc 12
lcom 1
cbo 2
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A fromString() 0 6 1
A fromStringDefaultTimeZone() 0 5 1
A fromStringWithInvalidAbbreviation() 0 4 1
A fromStringOutliers() 11 11 2
A getOutliers() 0 12 1
A fromOffset() 0 5 1
A fromOffsetInvalidOffsetValue() 0 4 1
A fromOffsetInvalidDSTValue() 0 4 1
A getLongName() 0 5 1
A getLongNameWithLongName() 0 5 1
A getLongNameOutlier() 0 5 1

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
namespace SubjectivePHPTest\Util;
3
4
use SubjectivePHP\Util\DateTimeZone;
5
use PHPUnit\Framework\TestCase;
6
7
/**
8
 * @coversDefaultClass \SubjectivePHP\Util\DateTimeZone
9
 * @covers ::<private>
10
 */
11
final class DateTimeZoneTest extends TestCase
12
{
13
    /**
14
     * Verify basic behavior of fromString()
15
     *
16
     * @test
17
     * @covers ::fromString
18
     *
19
     * @return void
20
     */
21
    public function fromString()
22
    {
23
        $timezone = DateTimeZone::fromString('Pacific/Honolulu');
24
        $this->assertSame('Pacific/Honolulu', $timezone->getName());
25
        $this->assertSame(-36000, $timezone->getOffset(new \DateTime('now', $timezone)));
26
    }
27
28
    /**
29
     * Verify behavior of fromString() with default timezone.
30
     *
31
     * @test
32
     * @covers ::fromString
33
     *
34
     * @return void
35
     */
36
    public function fromStringDefaultTimeZone()
37
    {
38
        $timezone = new \DateTimeZone('Pacific/Honolulu');
39
        $this->assertSame($timezone, DateTimeZone::fromString('Invalid', $timezone));
40
    }
41
42
    /**
43
     * Verify fromString() defaults to UTC on error.
44
     *
45
     * @test
46
     * @covers ::fromString
47
     *
48
     * @return void
49
     */
50
    public function fromStringWithInvalidAbbreviation()
51
    {
52
        $this->assertNull(DateTimeZone::fromString('NOT VALID'));
53
    }
54
55
    /**
56
     * Verify fromString() correctly converts edge case timezones.
57
     *
58
     * @param string $abbreviation The abbreviation to tests.
59
     * @param string $expected     The expected result from the fromString() call.
60
     *
61
     * @test
62
     * @covers ::fromString
63
     * @dataProvider getOutliers
64
     *
65
     * @return void
66
     */
67 View Code Duplication
    public function fromStringOutliers($abbreviation, $expected)
0 ignored issues
show
Duplication introduced by
This method 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...
68
    {
69
        if (timezone_name_from_abbr($abbreviation) !== false) {
70
            $this->markTestSkipped(
71
                "The timezone abbreviation '{$abbreviation}' is not considered an outlier on this system"
72
            );
73
            return;
74
        }
75
76
        $this->assertSame($expected, DateTimeZone::fromString($abbreviation)->getName());
77
    }
78
79
    /**
80
     * Dataprovider for outlier testing
81
     *
82
     * @return array
83
     */
84
    public function getOutliers()
85
    {
86
        return [
87
            ['WIB', 'Asia/Jakarta'],
88
            ['FET', 'Europe/Helsinki'],
89
            ['AEST', 'Australia/Tasmania'],
90
            ['AWST', 'Australia/West'],
91
            ['WITA', 'Asia/Makassar'],
92
            ['AEDT', 'Australia/Sydney'],
93
            ['ACDT', 'Australia/Adelaide'],
94
        ];
95
    }
96
97
    /**
98
     * Verify basic behavior of fromOffset()
99
     *
100
     * @test
101
     * @covers ::fromOffset
102
     * @uses \SubjectivePHP\Util\DateTimeZone::fromString
103
     *
104
     * @return void
105
     */
106
    public function fromOffset()
107
    {
108
        $timezone = DateTimeZone::fromOffset(-36000, false);
109
        $this->assertSame('Pacific/Honolulu', $timezone->getName());
110
    }
111
112
    /**
113
     * Verify behavior of fromOffset() when $gmtOffset is not an integer.
114
     *
115
     * @test
116
     * @covers ::fromOffset
117
     * @expectedException \InvalidArgumentException
118
     * @expectedExceptionMessage $gmtOffset must be an integer
119
     *
120
     * @return void
121
     */
122
    public function fromOffsetInvalidOffsetValue()
123
    {
124
        DateTimeZone::fromOffset(false, false);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a integer.

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...
125
    }
126
127
    /**
128
     * Verify behavior of fromOffset() when $isDaylightSavings is not a boolean.
129
     *
130
     * @test
131
     * @covers ::fromOffset
132
     * @expectedException \InvalidArgumentException
133
     * @expectedExceptionMessage $isDaylightSavings must be a boolean
134
     *
135
     * @return void
136
     */
137
    public function fromOffsetInvalidDSTValue()
138
    {
139
        DateTimeZone::fromOffset(-36000, 0);
0 ignored issues
show
Documentation introduced by
0 is of type integer, but the function expects a boolean.

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...
140
    }
141
142
    /**
143
     * Verify basic behavior of getLongName().
144
     *
145
     * @test
146
     * @covers ::getLongName
147
     *
148
     * @return void
149
     */
150
    public function getLongName()
151
    {
152
        $timezone = new \DateTimeZone('HST');
153
        $this->assertSame('Pacific/Honolulu', DateTimeZone::getLongName($timezone));
154
    }
155
156
    /**
157
     * Verify basic behavior of getLongName().
158
     *
159
     * @test
160
     * @covers ::getLongName
161
     *
162
     * @return void
163
     */
164
    public function getLongNameWithLongName()
165
    {
166
        $timezone = new \DateTimeZone('Pacific/Honolulu');
167
        $this->assertSame('Pacific/Honolulu', DateTimeZone::getLongName($timezone));
168
    }
169
170
    /**
171
     * Verify behavior of getLongName() with outlier.
172
     *
173
     * @test
174
     * @covers ::getLongName
175
     *
176
     * @return void
177
     */
178
    public function getLongNameOutlier()
179
    {
180
        $timezone = new \DateTimeZone('UTC');
181
        $this->assertSame('UTC', DateTimeZone::getLongName($timezone));
182
    }
183
}
184