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 ( 5154ac...1ebc51 )
by William
03:13
created

AbstractNegotiator::acceptFactory()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

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