SectionList::validateSection()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 4

Importance

Changes 0
Metric Value
dl 0
loc 16
ccs 8
cts 8
cp 1
rs 9.7333
c 0
b 0
f 0
cc 4
nc 4
nop 1
crap 4
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\MessageData;
5
6
final class SectionList
7
{
8
    private const RFC_3501_SECTION_FIXED = [
9
        'HEADER' => true,
10
        'TEXT' => true,
11
    ];
12
13
    /**
14
     * @var array<int, string>
15
     */
16
    private $sections = [];
17
18
    /**
19
     * @param array<int, string> $sections
20
     */
21 20
    public function __construct(array $sections = [])
22
    {
23 20
        foreach ($sections as $section) {
24 14
            $this->validateSection($section);
25
        }
26
27 16
        $this->sections = $sections;
28 16
    }
29
30
    /**
31
     * @return string
32
     */
33 14
    public function __toString(): string
34
    {
35 14
        return \sprintf(
36 14
            '[%s]',
37 14
            \implode(' ', $this->sections)
38
        );
39
    }
40
41
    /**
42
     * @param string $sections
43
     * @return SectionList
44
     */
45 11
    public static function fromString(string $sections): self
46
    {
47 11
        $result = \preg_match('/^\[(.*?)\]$/', $sections, $matches);
48 11
        if ($result !== 1) {
49 1
            throw new \InvalidArgumentException('No section list');
50
        }
51
52 10
        $sections = \array_filter(\explode(' ', $matches[1]));
53 10
        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...
54 8
            if ($sections[$i] === 'HEADER.FIELDS' || $sections[$i] === 'HEADER.FIELDS.NOT') {
55 3
                if (!isset($sections[$i + 1][0]) || $sections[$i + 1][0] !== '(') {
56 2
                    throw new \InvalidArgumentException(
57 2
                        'HEADER.FIELDS or HEADER.FIELDS.NOT requires header-list'
58
                    );
59
                }
60
61 1
                $appendIndex = $i;
62 1
                $i++;
63
                do {
64 1
                    $sections[$appendIndex] .= ' ' . $sections[$i];
65 1
                    unset($sections[$i]);
66 1
                    $i++;
67
68 1
                    if (\substr($sections[$appendIndex], -1) === ')') {
69 1
                        break;
70
                    }
71 1
                } while (isset($sections[$i]));
72
            }
73
        }
74
75 8
        return new self($sections);
76
    }
77
78
    /**
79
     * @param string $section
80
     */
81 14
    private function validateSection(string $section): void
82
    {
83 14
        if ($section === '') {
84 1
            throw new \InvalidArgumentException('Empty section');
85
        }
86
87 13
        if (isset(self::RFC_3501_SECTION_FIXED[$section])) {
88 7
            return;
89
        }
90
91 6
        if (\preg_match('/^HEADER.FIELDS(\.NOT)? \((.*?)\)$/', $section) === 1) {
92 3
            return;
93
        }
94
95 3
        throw new \InvalidArgumentException('Invalid section item ' . $section);
96
    }
97
}
98