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

AbstractAnnotationReadable   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 117
Duplicated Lines 19.66 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 26
lcom 1
cbo 4
dl 23
loc 117
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
C parse() 23 35 13
B dump() 0 28 6
A setReader() 0 6 1
A createResult() 0 6 1
couldTryNextLine() 0 1 ?
getClass() 0 1 ?
A getRefProperties() 0 9 2
A moveToNextLine() 0 6 1
A ensureResult() 0 10 2

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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