Failed Conditions
Pull Request — master (#75)
by
unknown
03:37
created

DateTimeZoneTest   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 120
Duplicated Lines 5.83 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 2
dl 7
loc 120
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A filter() 7 7 1
A filterAllowNullNotBoolean() 0 4 1
A filterNullAllowed() 0 4 1
A filterNullNotAllowed() 0 4 1
A filterTimeZonePass() 0 5 1
A filterInvalidName() 0 4 1
A filterEmptyValue() 0 4 1
A filterNonStringArgument() 0 4 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
3
namespace TraderInteractive\Filter;
4
5
use PHPUnit\Framework\TestCase;
6
7
/**
8
 * Unit tests for the \TraderInteractive\Filter\DateTimeZone class.
9
 *
10
 * @coversDefaultClass \TraderInteractive\Filter\DateTimeZone
11
 */
12
final class DateTimeZoneTest extends TestCase
13
{
14
    /**
15
     * Verify basic usage of filter().
16
     *
17
     * @test
18
     * @covers ::filter
19
     *
20
     * @return void
21
     */
22 View Code Duplication
    public function filter()
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...
23
    {
24
        $value = 'Pacific/Honolulu';
25
        $timezone = DateTimeZone::filter($value);
26
        $this->assertSame($value, $timezone->getName());
27
        $this->assertSame(-36000, $timezone->getOffset(new \DateTime('now', $timezone)));
28
    }
29
30
    /**
31
     * Verify behavior of filter() when $allowNull is not true or false.
32
     *
33
     * @test
34
     * @covers ::filter
35
     * @expectedException \InvalidArgumentException
36
     * @expectedExceptionMessage $allowNull was not a boolean value
37
     *
38
     * @return void
39
     */
40
    public function filterAllowNullNotBoolean()
41
    {
42
        DateTimeZone::filter('America/New_York', 5);
0 ignored issues
show
Documentation introduced by
5 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...
43
    }
44
45
    /**
46
     * Verify behavior of filter() when $allowNull is true and $value is null.
47
     *
48
     * @test
49
     * @covers ::filter
50
     *
51
     * @return void
52
     */
53
    public function filterNullAllowed()
54
    {
55
        $this->assertNull(DateTimeZone::filter(null, true));
56
    }
57
58
    /**
59
     * Verify behavior of filter() when $allowNull is false and $value is null.
60
     *
61
     * @test
62
     * @covers ::filter
63
     * @expectedException \TraderInteractive\Filter\Exception
64
     * @expectedExceptionMessage $value not a non-empty string
65
     *
66
     * @return void
67
     */
68
    public function filterNullNotAllowed()
69
    {
70
        $this->assertNull(DateTimeZone::filter(null, false));
71
    }
72
73
    /**
74
     * Verify behavior of filter() when $value is a \DateTimeZone object.
75
     *
76
     * @test
77
     * @covers ::filter
78
     *
79
     * @return void
80
     */
81
    public function filterTimeZonePass()
82
    {
83
        $timezone = new \DateTimeZone('America/New_York');
84
        $this->assertSame($timezone, DateTimeZone::filter($timezone));
85
    }
86
87
    /**
88
     * Verify behavior of filter() when $value is not a valid timezone.
89
     *
90
     * @test
91
     * @covers ::filter
92
     * @expectedException \TraderInteractive\Filter\Exception
93
     * @expectedExceptionMessage Unknown or bad timezone (INVALID)
94
     */
95
    public function filterInvalidName()
96
    {
97
        $timezone = DateTimeZone::filter('INVALID');
0 ignored issues
show
Unused Code introduced by
$timezone is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
98
    }
99
100
    /**
101
     * Verify behavior of filter() $value is a string with only whitespace.
102
     *
103
     * @param mixed $value The value to be filtered.
0 ignored issues
show
Bug introduced by
There is no parameter named $value. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
104
     *
105
     * @test
106
     * @covers ::filter
107
     * @expectedException \TraderInteractive\Filter\Exception
108
     * @expectedExceptionMessage $value not a non-empty string
109
     *
110
     * @return void
111
     */
112
    public function filterEmptyValue()
113
    {
114
        DateTimeZone::filter("\n\t");
115
    }
116
117
    /**
118
     * Verify behavior of filter() when $value is not a string.
119
     *
120
     * @test
121
     * @covers ::filter
122
     * @expectedException \TraderInteractive\Filter\Exception
123
     * @expectedExceptionMessage $value not a non-empty string
124
     *
125
     * @return void
126
     */
127
    public function filterNonStringArgument()
128
    {
129
        $timezone = DateTimeZone::filter(42);
0 ignored issues
show
Unused Code introduced by
$timezone is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
130
    }
131
}
132