LineStream   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Test Coverage

Coverage 80%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 34
ccs 8
cts 10
cp 0.8
rs 10
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A getIterator() 0 12 4
1
<?php declare(strict_types=1);
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2018, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/http-message/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/http-message
10
 */
11
12
namespace Sunrise\Http\Message\Stream;
13
14
use IteratorAggregate;
15
use Sunrise\Http\Message\Exception\InvalidArgumentException;
16
use Sunrise\Http\Message\Exception\RuntimeException;
17
use Sunrise\Http\Message\Stream;
18
use Traversable;
19
20
use function fgets;
21
use function is_resource;
22
use function rtrim;
23
24
/**
25
 * @implements IteratorAggregate<int, string>
26
 *
27
 * @since 3.6.0
28
 */
29
final class LineStream extends Stream implements IteratorAggregate
30
{
31
    /**
32
     * @var mixed
33
     */
34
    private $resource;
35
36
    /**
37
     * @param mixed $resource
38
     *
39
     * @throws InvalidArgumentException
40
     */
41 1
    public function __construct($resource, bool $autoClose = true)
42
    {
43 1
        parent::__construct($resource, $autoClose);
44
45 1
        $this->resource = $resource;
46
    }
47
48
    /**
49
     * @inheritDoc
50
     */
51 1
    public function getIterator(): Traversable
52
    {
53 1
        if (!is_resource($this->resource)) {
54
            throw new RuntimeException('Stream has no resource');
55
        }
56
57 1
        if (!$this->isReadable()) {
58
            throw new RuntimeException('Stream is not readable');
59
        }
60
61 1
        while (($line = fgets($this->resource)) !== false) {
62 1
            yield rtrim($line);
63
        }
64
    }
65
}
66