Host   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 43
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
c 1
b 0
f 0
dl 0
loc 43
ccs 15
cts 15
cp 1
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 21 4
A getValue() 0 3 1
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message\Uri\Component;
13
14
use Sunrise\Http\Message\Exception\InvalidArgumentException;
15
16
use function is_string;
17
use function preg_replace_callback;
18
use function rawurlencode;
19
use function strtolower;
20
21
/**
22
 * @link https://tools.ietf.org/html/rfc3986#section-3.2.2
23
 */
24
final class Host implements ComponentInterface
25
{
26
    // phpcs:ignore Generic.Files.LineLength
27
    private const NORMALIZATION_REGEX = '/(?:%[0-9A-Fa-f]{2}|[\x21\x24\x26-\x2e\x30-\x39\x3b\x3d\x41-\x5a\x5f\x61-\x7a\x7e]+)|(.?)/u';
28
29
    private string $value = '';
30
31
    /**
32
     * @param mixed $value
33
     *
34
     * @throws InvalidArgumentException
35
     */
36 124
    public function __construct($value)
37
    {
38 124
        if ($value === '') {
39 2
            return;
40
        }
41
42 124
        if (!is_string($value)) {
43 9
            throw new InvalidArgumentException('URI component "host" must be a string');
44
        }
45
46 115
        $this->value = (string) preg_replace_callback(
47 115
            self::NORMALIZATION_REGEX,
48 115
            static fn(array $matches): string => (
49
                /** @var array{0: string, 1?: string} $matches */
50 115
                isset($matches[1]) ? rawurlencode($matches[1]) : $matches[0]
51 115
            ),
52 115
            $value,
53 115
        );
54
55
        // the component is case-insensitive...
56 115
        $this->value = strtolower($this->value);
57
    }
58
59
    /**
60
     * {@inheritdoc}
61
     *
62
     * @return string
63
     */
64 115
    public function getValue(): string
65
    {
66 115
        return $this->value;
67
    }
68
}
69