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; |
|
|
|
|
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); |
|
|
|
|
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
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
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.