Completed
Pull Request — develop (#27)
by Chris
15:41 queued 43s
created

AbstractAnnotationReadable::parse()   C

Complexity

Conditions 11
Paths 8

Size

Total Lines 31
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 31
c 0
b 0
f 0
rs 5.2653
cc 11
eloc 19
nc 8
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
10
abstract class AbstractAnnotationReadable extends AbstractM3u8
11
{
12
    private $refProperties;
13
14
    public function parse()
15
    {
16
        do {
17
            $lineInfo = $this->getStream()->read();
18
            if (null === $lineInfo) {
19
                continue;
20
            }
21
22
            foreach ($this->getRefProperties() as $refProp) {
23
                $m3u8 = $this->getAnnotationReader()->getPropertyAnnotation($refProp, M3u8Interface::class);
24
                if (null === $m3u8) {
25
                    continue;
26
                }
27
28
                $value = null === $this->getResult() ? null : $refProp->getValue($this->getResult());
29
                if (null !== $value && !is_array($value)) {
30
                    continue;
31
                }
32
33
                $parsed = $m3u8->setStream($this->getStream())->parse()->getResult();
34
                if (null !== $parsed) {
35
                    is_array($value) ? $value[] = $parsed : $value = $parsed;
36
                    $refProp->setValue($this->ensureResult(), $value);
37
38
                    break;
39
                }
40
            }
41
        } while ($this->couldTryNextLine() && $this->moveToNextLine());
42
43
        return $this;
44
    }
45
46
    public function dump()
47
    {
48
        $refProps = $this->getRefProperties();
49
        $dumpers = [];
50
        foreach ($refProps as $refProp) {
51
            $m3u8 = $this->getAnnotationReader()->getPropertyAnnotation($refProp, M3u8Interface::class);
52
            if (null === $m3u8) {
53
                continue;
54
            }
55
56
            $dumpers[$m3u8->getSequence()] = compact('m3u8', 'refProp');
57
        }
58
59
        ksort($dumpers);
60
61
        foreach ($dumpers as $dumper) {
62
            $value = $dumper['refProp']->getValue($this->getResult());
63
            if (null === $value) {
64
                continue;
65
            }
66
67
            $dumper['m3u8']->setStream($this->getStream());
68
69
            !is_array($value) ? $dumper['m3u8']->setResult($value)->dump() : array_walk($value, function($val) use ($dumper) {
70
                $dumper['m3u8']->setResult($val)->dump();
71
            });
72
        }
73
    }
74
75
    abstract protected function couldTryNextLine();
76
77
    abstract protected function getAnnotationReader();
78
79
    abstract protected function getClass();
80
81
    abstract protected function createResult();
82
83
    private function getRefProperties()
84
    {
85
        if (null === $this->refProperties) {
86
            $refClass = new \ReflectionClass($this->getClass());
87
            $this->refProperties = $refClass->getProperties();
88
        }
89
90
        return $this->refProperties;
91
    }
92
93
    private function moveToNextLine()
94
    {
95
        $this->getStream()->next();
96
97
        return $this->getStream()->valid();
98
    }
99
100
    private function ensureResult()
101
    {
102
        $result = $this->getResult();
103
        if (null === $result) {
104
            $result = $this->createResult();
105
            $this->setResult($result);
106
        }
107
108
        return $result;
109
    }
110
}
111