DefaultHeaderParser   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 86
Duplicated Lines 25.58 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 1
dl 22
loc 86
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
use LunixREST\RequestFactory\HeaderParser\Exceptions\UnableToParseHeadersException;
5
6
/**
7
 * Parse out all the common headers from a request.
8
 * Class DefaultHeaderParser
9
 * @package LunixREST\RequestFactory\HeaderParser
10
 */
11
class DefaultHeaderParser implements HeaderParser
12
{
13
14
    /**
15
     * @var string
16
     */
17
    private $apiKeyHeaderKey;
18
19
    /**
20
     * DefaultHeaderParser constructor.
21
     * @param string $apiKeyHeaderKey
22
     */
23 7
    public function __construct($apiKeyHeaderKey = 'x-api-key')
24
    {
25 7
        $this->apiKeyHeaderKey = $apiKeyHeaderKey;
26 7
    }
27
28
    /**
29
     * @param array $headers
30
     * @return ParsedHeaders
31
     * @throws UnableToParseHeadersException
32
     */
33 6
    public function parse(array $headers): ParsedHeaders
34
    {
35 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...
36 6
        $acceptableMIMETypes = $this->findAcceptableMIMETypes($headers);
37 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...
38
39 6
        return new ParsedHeaders($contentType, $acceptableMIMETypes, $apiKey);
40
    }
41
42
    /**
43
     * @param array $headers
44
     * @return array
45
     */
46 6
    protected function findAcceptableMIMETypes(array $headers): array
47
    {
48
        //TODO: follow RFC2616 order
49 6
        $acceptedMIMETypes = [];
50
51 6
        foreach ($headers as $key => $values) {
52 6
            if (strtolower($key) == 'http-accept') {
53 1
                foreach ($values as $acceptedType) {
54 1
                    $typeParts = explode(';', $acceptedType);
55 1
                    if (count($typeParts) > 0) {
56 1
                        $acceptedMIMETypes[] = trim($typeParts[0]);
57
                    }
58
                }
59 1
                break;
60
            }
61
        }
62 6
        return $acceptedMIMETypes;
63
    }
64
65
    /**
66
     * @param array $headers
67
     * @return null
68
     */
69 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...
70
    {
71 6
        foreach ($headers as $key => $values) {
72 6
            if (strtolower($key) == strtolower($this->apiKeyHeaderKey)) {
73 3
                foreach($values as $value) {
74 3
                    return $value;
75
                }
76
            }
77
        }
78 3
        return null;
79
    }
80
81
    /**
82
     * @param array $headers
83
     * @return null
84
     */
85 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...
86
    {
87 6
        foreach ($headers as $key => $values) {
88 6
            if (strtolower($key) == 'content-type') {
89 2
                foreach ($values as $value) {
90 2
                    return $value;
91
                }
92
            }
93
        }
94 4
        return null;
95
    }
96
}
97