Passed
Push — master ( c44d00...13b3c2 )
by David
53s
created

IsSafeHttpMethodTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 137
Duplicated Lines 13.14 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 18
loc 137
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testSafeMethods() 9 9 1
B httpMethodsProvider() 0 35 1
A testSafeMethodsWithDefaults() 9 9 1
A safeDefaultsMatchingProvider() 0 65 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
declare(strict_types=1);
3
4
namespace TheCodingMachine\Middlewares\SafeRequests;
5
6
use PHPUnit\Framework\TestCase;
7
use Psr\Http\Message\RequestInterface;
8
use Psr\Http\Message\ServerRequestInterface;
9
10
/**
11
 * @covers \TheCodingMachine\Middlewares\SafeRequests\IsSafeHttpMethod
12
 */
13
final class IsSafeHttpMethodTest extends \PHPUnit_Framework_TestCase
14
{
15
    /**
16
     * @dataProvider httpMethodsProvider
17
     *
18
     * @param array  $safeMethods
19
     * @param string $httpMethod
20
     * @param bool   $expectedResult
21
     */
22 View Code Duplication
    public function testSafeMethods(array $safeMethods, string $httpMethod, bool $expectedResult)
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
        /* @var $request RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
25
        $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
26
27
        $request->expects(self::any())->method('getMethod')->willReturn($httpMethod);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Psr\Http\Message\RequestInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
28
29
        self::assertSame($expectedResult, (new IsSafeHttpMethod(...$safeMethods))->__invoke($request));
0 ignored issues
show
Documentation introduced by
$request is of type object<Psr\Http\Message\..._MockObject_MockObject>, but the function expects a object<Psr\Http\Message\ServerRequestInterface>.

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...
30
    }
31
32
    public function httpMethodsProvider() : array
33
    {
34
        return [
35
            'empty' => [
36
                [],
37
                'GET',
38
                false,
39
            ],
40
            'GET only' => [
41
                ['GET'],
42
                'GET',
43
                true,
44
            ],
45
            'get only' => [
46
                ['get'],
47
                'GET',
48
                true,
49
            ],
50
            'GET only, matching lowercase get' => [
51
                ['GET'],
52
                'get',
53
                true,
54
            ],
55
            'GET only, non-matching method' => [
56
                ['GET'],
57
                'PUT',
58
                false,
59
            ],
60
            'GET, PUT only, matching method' => [
61
                ['GET', 'PUT'],
62
                'PUT',
63
                true,
64
            ],
65
        ];
66
    }
67
68
    /**
69
     * @dataProvider safeDefaultsMatchingProvider
70
     *
71
     * @param string $httpMethod
72
     * @param bool   $expectedResult
73
     */
74 View Code Duplication
    public function testSafeMethodsWithDefaults(string $httpMethod, bool $expectedResult)
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...
75
    {
76
        /* @var $request RequestInterface|\PHPUnit_Framework_MockObject_MockObject */
77
        $request = $this->getMockBuilder(ServerRequestInterface::class)->getMock();
78
79
        $request->expects(self::any())->method('getMethod')->willReturn($httpMethod);
0 ignored issues
show
Bug introduced by
The method expects does only exist in PHPUnit_Framework_MockObject_MockObject, but not in Psr\Http\Message\RequestInterface.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
80
81
        self::assertSame($expectedResult, IsSafeHttpMethod::fromDefaultSafeMethods()->__invoke($request));
82
    }
83
84
    public function safeDefaultsMatchingProvider() : array
85
    {
86
        return [
87
            'empty' => [
88
                '',
89
                false,
90
            ],
91
            'GET' => [
92
                'GET',
93
                true,
94
            ],
95
            'get' => [
96
                'get',
97
                true,
98
            ],
99
            'HEAD' => [
100
                'HEAD',
101
                true,
102
            ],
103
            'head' => [
104
                'head',
105
                true,
106
            ],
107
            'OPTIONS' => [
108
                'OPTIONS',
109
                true,
110
            ],
111
            'options' => [
112
                'options',
113
                true,
114
            ],
115
            'DELETE' => [
116
                'DELETE',
117
                false,
118
            ],
119
            'delete' => [
120
                'delete',
121
                false,
122
            ],
123
            'POST' => [
124
                'POST',
125
                false,
126
            ],
127
            'post' => [
128
                'post',
129
                false,
130
            ],
131
            'PUT' => [
132
                'PUT',
133
                false,
134
            ],
135
            'put' => [
136
                'put',
137
                false,
138
            ],
139
            'UNKNOWN' => [
140
                'UNKNOWN',
141
                false,
142
            ],
143
            'unknown' => [
144
                'unknown',
145
                false,
146
            ],
147
        ];
148
    }
149
}
150