HeaderValueHelper   A
last analyzed

Complexity

Total Complexity 40

Size/Duplication

Total Lines 284
Duplicated Lines 0 %

Test Coverage

Coverage 98.97%

Importance

Changes 2
Bugs 1 Features 0
Metric Value
wmc 40
eloc 92
c 2
b 1
f 0
dl 0
loc 284
ccs 96
cts 97
cp 0.9897
rs 9.2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getValueAndParameters() 0 21 4
B getSortedValueAndParameters() 0 60 11
C getSortedAcceptTypes() 0 64 16
B getParameters() 0 51 9

How to fix   Complexity   

Complex Class

Complex classes like HeaderValueHelper often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use HeaderValueHelper, and based on these observations, apply Extract Interface, too.

1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Http;
6
7
use InvalidArgumentException;
8
9
use function array_shift;
10
use function asort;
11
use function count;
12
use function explode;
13
use function implode;
14
use function is_array;
15
use function is_string;
16
use function mb_strtolower;
17
use function mb_strpos;
18
use function mb_substr;
19
use function preg_match;
20
use function preg_split;
21
use function preg_replace;
22
use function preg_replace_callback;
23
use function reset;
24
use function rtrim;
25
use function strtolower;
26
use function strpos;
27
use function substr;
28
use function trim;
29
use function usort;
30
31
/**
32
 * `HeaderValueHelper` parses the header value parameters.
33
 *
34
 * @psalm-type QFactorHeader = array{q: float}&non-empty-array<array-key, string>
35
 */
