1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the league/commonmark package. |
5
|
|
|
* |
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
7
|
|
|
* (c) 2015 Martin Hasoň <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace League\CommonMark\Extension\Attributes\Event; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Block\Element\AbstractBlock; |
18
|
|
|
use League\CommonMark\Block\Element\FencedCode; |
19
|
|
|
use League\CommonMark\Block\Element\ListBlock; |
20
|
|
|
use League\CommonMark\Block\Element\ListItem; |
21
|
|
|
use League\CommonMark\Event\DocumentParsedEvent; |
22
|
|
|
use League\CommonMark\Extension\Attributes\Node\Attributes; |
23
|
|
|
use League\CommonMark\Extension\Attributes\Node\AttributesInline; |
24
|
|
|
use League\CommonMark\Extension\Attributes\Util\AttributesHelper; |
25
|
|
|
use League\CommonMark\Inline\Element\AbstractInline; |
26
|
|
|
use League\CommonMark\Node\Node; |
27
|
|
|
|
28
|
|
|
final class AttributesListener |
29
|
|
|
{ |
30
|
|
|
private const DIRECTION_PREFIX = 'prefix'; |
31
|
|
|
private const DIRECTION_SUFFIX = 'suffix'; |
32
|
|
|
|
33
|
18 |
|
public function processDocument(DocumentParsedEvent $event): void |
34
|
|
|
{ |
35
|
18 |
|
$walker = $event->getDocument()->walker(); |
36
|
18 |
|
while ($event = $walker->next()) { |
37
|
18 |
|
$node = $event->getNode(); |
38
|
18 |
|
if (!$node instanceof AttributesInline && ($event->isEntering() || !$node instanceof Attributes)) { |
39
|
18 |
|
continue; |
40
|
|
|
} |
41
|
|
|
|
42
|
18 |
|
[$target, $direction] = self::findTargetAndDirection($node); |
|
|
|
|
43
|
|
|
|
44
|
18 |
|
if ($target instanceof AbstractBlock || $target instanceof AbstractInline) { |
45
|
18 |
|
$parent = $target->parent(); |
46
|
18 |
|
if ($parent instanceof ListItem && $parent->parent() instanceof ListBlock && $parent->parent()->isTight()) { |
47
|
3 |
|
$target = $parent; |
48
|
|
|
} |
49
|
|
|
|
50
|
18 |
|
if ($direction === self::DIRECTION_SUFFIX) { |
51
|
18 |
|
$attributes = AttributesHelper::mergeAttributes($target, $node->getAttributes()); |
52
|
|
|
} else { |
53
|
9 |
|
$attributes = AttributesHelper::mergeAttributes($node->getAttributes(), $target); |
54
|
|
|
} |
55
|
|
|
|
56
|
18 |
|
$target->data['attributes'] = $attributes; |
57
|
|
|
} |
58
|
|
|
|
59
|
18 |
|
if ($node instanceof AbstractBlock && $node->endsWithBlankLine() && $node->next() && $node->previous()) { |
60
|
9 |
|
$previous = $node->previous(); |
61
|
9 |
|
if ($previous instanceof AbstractBlock) { |
62
|
9 |
|
$previous->setLastLineBlank(true); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
18 |
|
$node->detach(); |
67
|
|
|
} |
68
|
18 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param Node $node |
72
|
|
|
* |
73
|
|
|
* @return array<Node|string|null> |
74
|
|
|
*/ |
75
|
18 |
|
private static function findTargetAndDirection(Node $node): array |
76
|
|
|
{ |
77
|
18 |
|
$target = null; |
78
|
18 |
|
$direction = null; |
79
|
18 |
|
$previous = $next = $node; |
80
|
18 |
|
while (true) { |
81
|
18 |
|
$previous = self::getPrevious($previous); |
82
|
18 |
|
$next = self::getNext($next); |
83
|
|
|
|
84
|
18 |
|
if ($previous === null && $next === null) { |
85
|
15 |
|
if (!$node->parent() instanceof FencedCode) { |
86
|
12 |
|
$target = $node->parent(); |
87
|
12 |
|
$direction = self::DIRECTION_SUFFIX; |
88
|
|
|
} |
89
|
|
|
|
90
|
15 |
|
break; |
91
|
|
|
} |
92
|
|
|
|
93
|
18 |
|
if ($node instanceof AttributesInline && ($previous === null || ($previous instanceof AbstractInline && $node->isBlock()))) { |
94
|
15 |
|
continue; |
95
|
|
|
} |
96
|
|
|
|
97
|
18 |
|
if ($previous !== null && !self::isAttributesNode($previous)) { |
98
|
18 |
|
$target = $previous; |
99
|
18 |
|
$direction = self::DIRECTION_SUFFIX; |
100
|
|
|
|
101
|
18 |
|
break; |
102
|
|
|
} |
103
|
|
|
|
104
|
9 |
|
if ($next !== null && !self::isAttributesNode($next)) { |
105
|
9 |
|
$target = $next; |
106
|
9 |
|
$direction = self::DIRECTION_PREFIX; |
107
|
|
|
|
108
|
9 |
|
break; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
18 |
|
return [$target, $direction]; |
113
|
|
|
} |
114
|
|
|
|
115
|
18 |
|
private static function getPrevious(?Node $node = null): ?Node |
116
|
|
|
{ |
117
|
18 |
|
$previous = $node instanceof Node ? $node->previous() : null; |
118
|
|
|
|
119
|
18 |
|
if ($previous instanceof AbstractBlock && $previous->endsWithBlankLine()) { |
120
|
6 |
|
$previous = null; |
121
|
|
|
} |
122
|
|
|
|
123
|
18 |
|
return $previous; |
124
|
|
|
} |
125
|
|
|
|
126
|
18 |
|
private static function getNext(?Node $node = null): ?Node |
127
|
|
|
{ |
128
|
18 |
|
$next = $node instanceof Node ? $node->next() : null; |
129
|
|
|
|
130
|
18 |
|
if ($node instanceof AbstractBlock && $node->endsWithBlankLine()) { |
131
|
9 |
|
$next = null; |
132
|
|
|
} |
133
|
|
|
|
134
|
18 |
|
return $next; |
135
|
|
|
} |
136
|
|
|
|
137
|
18 |
|
private static function isAttributesNode(Node $node): bool |
138
|
|
|
{ |
139
|
18 |
|
return $node instanceof Attributes || $node instanceof AttributesInline; |
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: