Completed
Pull Request — develop (#27)
by Chris
04:43
created

AbstractParentCore::parse()   D

Complexity

Conditions 10
Paths 12

Size

Total Lines 34
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 34
rs 4.8196
c 0
b 0
f 0
cc 10
eloc 21
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
    /**
8
     * @var Chrisyue\PhpM3u8\M3u8\Builder\BuilderInterface
9
     */
10
    public $builder;
11
12
    public function parse()
13
    {
14
        $parsedSequence = -1;
15
        $result = $this->builder->setResult($this->initResult());
16
17
        $ret = null;
0 ignored issues
show
Unused Code introduced by
$ret is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
18
        do {
19
            foreach ($this->getChildren() as $property => $parser) {
20
                if ($parser->getSequence() < $parsedSequence) {
21
                    continue;
22
                }
23
24
                $oldChildValue = $this->builder->getValue($property);
25
                $isChildValueArray = is_array($oldChildValue);
26
                if (null !== $oldChildValue && !$isChildValueArray) {
27
                    continue;
28
                }
29
30
                $childValue = $parser->setLines($this->getLines())->parse();
31
                if (null === $childValue) {
32
                    continue;
33
                }
34
35
                $isChildValueArray ? $this->builder->addValue($property, $childValue) : $this->builder->setValue($property, $childValue);
36
                $parsedSequence = $parser->getSequence();
37
38
                break;
39
            }
40
41
            $ret = $parsedSequence > -1 ? $result : null;
42
        } while ($this->shouldParseNextLine($ret) && $this->moveToNextLine());
43
44
        return $ret;
45
    }
46
47
    public function dump($result)
48
    {
49
        $children = $this->getChildren();
50
        uasort($children, function (ChildCoreInterface $core, ChildCoreInterface $core2) {
51
            return $core->getSequence() > $core2->getSequence();
52
        });
53
54
        foreach ($children as $property => $dumper) {
55
            $childResult = $this->getChildValue($result, $property);
0 ignored issues
show
Bug introduced by
The method getChildValue() does not seem to exist on object<Chrisyue\PhpM3u8\...ore\AbstractParentCore>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
56
            if (null === $childResult) {
57
                continue;
58
            }
59
60
            $dumper->setLines($this->getLines());
61
62
            is_array($childResult) ? array_walk($childResult, function ($val) use ($dumper) {
63
                $dumper->dump($val);
64
            }) : $dumper->dump($childResult);
65
        }
66
    }
67
68
    abstract protected function initResult();
69
70
    abstract protected function shouldParseNextLine($result);
71
72
    abstract protected function getChildren();
73
74
    private function moveToNextLine()
75
    {
76
        $this->getLines()->goNext();
77
78
        return $this->getLines()->isValid();
79
    }
80
}
81