Completed
Pull Request — master (#50)
by Frederik
02:46
created

HeaderName::validate()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 6.0493

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 9
cp 0.8889
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 9
nc 4
nop 1
crap 6.0493
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Header;
5
6
final class HeaderName
7
{
8
    /**
9
     * @var string
10
     */
11
    private $name;
12
13
    /**
14
     * @param string $name
15
     */
16 180
    public function __construct(string $name)
17
    {
18 180
        $this->assert($name);
19
20 173
        $this->name = $name;
21 173
    }
22
23
    /**
24
     * @param string $name
25
     * @return bool
26
     */
27 5
    public function is(string $name): bool
28
    {
29 5
        return \strcasecmp($this->name, $name) === 0;
30
    }
31
32
    /**
33
     * @param string $name
34
     */
35 180
    private function assert(string $name)
36
    {
37 180
        if ($this->validate($name) === false) {
38 7
            throw new \InvalidArgumentException(
39 7
                'Invalid header name ' . $name . '. Wrong characters used or length too long (max 74)'
40
            );
41
        }
42 173
    }
43
44
    /**
45
     * @param string $name
46
     * @return bool
47
     */
48 180
    private function validate(string $name): bool
49
    {
50 180
        $tot = \strlen($name);
51
52 180
        if ($tot > 74) {
53
            return false;
54
        }
55
56 180
        for ($i = 0; $i < $tot; $i += 1) {
57 180
            $ord = \ord($name[$i]);
58 180
            if ($ord < 33 || $ord > 126 || $ord === 58) {
59 7
                return false;
60
            }
61
        }
62 173
        return true;
63
    }
64
65
    /**
66
     * @return string
67
     */
68 168
    public function __toString(): string
69
    {
70 168
        return $this->name;
71
    }
72
}
73