Completed
Pull Request — develop (#27)
by Chris
02:13
created

AbstractParentCore::parse()   D

Complexity

Conditions 10
Paths 12

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 4.8196
cc 10
eloc 19
nc 12
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\Core;
4
5
abstract class AbstractParentCore extends AbstractCore
6
{
7
    public function parse()
8
    {
9
        $parsedSequence = -1;
10
        $result = $this->initResult();
11
12
        do {
13
            foreach ($this->getChildren() as $property => $parser) {
14
                if ($parser->getSequence() < $parsedSequence) {
15
                    continue;
16
                }
17
18
                $oldChildValue = $this->getChildValue($result, $property);
19
                $isChildValueArray = is_array($oldChildValue);
20
                if (null !== $oldChildValue && !$isChildValueArray) {
21
                    continue;
22
                }
23
24
                $childValue = $parser->setLines($this->getLines())->parse();
25
                if (null === $childValue) {
26
                    continue;
27
                }
28
29
                $isChildValueArray ? $this->addChildValue($result, $property, $childValue) : $this->setChildValue($result, $property, $childValue);
30
                $parsedSequence = $parser->getSequence();
31
32
                break;
33
            }
34
        } while ($this->shouldParseNextLine($result, $parsedSequence) && $this->moveToNextLine());
35
36
        return $parsedSequence > -1 ? $result : null;
37
    }
38
39
    public function dump($result)
40
    {
41
        $children = $this->getChildren();
42
        uasort($children, function (ChildCoreInterface $core, ChildCoreInterface $core2) {
43
            return $core->getSequence() > $core2->getSequence();
44
        });
45
46
        foreach ($children as $property => $dumper) {
47
            $childResult = $this->getChildValue($result, $property);
48
            if (null === $childResult) {
49
                continue;
50
            }
51
52
            $dumper->setLines($this->getLines());
53
54
            is_array($childResult) ? array_walk($childResult, function ($val) use ($dumper) {
55
                $dumper->dump($val);
56
            }) : $dumper->dump($childResult);
57
        }
58
    }
59
60
    abstract protected function initResult();
61
62
    abstract protected function getChildValue($result, $childKey);
63
64
    abstract protected function addChildValue($result, $childKey, $value);
65
66
    abstract protected function setChildValue($result, $childKey, $value);
67
68
    abstract protected function shouldParseNextLine($result, $parsedSequence);
69
70
    abstract protected function getChildren();
71
72
    private function moveToNextLine()
73
    {
74
        $this->getLines()->goNext();
75
76
        return $this->getLines()->isValid();
77
    }
78
}
79