GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Pull Request — master (#82)
by William
06:27
created

AbstractNegotiator   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 112
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.62%

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 3
dl 0
loc 112
ccs 41
cts 42
cp 0.9762
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
B getBest() 0 37 6
acceptFactory() 0 1 ?
A match() 0 15 3
A parseHeader() 0 10 2
A findMatches() 0 13 4
1
<?php
2
3
namespace Negotiation;
4
5
use Negotiation\Exception\InvalidArgument;
6
use Negotiation\Exception\InvalidHeader;
7
8
abstract class AbstractNegotiator
9
{
10
    /**
11
     * @param string $header     A string containing an `Accept|Accept-*` header.
12
     * @param array  $priorities A set of server priorities.
13
     *
14
     * @return AcceptHeader|null best matching type
15
     */
16 58
    public function getBest($header, array $priorities)
17
    {
18 58
        if (empty($priorities)) {
19 1
            throw new InvalidArgument('A set of server priorities should be given.');
20
        }
21
22 57
        if (!$header) {
23 3
            throw new InvalidArgument('The header string should not be empty.');
24
        }
25
26 54
        $headers = $this->parseHeader($header);
0 ignored issues
show
Unused Code introduced by
$headers 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...
27
28
        // Once upon a time, two `array_map` calls were sitting there, but for
29
        // some reasons, they triggered `E_WARNING` time to time (because of
30
        // PHP bug [55416](https://bugs.php.net/bug.php?id=55416). Now, they
31
        // are gone.
32
        // See: https://github.com/willdurand/Negotiation/issues/81
33
34 54
        $acceptedHeaders = array();
35 54
        foreach ($this->parseHeader($header) as $h) {
36 54
            $acceptedHeaders[] = $this->acceptFactory($h);
0 ignored issues
show
Documentation introduced by
$h is of type object<Negotiation\AcceptHeader>, but the function expects a string.

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...
37 53
        }
38
39 53
        $acceptedPriorities = array();
40 53
        foreach ($priorities as $p) {
41 53
            $acceptedPriorities[] = $this->acceptFactory($p);
42 52
        }
43
44 52
        $matches         = $this->findMatches($acceptedHeaders, $acceptedPriorities);
45 52
        $specificMatches = array_reduce($matches, 'Negotiation\Match::reduce', []);
46
47 52
        usort($specificMatches, 'Negotiation\Match::compare');
48
49 52
        $match = array_shift($specificMatches);
50
51 52
        return null === $match ? null : $acceptedPriorities[$match->index];
52
    }
53
54
    /**
55
     * @param string $header accept header part or server priority
56
     *
57
     * @return AcceptHeader Parsed header object
58
     */
59
    abstract protected function acceptFactory($header);
60
61
    /**
62
     * @param AcceptHeader $header
63
     * @param AcceptHeader $priority
64
     * @param integer      $index
65
     *
66
     * @return Match|null Headers matched
67
     */
68 17
    protected function match(AcceptHeader $header, AcceptHeader $priority, $index)
69
    {
70 17
        $ac = $header->getType();
71 17
        $pc = $priority->getType();
72
73 17
        $equal = !strcasecmp($ac, $pc);
74
75 17
        if ($equal || $ac === '*') {
76 14
            $score = 1 * $equal;
77
78 14
            return new Match($header->getQuality(), $score, $index);
79
        }
80
81 17
        return null;
82
    }
83
84
    /**
85
     * @param string $header A string that contains an `Accept*` header.
86
     *
87
     * @return AcceptHeader[]
88
     */
89 71
    private function parseHeader($header)
90
    {
91 71
        $res = preg_match_all('/(?:[^,"]*+(?:"[^"]*+")?)+[^,"]*+/', $header, $matches);
92
93 71
        if (!$res) {
94
            throw new InvalidHeader(sprintf('Failed to parse accept header: "%s"', $header));
95
        }
96
97 71
        return array_values(array_filter(array_map('trim', $matches[0])));
98
    }
99
100
    /**
101
     * @param AcceptHeader[] $headerParts
102
     * @param Priority[]     $priorities  Configured priorities
103
     *
104
     * @return Match[] Headers matched
105
     */
106 55
    private function findMatches(array $headerParts, array $priorities)
107
    {
108 55
        $matches = [];
109 55
        foreach ($priorities as $index => $p) {
110 55
            foreach ($headerParts as $h) {
111 55
                if (null !== $match = $this->match($h, $p, $index)) {
112 46
                    $matches[] = $match;
113 46
                }
114 55
            }
115 55
        }
116
117 55
        return $matches;
118
    }
119
}
120