Completed
Pull Request — master (#42)
by Frederik
02:22
created

SectionList::fromString()   C

Complexity

Conditions 8
Paths 6

Size

Total Lines 32
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8.2518

Importance

Changes 0
Metric Value
dl 0
loc 32
ccs 16
cts 19
cp 0.8421
rs 5.3846
c 0
b 0
f 0
cc 8
eloc 20
nc 6
nop 1
crap 8.2518
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\MessageData;
5
6
/**
7
 * Class SectionList
8
 * @package Genkgo\Mail\Protocol\Imap\MessageData
9
 */
10
final class SectionList
11
{
12
    /**
13
     *
14
     */
15
    private CONST RFC_3501_SECTION_FIXED = [
16
        'HEADER' => true,
17
        'TEXT' => true,
18
    ];
19
20
    /**
21
     * @var array
22
     */
23
    private $sections = [];
24
25
    /**
26
     * SectionList constructor.
27
     * @param array $sections
28
     */
29 11
    public function __construct(array $sections = [])
30
    {
31 11
        foreach ($sections as $section) {
32 8
            $this->validateSection($section);
33
        }
34
35 9
        $this->sections = $sections;
36 9
    }
37
38
    /**
39
     * @return string
40
     */
41 8
    public function __toString(): string
42
    {
43 8
        return sprintf(
44 8
            '[%s]',
45 8
            implode(' ', $this->sections)
46
        );
47
    }
48
49
    /**
50
     * @param string $sections
51
     * @return SectionList
52
     */
53 2
    public static function fromString(string $sections): self
54
    {
55 2
        $result = preg_match('/^\[(.*?)\]$/', $sections, $matches);
56 2
        if ($result !== 1) {
57
            throw new \InvalidArgumentException('No section list');
58
        }
59
60 2
        $sections = array_filter(explode(' ', $matches[1]));
61 2
        for ($i = 0; $i < count($sections); $i++) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
62 2
            if ($sections[$i] === 'HEADER.FIELDS' || $sections[$i] === 'HEADER.FIELDS.NOT') {
63 1
                if ($sections[$i + 1][0] !== '(') {
64
                    throw new \InvalidArgumentException(
65
                        'HEADER.FIELDS or HEADER.FIELDS.NOT requires header-list'
66
                    );
67
                }
68
69 1
                $appendIndex = $i;
70 1
                $i++;
71
                do {
72 1
                    $sections[$appendIndex] .= ' ' . $sections[$i];
73 1
                    unset($sections[$i]);
74 1
                    $i++;
75
76 1
                    if (substr($sections[$appendIndex], -1) === ')') {
77 1
                        break;
78
                    }
79 1
                } while (isset($sections[$i]));
80
            }
81
        }
82
83 2
        return new self($sections);
84
    }
85
86
    /**
87
     * @param string $section
88
     */
89 8
    private function validateSection(string $section): void
90
    {
91 8
        if ($section === '') {
92
            throw new \InvalidArgumentException('Empty section');
93
        }
94
95 8
        if (isset(self::RFC_3501_SECTION_FIXED[$section])) {
96 3
            return;
97
        }
98
99 5
        if (preg_match('/^HEADER.FIELDS(\.NOT)? \((.*?)\)$/', $section) === 1) {
100 3
            return;
101
        }
102
103 2
        throw new \InvalidArgumentException('Invalid section item ' . $section);
104
    }
105
}