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
Push — master ( bc8142...d8df64 )
by William
26s
created

AbstractNegotiator::getBest()   C

Complexity

Conditions 8
Paths 15

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 8

Importance

Changes 0
Metric Value
dl 0
loc 38
ccs 21
cts 21
cp 1
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 20
nc 15
nop 3
crap 8
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 69
    public function getBest($header, array $priorities, $strict = false)
17
    {
18 69
        if (empty($priorities)) {
19 1
            throw new InvalidArgument('A set of server priorities should be given.');
20
        }
21
22 68
        if (!$header) {
23 3
            throw new InvalidArgument('The header string should not be empty.');
24
        }
25
26
        // Once upon a time, two `array_map` calls were sitting there, but for
27
        // some reasons, they triggered `E_WARNING` time to time (because of
28
        // PHP bug [55416](https://bugs.php.net/bug.php?id=55416). Now, they
29
        // are gone.
30
        // See: https://github.com/willdurand/Negotiation/issues/81
31 65
        $acceptedHeaders = array();
32 65
        foreach ($this->parseHeader($header) as $h) {
33
            try {
34 65
                $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...
35 65
            } catch (Exception\Exception $e) {
36 3
                if ($strict) {
37 1
                    throw $e;
38
                }
39
            }
40 64
        }
41 64
        $acceptedPriorities = array();
42 64
        foreach ($priorities as $p) {
43 64
            $acceptedPriorities[] = $this->acceptFactory($p);
44 63
        }
45 63
        $matches         = $this->findMatches($acceptedHeaders, $acceptedPriorities);
46 63
        $specificMatches = array_reduce($matches, 'Negotiation\Match::reduce', []);
47
48 63
        usort($specificMatches, 'Negotiation\Match::compare');
49
50 63
        $match = array_shift($specificMatches);
51
52 63
        return null === $match ? null : $acceptedPriorities[$match->index];
53
    }
54
55
    /**
56
     * @param string $header accept header part or server priority
57
     *
58
     * @return AcceptHeader Parsed header object
59
     */
60
    abstract protected function acceptFactory($header);
61
62
    /**
63
     * @param AcceptHeader $header
64
     * @param AcceptHeader $priority
65
     * @param integer      $index
66
     *
67
     * @return Match|null Headers matched
68
     */
69 22
    protected function match(AcceptHeader $header, AcceptHeader $priority, $index)
70
    {
71 22
        $ac = $header->getType();
72 22
        $pc = $priority->getType();
73
74 22
        $equal = !strcasecmp($ac, $pc);
75
76 22
        if ($equal || $ac === '*') {
77 19
            $score = 1 * $equal;
78
79 19
            return new Match($header->getQuality() * $priority->getQuality(), $score, $index);
80
        }
81
82 22
        return null;
83
    }
84
85
    /**
86
     * @param string $header A string that contains an `Accept*` header.
87
     *
88
     * @return AcceptHeader[]
89
     */
90 82
    private function parseHeader($header)
91
    {
92 82
        $res = preg_match_all('/(?:[^,"]*+(?:"[^"]*+")?)+[^,"]*+/', $header, $matches);
93
94 82
        if (!$res) {
95
            throw new InvalidHeader(sprintf('Failed to parse accept header: "%s"', $header));
96
        }
97
98 82
        return array_values(array_filter(array_map('trim', $matches[0])));
99
    }
100
101
    /**
102
     * @param AcceptHeader[] $headerParts
103
     * @param Priority[]     $priorities  Configured priorities
104
     *
105
     * @return Match[] Headers matched
106
     */
107 66
    private function findMatches(array $headerParts, array $priorities)
108
    {
109 66
        $matches = [];
110 66
        foreach ($priorities as $index => $p) {
111 66
            foreach ($headerParts as $h) {
112 65
                if (null !== $match = $this->match($h, $p, $index)) {
113 56
                    $matches[] = $match;
114 56
                }
115 66
            }
116 66
        }
117
118 66
        return $matches;
119
    }
120
}
121