Completed
Pull Request — develop (#27)
by Chris
12:14
created

AbstractParentCore::parse()   D

Complexity

Conditions 10
Paths 10

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
rs 4.8196
cc 10
eloc 15
nc 10
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 || null !== $result->$property && !is_array($result->$property)) {
15
                    continue;
16
                }
17
18
                $childResult = $parser->setLines($this->getLines())->parse();
19
                if (null === $childResult) {
20
                    continue;
21
                }
22
23
                is_array($result->$property) ? $result->$property[] = $childResult : $result->$property = $childResult;
24
                $parsedSequence = $parser->getSequence();
25
26
                break;
27
            }
28
        } while ($this->shouldParseNextLine($result, $parsedSequence) && $this->moveToNextLine());
29
30
        return $parsedSequence > -1 ? $result : null;
31
    }
32
33
    public function dump($result)
34
    {
35
        $children = $this->getChildren();
36
        uasort($children, function (ChildCoreInterface $core, ChildCoreInterface $core2) {
37
            return $core->getSequence() > $core2->getSequence();
38
        });
39
40
        foreach ($children as $property => $dumper) {
41
            $childResult = $result->$property;
42
            if (null === $childResult) {
43
                continue;
44
            }
45
46
            $dumper->setLines($this->getLines());
47
48
            is_array($childResult) ? array_walk($childResult, function ($val) use ($dumper) {
49
                $dumper->dump($val);
50
            }) : $dumper->dump($childResult);
51
        }
52
    }
53
54
    abstract protected function initResult();
55
56
    abstract protected function shouldParseNextLine($result, $parsedSequence);
57
58
    abstract protected function getChildren();
59
60
    private function moveToNextLine()
61
    {
62
        $this->getLines()->goNext();
63
64
        return $this->getLines()->isValid();
65
    }
66
}
67