MailboxWildcard   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 83
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 12
lcom 0
cbo 0
dl 0
loc 83
ccs 33
cts 33
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
B __construct() 0 48 10
A __toString() 0 8 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap;
5
6
final class MailboxWildcard
7
{
8
    private const RFC_3501_ASTRING = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x20\x1A\x1B\x1C\x1D\x1E\x1F\x22\x28\x29\x5C\x7B\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF";
9
    
10
    private const RFC_3501_TEXT_CHAR = "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E\x1F\x7F\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8A\x8B\x8C\x8D\x8E\x8F\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9A\x9B\x9C\x9D\x9E\x9F\xA0\xA1\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xAA\xAB\xAC\xAD\xAE\xAF\xB0\xB1\xB2\xB3\xB4\xB5\xB6\xB7\xB8\xB9\xBA\xBB\xBC\xBD\xBE\xBF\xC0\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xCA\xCB\xCC\xCD\xCE\xCF\xD0\xD1\xD2\xD3\xD4\xD5\xD6\xD7\xD8\xD9\xDA\xDB\xDC\xDD\xDE\xDF\xE0\xE1\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xEA\xEB\xEC\xED\xEE\xEF\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\xFA\xFB\xFC\xFD\xFE\xFF";
11
    
12
    private const RFC_3501_FIXED = [
13
        "INBOX" => true,
14
    ];
15
    
16
    private const PARSE_STATE_UNQUOTED = 1;
17
    
18
    private const PARSE_STATE_QUOTED = 2;
19
20
    /**
21
     * @var string
22
     */
23
    private $name;
24
25
    /**
26
     * @param string $name
27
     */
28 13
    public function __construct(string $name)
29
    {
30 13
        if (isset(self::RFC_3501_FIXED[$name])) {
31 1
            return;
32
        }
33
34 13
        $index = 0;
35 13
        $state = self::PARSE_STATE_UNQUOTED;
36 13
        while (isset($name[$index])) {
37 10
            $char = $name[$index];
38
39
            switch ($state) {
40 10
                case self::PARSE_STATE_UNQUOTED:
41 10
                    if ($char === '"') {
42 4
                        $state = self::PARSE_STATE_QUOTED;
43 4
                        break;
44
                    }
45
46 9
                    if (\strlen($char) !== \strcspn($char, self::RFC_3501_ASTRING)) {
47 1
                        throw new \InvalidArgumentException(
48 1
                            \sprintf(
49 1
                                'Invalid mailbox character %s, use a quoted string for a broader range of allowed character',
50 1
                                $char
51
                            )
52
                        );
53
                    }
54 9
                    break;
55 4
                case self::PARSE_STATE_QUOTED:
56 4
                    if ($char === '"') {
57 2
                        $state = self::PARSE_STATE_UNQUOTED;
58 2
                        break;
59
                    }
60
61 4
                    if (\strlen($char) !== \strcspn($char, self::RFC_3501_TEXT_CHAR)) {
62 1
                        throw new \InvalidArgumentException('Invalid mailbox name');
63
                    }
64 3
                    break;
65
            }
66
67 10
            $index++;
68
        }
69
70 11
        if ($state === self::PARSE_STATE_QUOTED) {
71 1
            throw new \InvalidArgumentException('Unfinished quoted literal');
72
        }
73
74 10
        $this->name = $name;
75 10
    }
76
77
    /**
78
     * @return string
79
     */
80 5
    public function __toString(): string
81
    {
82 5
        if ($this->name === '') {
83 2
            return '""';
84
        }
85
86 3
        return $this->name;
87
    }
88
}
89