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); |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.