Completed
Pull Request — master (#87)
by Frederik
03:37
created

ParsedFetchCommandResponse::assertCompletion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Protocol\Imap\Response\Command;
5
6
use Genkgo\Mail\Exception\AssertionFailedException;
7
use Genkgo\Mail\Protocol\Imap\MessageData\ItemList;
8
use Genkgo\Mail\Protocol\Imap\Response\CompletionResult;
9
use Genkgo\Mail\Protocol\Imap\ResponseInterface;
10
11
final class ParsedFetchCommandResponse implements ResponseInterface
12
{
13
    /**
14
     * @var int
15
     */
16
    private $number;
17
18
    /**
19
     * @var ItemList
20
     */
21
    private $itemList;
22
23
    /**
24
     * @param \Iterator<int, string> $lineIterator
25
     */
26 7
    public function __construct(\Iterator $lineIterator)
27
    {
28 7
        $line = $lineIterator->current();
29 7
        if (\substr($line, 0, 2) !== '* ') {
30
            throw new \InvalidArgumentException('Expecting an untagged response');
31
        }
32
33 7
        $lineContent = \substr($line, 2);
34
35 7
        $fetchStart = \preg_match('/^([0-9]+) FETCH (.*)$/s', $lineContent, $matches);
36 7
        if ($fetchStart !== 1) {
37
            throw new \InvalidArgumentException('Expecting a fetch command response');
38
        }
39
40 7
        $this->number = (int)$matches[1];
41 7
        $this->itemList = ItemList::fromBytes(
0 ignored issues
show
Documentation Bug introduced by
It seems like \Genkgo\Mail\Protocol\Im...; } } })()) of type object<self> is incompatible with the declared type object<Genkgo\Mail\Proto...p\MessageData\ItemList> of property $itemList.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
42
            (function () use ($matches, $lineIterator) {
43 7
                foreach (\str_split($matches[2]) as $char) {
44 7
                    yield $char;
45
                }
46
47 5
                while ($lineIterator->valid()) {
48 5
                    $lineIterator->next();
49
50 5
                    foreach (\str_split($lineIterator->current()) as $char) {
51 5
                        yield $char;
52
                    }
53
                }
54 7
            })()
55
        );
56 7
    }
57
58
    /**
59
     * @return string
60
     */
61 2
    public function __toString(): string
62
    {
63 2
        return \sprintf("* %s\r\n", $this->getBody());
64
    }
65
66
    /**
67
     * @param CompletionResult $expectedResult
68
     * @return ResponseInterface
69
     * @throws AssertionFailedException
70
     */
71 1
    public function assertCompletion(CompletionResult $expectedResult): ResponseInterface
72
    {
73 1
        throw new AssertionFailedException('A parsed fetch command response is a completion result');
74
    }
75
76
    /**
77
     * @return ResponseInterface
78
     * @throws AssertionFailedException
79
     */
80 2
    public function assertContinuation(): ResponseInterface
81
    {
82 2
        throw new AssertionFailedException();
83
    }
84
85
    /**
86
     * @return ResponseInterface
87
     * @throws AssertionFailedException
88
     */
89 2
    public function assertTagged(): ResponseInterface
90
    {
91 2
        throw new AssertionFailedException('An untagged response is never tagged');
92
    }
93
94
    /**
95
     * @param string $className
96
     * @return ResponseInterface
97
     * @throws AssertionFailedException
98
     */
99
    public function assertParsed(string $className): ResponseInterface
100
    {
101
        if ($className !== self::class) {
102
            throw new AssertionFailedException(self::class . ' is not parsed as a ' . $className);
103
        }
104
105
        return $this;
106
    }
107
108
    /**
109
     * @return string
110
     */
111 4
    public function getBody(): string
112
    {
113 4
        return \sprintf(
114 4
            '%s FETCH %s',
115 4
            $this->number,
116 4
            (string)$this->itemList
117
        );
118
    }
119
120
    /**
121
     * @return int
122
     */
123 1
    public function getNumber(): int
124
    {
125 1
        return $this->number;
126
    }
127
128
    /**
129
     * @return ItemList
130
     */
131 1
    public function getItemList(): ItemList
132
    {
133 1
        return $this->itemList;
134
    }
135
}
136