36
final class HeaderValueHelper
37
{
38
    /**
39
     * @link https://www.rfc-editor.org/rfc/rfc2616.html#section-2.2
40
     * token = 1*<any CHAR except CTLs or separators>
41
     */
42
    private const PATTERN_TOKEN = '(?:(?:[^()<>@,;:\\"\/[\\]?={} \t\x7f]|[\x00-\x1f])+)';
43
44
    /**
45
     * @link https://www.rfc-editor.org/rfc/rfc2616.html#section-3.6
46
     * attribute = token
47
     */
48
    private const PATTERN_ATTRIBUTE = self::PATTERN_TOKEN;
49
50
    /**
51
     * @link https://www.rfc-editor.org/rfc/rfc2616.html#section-2.2
52
     * quoted-string  = ( <"> *(qdtext | quoted-pair ) <"> )
53
     * qdtext         = <any TEXT except <">>
54
     * quoted-pair    = "\" CHAR
55
     */
56
    private const PATTERN_QUOTED_STRING = '(?:"(?:(?:\\\\.)+|[^\\"]+)*")';
57
58
    /**
59
     * @link https://www.rfc-editor.org/rfc/rfc2616.html#section-3.6
60
     * value = token | quoted-string
61
     */
62
    private const PATTERN_VALUE = '(?:' . self::PATTERN_QUOTED_STRING . '|' . self::PATTERN_TOKEN . ')';
63
64
    /**
65
     * Explodes a header value to value and parameters (eg. text/html;q=2;version=6)
66
     *
67
     * @link https://www.rfc-editor.org/rfc/rfc2616.html#section-3.6
68
     * transfer-extension = token *( ";" parameter )
69
     *
70
     * @param string $headerValue Header value.
71
     * @param bool $lowerCaseValue Whether should cast header value to lowercase.
72
     * @param bool $lowerCaseParameter Whether should cast header parameter name to lowercase.
73
     * @param bool $lowerCaseParameterValue Whether should cast header parameter value to lowercase.
74
     *
75
     * @return string[] First element is the value, and key-value are the parameters.
76
     */
77 31
    public static function getValueAndParameters(
78
        string $headerValue,
79
        bool $lowerCaseValue = true,
80
        bool $lowerCaseParameter = true,
81
        bool $lowerCaseParameterValue = true
82
    ): array {
83 31
        $headerValue = trim($headerValue);
84
85 31
        if ($headerValue === '') {
86 1
            return [];
87
        }
88
89 30
        $parts = explode(';', $headerValue, 2);
90 30
        $output = [$lowerCaseValue ? strtolower($parts[0]) : $parts[0]];
91
92 30
        if (count($parts) === 1) {
93 12
            return $output;
94
        }
95
        /** @psalm-var array{0:string,1:string} $parts */
96
97 27
        return $output + self::getParameters($parts[1], $lowerCaseParameter, $lowerCaseParameterValue);
98
    }
99
100
    /**
101
     * Explodes a header value parameters (eg. q=2;version=6)
102
     *
103
     * @link https://tools.ietf.org/html/rfc7230#section-3.2.6
104
     *
105
     * @param string $headerValueParameters Header value parameters.
106
     * @param bool $lowerCaseParameter Whether should cast header parameter name to lowercase.
107
     * @param bool $lowerCaseParameterValue Whether should cast header parameter value to lowercase.
108
     *
109
     * @return string[] Key-value are the parameters.
110
     *
111
     * @psalm-return array<string,string>
112
     */
113 60
    public static function getParameters(
114
        string $headerValueParameters,
115
        bool $lowerCaseParameter = true,
116
        bool $lowerCaseParameterValue = true
117
    ): array {
118 60
        $headerValueParameters = trim($headerValueParameters);
119
120 60
        if ($headerValueParameters === '') {
121
            return [];
122
        }
123
124 60
        if (rtrim($headerValueParameters, ';') !== $headerValueParameters) {
125 1
            throw new InvalidArgumentException('Cannot end with a semicolon.');
126
        }
127
128 59
        $output = [];
129
130
        do {
131
            /** @psalm-suppress InvalidArgument */
132 59
            $headerValueParameters = preg_replace_callback(
133 59
                '/^[ \t]*(?<parameter>' . self::PATTERN_ATTRIBUTE . ')[ \t]*=[ \t]*(?<value>' . self::PATTERN_VALUE . ')[ \t]*(?:;|$)/u',
134 59
                static function (array $matches) use (&$output, $lowerCaseParameter, $lowerCaseParameterValue) {
135 50
                    $value = $matches['value'];
136
137 50
                    if (mb_strpos($matches['value'], '"') === 0) {
138
                        // unescape + remove first and last quote
139 13
                        $value = preg_replace('/\\\\(.)/u', '$1', mb_substr($value, 1, -1));
140
                    }
141
142 50
                    $key = $lowerCaseParameter ? mb_strtolower($matches['parameter']) : $matches['parameter'];
143
144 50
                    if (isset($output[$key])) {
145
                        // The first is the winner.
146 2
                        return;
147
                    }
148
149
                    /** @psalm-suppress MixedArrayAssignment False-positive error */
150 50
                    $output[$key] = $lowerCaseParameterValue ? mb_strtolower($value) : $value;
151 59
                },
152 59
                $headerValueParameters,
153 59
                1,
154 59
                $count
155 59
            );
156
157 59
            if ($count !== 1) {
158 11
                throw new InvalidArgumentException('Invalid input: ' . $headerValueParameters);
159
            }
160 50
        } while ($headerValueParameters !== '');
161
        /** @var array<string,string> $output */
162
163 48
        return $output;
164
    }
165
166
    /**
167
     * Returns a header value as "q" factor sorted list.
168
     *
169
     * @link https://developer.mozilla.org/en-US/docs/Glossary/Quality_values
170
     * @link https://www.ietf.org/rfc/rfc2045.html#section-2
171
     * @see getValueAndParameters
172
     *
173
     * @param string|string[] $values Header value as a comma-separated string or already exploded string array.
174
     * @param bool $lowerCaseValue Whether should cast header value to lowercase.
175
     * @param bool $lowerCaseParameter Whether should cast header parameter name to lowercase.
176
     * @param bool $lowerCaseParameterValue Whether should cast header parameter value to lowercase.
177
     *
178
     * @return array[] The q factor sorted list.
179
     *
180
     * @psalm-return list<QFactorHeader>
181
     * @psalm-suppress MoreSpecificReturnType, LessSpecificReturnStatement Need for Psalm 4.30
182
     */
183 31
    public static function getSortedValueAndParameters(
184
        $values,
185
        bool $lowerCaseValue = true,
186
        bool $lowerCaseParameter = true,
187
        bool $lowerCaseParameterValue = true
188
    ): array {
189
        /** @var mixed $values Don't trust to annotations. */
190
191 31
        if (!is_array($values) && !is_string($values)) {
192 5
            throw new InvalidArgumentException('Values are neither array nor string.');
193
        }
194
195 26
        $list = [];
196 26
        foreach ((array) $values as $headerValue) {
197 24
            if (!is_string($headerValue)) {
198 1
                throw new InvalidArgumentException('Values must be array of strings.');
199
            }
200
201
            /** @psalm-suppress InvalidOperand Presume that `preg_split` never returns false here. */
202 23
            $list = [...$list, ...preg_split('/\s*,\s*/', trim($headerValue), -1, PREG_SPLIT_NO_EMPTY)];
203
        }
204
205
        /** @var string[] $list */
206
207 25
        if (count($list) === 0) {
208 4
            return [];
209
        }
210
211 21
        $output = [];
212
213 21
        foreach ($list as $value) {
214 21
            $parse = self::getValueAndParameters(
215 21
                $value,
216 21
                $lowerCaseValue,
217 21
                $lowerCaseParameter,
218 21
                $lowerCaseParameterValue
219 21
            );
220
            // case-insensitive "q" parameter
221 21
            $q = $parse['q'] ?? $parse['Q'] ?? 1.0;
222
223
            // min 0.000 max 1.000, max 3 digits, without digits allowed
224 21
            if (is_string($q) && preg_match('/^(?:0(?:\.\d{1,3})?|1(?:\.0{1,3})?)$/', $q) === 0) {
225 4
                throw new InvalidArgumentException('Invalid q factor.');
226
            }
227
228 17
            $parse['q'] = (float) $q;
229 17
            unset($parse['Q']);
230 17
            $output[] = $parse;
231
        }
232
233 17
        usort($output, static function (array $a, array $b) {
234 17
            $a = $a['q'];
235 17
            $b = $b['q'];
236 17
            if ($a === $b) {
237 9
                return 0;
238
            }
239 9
            return $a > $b ? -1 : 1;
240 17
        });
241
242 17
        return $output;
243
    }
244
245
    /**
246
     * Returns a list of sorted content types from the accept header values.
247
     *
248
     * @param string|string[] $values Header value as a comma-separated string or already exploded string array.
249
     *
250
     * @return string[] Sorted accept types. Note: According to RFC 7231, special parameters (except the q factor)
251
     * are added to the type, which are always appended by a semicolon and sorted by string.
252
     *
253
     * @link https://tools.ietf.org/html/rfc7231#section-5.3.2
254
     * @link https://www.ietf.org/rfc/rfc2045.html#section-2
255
     */
256 14
    public static function getSortedAcceptTypes($values): array
257
    {
258 14
        $output = self::getSortedValueAndParameters($values);
259
260 14
        usort($output, static function (array $a, array $b) {
261
            /**
262
             * @psalm-var QFactorHeader $a
263
             * @psalm-var QFactorHeader $b
264
             */
265
266 12
            if ($a['q'] !== $b['q']) {
267
                // The higher q value wins
268 5
                return $a['q'] > $b['q'] ? -1 : 1;
269
            }
270
271
            /** @var string $typeA */
272 8
            $typeA = reset($a);
273
274
            /** @var string $typeB */
275 8
            $typeB = reset($b);
276
277 8
            if (strpos($typeA, '*') === false && strpos($typeB, '*') === false) {
278 8
                $countA = count($a);
279 8
                $countB = count($b);
280 8
                if ($countA === $countB) {
281
                    // They are equivalent for the same parameter number
282 6
                    return 0;
283
                }
284
                // No wildcard character, higher parameter number wins
285 2
                return $countA > $countB ? -1 : 1;
286
            }
287
288 1
            $endWildcardA = substr($typeA, -1, 1) === '*';
289 1
            $endWildcardB = substr($typeB, -1, 1) === '*';
290
291 1
            if (($endWildcardA && !$endWildcardB) || (!$endWildcardA && $endWildcardB)) {
292
                // The wildcard ends is the loser.
293 1
                return $endWildcardA ? 1 : -1;
294
            }
295
296
            // The wildcard starts is the loser.
297 1
            return strpos($typeA, '*') === 0 ? 1 : -1;
298 14
        });
299
300 14
        foreach ($output as $key => $value) {
301 12
            $type = array_shift($value);
302 12
            unset($value['q']);
303
304 12
            if (count($value) === 0) {
305 11
                $output[$key] = $type;
306 11
                continue;
307
            }
308
309 4
            foreach ($value as $k => $v) {
310 4
                $value[$k] = $k . '=' . $v;
311
            }
312
313
            // Parameters are sorted for easier use of parameter variations.
314 4
            asort($value, SORT_STRING);
315 4
            $output[$key] = $type . ';' . implode(';', $value);
316
        }
317
        /** @var string[] $output */
318
319 14
        return $output;
320
    }
321
}
322