Completed
Push — master ( 0a059e...292010 )
by John
02:21
created

DefaultHeaderParser   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 85
Duplicated Lines 25.88 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 22
loc 85
ccs 30
cts 30
cp 1
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A parse() 0 8 1
B findAcceptableMIMETypes() 0 18 5
A findAPIKey() 11 11 4
A getContentType() 11 11 4

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 LunixREST\RequestFactory\HeaderParser;
3
4
/**
5
 * Parse out all the common headers from a request.
6
 * Class DefaultHeaderParser
7
 * @package LunixREST\RequestFactory\HeaderParser
8
 */
9
class DefaultHeaderParser implements HeaderParser
10
{
11
12
    /**
13
     * @var string
14
     */
15
    private $apiKeyHeaderKey;
16
17
    /**
18
     * DefaultHeaderParser constructor.
19
     * @param string $apiKeyHeaderKey
20
     */
21 7
    public function __construct($apiKeyHeaderKey = 'x-api-key')
22
    {
23 7
        $this->apiKeyHeaderKey = $apiKeyHeaderKey;
24 7
    }
25
26
    /**
27
     * @param array $headers
28
     * @return ParsedHeaders
29
     */
30 6
    public function parse(array $headers): ParsedHeaders
31
    {
32 6
        $contentType = $this->getContentType($headers);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $contentType is correct as $this->getContentType($headers) (which targets LunixREST\RequestFactory...arser::getContentType()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
33 6
        $acceptableMIMETypes = $this->findAcceptableMIMETypes($headers);
34 6
        $apiKey = $this->findAPIKey($headers);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $apiKey is correct as $this->findAPIKey($headers) (which targets LunixREST\RequestFactory...derParser::findAPIKey()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
35
36 6
        return new ParsedHeaders($contentType, $acceptableMIMETypes, $apiKey);
37
    }
38
39
    /**
40
     * @param array $headers
41
     * @return array
42
     */
43 6
    protected function findAcceptableMIMETypes(array $headers): array
44
    {
45
        //TODO: follow RFC2616 order
46 6
        $acceptedMIMETypes = [];
47
48 6
        foreach ($headers as $key => $values) {
49 6
            if (strtolower($key) == 'http-accept') {
50 1
                foreach ($values as $acceptedType) {
51 1
                    $typeParts = explode(';', $acceptedType);
52 1
                    if (count($typeParts) > 0) {
53 1
                        $acceptedMIMETypes[] = trim($typeParts[0]);
54
                    }
55
                }
56 1
                break;
57
            }
58
        }
59 6
        return $acceptedMIMETypes;
60
    }
61
62
    /**
63
     * @param array $headers
64
     * @return null
65
     */
66 6 View Code Duplication
    protected function findAPIKey(array $headers)
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...
67
    {
68 6
        foreach ($headers as $key => $values) {
69 6
            if (strtolower($key) == strtolower($this->apiKeyHeaderKey)) {
70 3
                foreach($values as $value) {
71 3
                    return $value;
72
                }
73
            }
74
        }
75 3
        return null;
76
    }
77
78
    /**
79
     * @param array $headers
80
     * @return null
81
     */
82 6 View Code Duplication
    protected function getContentType(array $headers)
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...
83
    {
84 6
        foreach ($headers as $key => $values) {
85 6
            if (strtolower($key) == 'content-type') {
86 2
                foreach ($values as $value) {
87 2
                    return $value;
88
                }
89
            }
90
        }
91 4
        return null;
92
    }
93
}
94