Completed
Pull Request — develop (#27)
by Chris
11:06
created

AbstractAnnotationReadable::parse()   C

Complexity

Conditions 13
Paths 14

Size

Total Lines 35
Code Lines 21

Duplication

Lines 23
Ratio 65.71 %

Importance

Changes 0
Metric Value
dl 23
loc 35
rs 5.1234
c 0
b 0
f 0
cc 13
eloc 21
nc 14
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Chrisyue\PhpM3u8\M3u8\AnnotationReadable;
4
5
use Chrisyue\PhpM3u8\Stream\StreamInterface;
6
use Chrisyue\PhpM3u8\M3u8\AbstractM3u8;
7
use Chrisyue\PhpM3u8\M3u8\M3u8Interface;
8
use Chrisyue\PhpM3u8\M3u8\SequenceAwareInterface;
9
use Doctrine\Common\Annotations\Reader;
10
use Chrisyue\PhpM3u8\ReaderAwareInterface;
11
12
abstract class AbstractAnnotationReadable extends AbstractM3u8 implements ReaderAwareInterface
13
{
14
    private $refProperties;
15
16
    private $reader;
17
18
    public function parse()
19
    {
20
        do {
21
            $lineInfo = $this->getStream()->read();
22
            if (empty($lineInfo['tag']) && empty($lineInfo['value'])) {
23
                continue;
24
            }
25
26 View Code Duplication
            foreach ($this->getRefProperties() as $refProp) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across 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...
27
                $m3u8 = $this->reader->getPropertyAnnotation($refProp, M3u8Interface::class);
28
                if (null === $m3u8) {
29
                    continue;
30
                }
31
32
                $value = null === $this->getResult() ? null : $refProp->getValue($this->getResult());
33
                if (null !== $value && !is_array($value)) {
34
                    continue;
35
                }
36
37
                if ($m3u8 instanceof ReaderAwareInterface) {
38
                    $m3u8->setReader($this->reader);
39
                }
40
41
                $parsed = $m3u8->setStream($this->getStream())->parse()->getResult();
42
                if (null !== $parsed) {
43
                    is_array($value) ? $value[] = $parsed : $value = $parsed;
44
                    $refProp->setValue($this->ensureResult(), $value);
45
46
                    break;
47
                }
48
            }
49
        } while ($this->couldTryNextLine() && $this->moveToNextLine());
50
51
        return $this;
52
    }
53
54
    public function dump()
55
    {
56
        $refProps = $this->getRefProperties();
57
        $dumpers = [];
58
        foreach ($refProps as $refProp) {
59
            $m3u8 = $this->reader->getPropertyAnnotation($refProp, M3u8Interface::class);
60
            if (null === $m3u8) {
61
                continue;
62
            }
63
64
            $dumpers[$m3u8->getSequence()] = compact('m3u8', 'refProp');
65
        }
66
67
        ksort($dumpers);
68
69
        foreach ($dumpers as $dumper) {
70
            $value = $dumper['refProp']->getValue($this->getResult());
71
            if (null === $value) {
72
                continue;
73
            }
74
75
            $dumper['m3u8']->setStream($this->getStream());
76
77
            !is_array($value) ? $dumper['m3u8']->setResult($value)->dump() : array_walk($value, function($val) use ($dumper) {
78
                $dumper['m3u8']->setResult($val)->dump();
79
            });
80
        }
81
    }
82
83
    public function setReader(Reader $reader)
84
    {
85
        $this->reader = $reader;
86
87
        return $this;
88
    }
89
90
    protected function createResult()
91
    {
92
        $class = $this->getClass();
93
94
        return new $class;
95
    }
96
97
    abstract protected function couldTryNextLine();
98
99
    abstract protected function getClass();
100
101
    private function getRefProperties()
102
    {
103
        if (null === $this->refProperties) {
104
            $refClass = new \ReflectionClass($this->getClass());
105
            $this->refProperties = $refClass->getProperties();
106
        }
107
108
        return $this->refProperties;
109
    }
110
111
    private function moveToNextLine()
112
    {
113
        $this->getStream()->next();
114
115
        return $this->getStream()->valid();
116
    }
117
118
    private function ensureResult()
119
    {
120
        $result = $this->getResult();
121
        if (null === $result) {
122
            $result = $this->createResult();
123
            $this->setResult($result);
124
        }
125
126
        return $result;
127
    }
128
}
129