HostHeaderTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 37
Duplicated Lines 29.73 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 11
loc 37
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A testFetchHost() 0 10 1
A testForwardedHostIgnored() 11 11 1
A testMultipleHostHeaders() 0 11 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 TheCodingMachine\Middlewares\OriginFetchers;
4
5
use PHPUnit\Framework\TestCase;
6
use TheCodingMachine\Middlewares\CsrfHeaderCheckMiddlewareException;
7
use Zend\Diactoros\ServerRequest;
8
9
class HostHeaderTest extends TestCase
10
{
11
    public function testFetchHost()
12
    {
13
        $request = new ServerRequest([], [], "http://alice.com:8080/hello", "Post");
14
15
        $hostHeader = new HostHeader();
16
17
        $hosts = $hostHeader($request);
18
19
        $this->assertSame(['alice.com'], $hosts);
20
    }
21
22 View Code Duplication
    public function testForwardedHostIgnored()
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
        $request = new ServerRequest([], [], "http://alice.com:8080/hello", "Post");
25
        $request = $request->withHeader('X-Forwarded-Host', 'eve.com');
26
27
        $hostHeader = new HostHeader();
28
29
        $hosts = $hostHeader($request);
30
31
        $this->assertSame(['alice.com'], $hosts);
32
    }
33
34
    public function testMultipleHostHeaders()
35
    {
36
        $request = new ServerRequest([], [], "http://alice.com:8080/hello", "Post");
37
        $request = $request->withAddedHeader('Host', 'eve.com');
38
39
        $hostHeader = new HostHeader();
40
41
        $this->expectException(CsrfHeaderCheckMiddlewareException::class);
42
        $this->expectExceptionMessage("Unexpected request: more than one HOST header sent.");
43
        $hostHeader($request);
44
    }
45
}
46