Completed
Pull Request — master (#42)
by Frederik
01:59
created

MailboxWildcard::__construct()   C

Complexity

Conditions 10
Paths 15

Size

Total Lines 48
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 10

Importance

Changes 0
Metric Value
dl 0
loc 48
ccs 29
cts 29
cp 1
rs 5.3454
c 0
b 0
f 0
cc 10
eloc 29
nc 15
nop 1
crap 10

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap;
5
6
/**
7
 *
8
 */
9
final class MailboxWildcard
10
{
11
    /**
12
     *
13
     */
14
    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";
15
    /**
16
     *
17
     */
18
    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";
19
20
    /**
21
     *
22
     */
23
    private CONST RFC_3501_FIXED = [
24
        "INBOX" => true,
25
    ];
26
27
    /**
28
     *
29
     */
30
    private CONST PARSE_STATE_UNQUOTED = 1;
31
    /**
32
     *
33
     */
34
    private CONST PARSE_STATE_QUOTED = 2;
35
36
    /**
37
     * @var string
38
     */
39
    private $name;
40
41
    /**
42
     * Mailbox constructor.
43
     * @param string $name
44
     */
45 13
    public function __construct(string $name)
46
    {
47 13
        if (isset(self::RFC_3501_FIXED[$name])) {
48 1
            return;
49
        }
50
51 13
        $index = 0;
52 13
        $state = self::PARSE_STATE_UNQUOTED;
53 13
        while (isset($name[$index])) {
54 10
            $char = $name[$index];
55
56
            switch ($state) {
57 10
                case self::PARSE_STATE_UNQUOTED:
58 10
                    if ($char === '"') {
59 4
                        $state = self::PARSE_STATE_QUOTED;
60 4
                        break;
61
                    }
62
63 9
                    if (strlen($char) !== strcspn($char, self::RFC_3501_ASTRING)) {
64 1
                        throw new \InvalidArgumentException(
65 1
                            \sprintf(
66 1
                                'Invalid mailbox character %s, use a quoted string for a broader range of allowed character',
67 1
                                $char
68
                            )
69
                        );
70
                    }
71 9
                    break;
72 4
                case self::PARSE_STATE_QUOTED:
73 4
                    if ($char === '"') {
74 2
                        $state = self::PARSE_STATE_UNQUOTED;
75 2
                        break;
76
                    }
77
78 4
                    if (strlen($char) !== strcspn($char, self::RFC_3501_TEXT_CHAR)) {
79 1
                        throw new \InvalidArgumentException('Invalid mailbox name');
80
                    }
81 3
                    break;
82
            }
83
84 10
            $index++;
85
        }
86
87 11
        if ($state === self::PARSE_STATE_QUOTED) {
88 1
            throw new \InvalidArgumentException('Unfinished quoted literal');
89
        }
90
91 10
        $this->name = $name;
92 10
    }
93
94
    /**
95
     * @return string
96
     */
97 5
    public function __toString(): string
98
    {
99 5
        if ($this->name === '') {
100 2
            return '""';
101
        }
102
103 3
        return $this->name;
104
    }
105
}