LineIterator   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 1
dl 0
loc 39
ccs 18
cts 18
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B newIterator() 0 24 6
1
<?php
2
declare(strict_types=1);
3
4
namespace Genkgo\Mail\Stream;
5
6
use Genkgo\Mail\StreamInterface;
7
8
final class LineIterator extends \IteratorIterator
9
{
10
    /**
11
     * @param StreamInterface $stream
12
     */
13 44
    public function __construct(StreamInterface $stream)
14
    {
15 44
        parent::__construct($this->newIterator($stream));
16 44
    }
17
18
    /**
19
     * @param StreamInterface $stream
20
     * @return \Generator<string>
0 ignored issues
show
Documentation introduced by
The doc-type \Generator<string> could not be parsed: Expected "|" or "end of type", but got "<" at position 10. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
21
     */
22 44
    private function newIterator(StreamInterface $stream): \Generator
23
    {
24 44
        $stream->rewind();
25
26 44
        $bytes = '';
27 44
        while (!$stream->eof()) {
28 44
            $bytes .= $stream->read(1000);
29
30 44
            $index = 0;
31 44
            while (isset($bytes[$index])) {
32 44
                if ($bytes[$index] === "\r" && isset($bytes[$index + 1]) && $bytes[$index + 1] === "\n") {
33 23
                    $line = \substr($bytes, 0, $index);
34 23
                    $bytes = \substr($bytes, $index + 2);
35 23
                    $index = -1;
36
37 23
                    yield $line;
38
                }
39
40 44
                $index++;
41
            }
42
        }
43
44 29
        yield $bytes;
45 22
    }
46
}
47