Test Failed
Push — master ( 3a3652...184b95 )
by 世昌
02:06
created

ReadLineIterator   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 24
dl 0
loc 47
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getIterator() 0 22 6
A __construct() 0 5 1
1
<?php
2
namespace nebula\route\collection;
3
4
use Iterator;
5
use IteratorAggregate;
6
7
/**
8
 * 逐行读文件迭代器
9
 *
10
 */
11
class ReadLineIterator implements IteratorAggregate
12
{
13
    protected $path;
14
    protected $bufferSize;
15
    protected $offset;
16
17
    /**
18
     * 构建迭代器
19
     *
20
     * @param string $path 路径
21
     * @param integer|null $offset 起始偏移
22
     * @param integer $bufferSize
23
     */
24
    public function __construct(string $path, ?int $offset = 0, int $bufferSize = 1024)
25
    {
26
        $this->path = $path;
27
        $this->offset = $offset;
28
        $this->bufferSize = $bufferSize;
29
    }
30
31
    /**
32
     * 获取迭代器
33
     *
34
     * @return Iterator
35
     */
36
    public function getIterator():Iterator
37
    {
38
        if (file_exists($this->path)) {
39
            $fp = fopen($this->path, 'rb');
40
            $offset = 0;
41
            while (!feof($fp)) {
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of feof() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

41
            while (!feof(/** @scrutinizer ignore-type */ $fp)) {
Loading history...
42
                fseek($fp, $offset);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fseek() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
                fseek(/** @scrutinizer ignore-type */ $fp, $offset);
Loading history...
43
                $content = fread($fp, $this->bufferSize);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fread() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

43
                $content = fread(/** @scrutinizer ignore-type */ $fp, $this->bufferSize);
Loading history...
44
                $content.=(feof($fp))? "\n":'';
45
                $size = strpos($content, "\n");
46
                $offset += $size;
47
                if ($content[$size - 1] === "\r") {
48
                    $content = substr($content, 0, $size -1);
49
                }
50
                if (is_null($this->offset)) {
51
                    yield $content;
52
                } else {
53
                    yield $offset => $content;
54
                }
55
            }
56
        } else {
57
            throw new \Exception(sprintf('readline iterator: file note exists %s', $this->path));
58
        }
59
    }
60
}
61