Completed
Pull Request — master (#42)
by Frederik
04:41
created

MailboxName::__construct()   C

Complexity

Conditions 11
Paths 16

Size

Total Lines 52
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 31
CRAP Score 11

Importance

Changes 0
Metric Value
dl 0
loc 52
ccs 31
cts 31
cp 1
rs 5.9999
c 0
b 0
f 0
cc 11
eloc 31
nc 16
nop 1
crap 11

How to fix   Long Method    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 MailboxName
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\x25\x28\x29\x2A\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 10
    public function __construct(string $name)
46
    {
47 10
        if ($name === '') {
48 1
            throw new \InvalidArgumentException('Mailbox name cannot be empty');
49
        }
50
51 9
        if (isset(self::RFC_3501_FIXED[$name])) {
52 1
            return;
53
        }
54
55 9
        $index = 0;
56 9
        $state = self::PARSE_STATE_UNQUOTED;
57 9
        while (isset($name[$index])) {
58 9
            $char = $name[$index];
59
60
            switch ($state) {
61 9
                case self::PARSE_STATE_UNQUOTED:
62 9
                    if ($char === '"') {
63 5
                        $state = self::PARSE_STATE_QUOTED;
64 5
                        break;
65
                    }
66
67 7
                    if (strlen($char) !== strcspn($char, self::RFC_3501_ASTRING)) {
68 3
                        throw new \InvalidArgumentException(
69 3
                            \sprintf(
70 3
                                'Invalid mailbox character %s, use a quoted string for a broader range of allowed character',
71 3
                                $char
72
                            )
73
                        );
74
                    }
75 7
                    break;
76 5
                case self::PARSE_STATE_QUOTED:
77 5
                    if ($char === '"') {
78 3
                        $state = self::PARSE_STATE_UNQUOTED;
79 3
                        break;
80
                    }
81
82 5
                    if (strlen($char) !== strcspn($char, self::RFC_3501_TEXT_CHAR)) {
83 1
                        throw new \InvalidArgumentException('Invalid mailbox name');
84
                    }
85 4
                    break;
86
            }
87
88 9
            $index++;
89
        }
90
91 5
        if ($state === self::PARSE_STATE_QUOTED) {
92 1
            throw new \InvalidArgumentException('Unfinished quoted literal');
93
        }
94
95 4
        $this->name = $name;
96 4
    }
97
98
    /**
99
     * @return string
100
     */
101 1
    public function __toString(): string
102
    {
103 1
        return $this->name;
104
    }
105
}