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

HeaderName   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 67
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 95.45%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 0
dl 0
loc 67
ccs 21
cts 22
cp 0.9545
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A is() 0 4 1
A assert() 0 8 2
B validate() 0 16 6
A __toString() 0 4 1
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