Completed
Pull Request — develop (#27)
by Chris
10:40
created

AbstractStream::getLineInfo()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 19
Ratio 100 %

Importance

Changes 0
Metric Value
dl 19
loc 19
rs 9.2
c 0
b 0
f 0
cc 4
eloc 10
nc 4
nop 1
1
<?php
2
3
namespace Chrisyue\PhpM3u8\Stream;
4
5
abstract class AbstractStream implements StreamInterface
6
{
7
    public function read()
8
    {
9
        static $line;
10
        static $lineInfo;
11
        if ($this->getLine() !== $line) {
12
            $line = $this->getLine();
13
            $lineInfo = $this->getLineInfo($line);
14
        }
15
16
        return $lineInfo;
17
    }
18
19
    public function write(array $lineInfo)
20
    {
21
        if (empty($lineInfo['tag'])) {
22
            $this->putLine($lineInfo['value']);
23
24
            return;
25
        }
26
27
        if (empty($lineInfo['value'])) {
28
            $this->putLine($lineInfo['tag']);
29
30
            return;
31
        }
32
33
        $this->putLine(sprintf('%s:%s', $lineInfo['tag'], $lineInfo['value']));
34
    }
35
36
    abstract protected function getLine();
37
38
    abstract protected function putLine($line);
39
40 View Code Duplication
    private function getLineInfo($line)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
41
    {
42
        $line = trim($line);
43
        if (empty($line)) {
44
            return;
45
        }
46
47
        if ('#' !== $line[0]) {
48
            return ['value' => $line];
49
        }
50
51
        if (strlen($line) < 2) {
52
            return;
53
        }
54
55
        list($tag, $value) = array_pad(explode(':', $line, 2), 2, '');
56
57
        return compact('tag', 'value');
58
    }
59
}
60