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

AbstractParentCore::dump()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 21
rs 9.0534
c 0
b 0
f 0
cc 4
eloc 13
nc 4
nop 1
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
        $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 ? $this->builder->getResult() : 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
        $this->builder->setResult($result);
55
        foreach ($children as $property => $dumper) {
56
            $childResult = $this->builder->getValue($result, $property);
57
            if (null === $childResult) {
58
                continue;
59
            }
60
61
            $dumper->setLines($this->getLines());
62
63
            is_array($childResult) ? array_walk($childResult, function ($val) use ($dumper) {
64
                $dumper->dump($val);
65
            }) : $dumper->dump($childResult);
66
        }
67
    }
68
69
    abstract protected function initResult();
70
71
    abstract protected function shouldParseNextLine($result);
72
73
    abstract protected function getChildren();
74
75
    private function moveToNextLine()
76
    {
77
        $this->getLines()->goNext();
78
79
        return $this->getLines()->isValid();
80
    }
81
}
82