1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the league/commonmark package. |
5
|
|
|
* |
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
7
|
|
|
* (c) Rezo Zero / Ambroise Maupate |
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\Footnote\Parser; |
16
|
|
|
|
17
|
|
|
use League\CommonMark\Extension\Footnote\Node\FootnoteRef; |
18
|
|
|
use League\CommonMark\Normalizer\SlugNormalizer; |
19
|
|
|
use League\CommonMark\Normalizer\TextNormalizerInterface; |
20
|
|
|
use League\CommonMark\Parser\Inline\InlineParserInterface; |
21
|
|
|
use League\CommonMark\Parser\InlineParserContext; |
22
|
|
|
use League\CommonMark\Reference\Reference; |
23
|
|
|
|
24
|
|
|
final class AnonymousFootnoteRefParser implements InlineParserInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
42 |
|
* @var TextNormalizerInterface |
28
|
|
|
* |
29
|
42 |
|
* @psalm-readonly |
30
|
|
|
*/ |
31
|
|
|
private $slugNormalizer; |
32
|
18 |
|
|
33
|
|
|
public function __construct() |
34
|
18 |
|
{ |
35
|
18 |
|
$this->slugNormalizer = new SlugNormalizer(); |
36
|
18 |
|
} |
37
|
18 |
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritDoc} |
40
|
|
|
*/ |
41
|
18 |
|
public function getCharacters(): array |
42
|
|
|
{ |
43
|
18 |
|
return ['^']; |
44
|
18 |
|
} |
45
|
18 |
|
|
46
|
18 |
|
public function parse(InlineParserContext $inlineContext): bool |
47
|
18 |
|
{ |
48
|
|
|
$container = $inlineContext->getContainer(); |
49
|
18 |
|
$cursor = $inlineContext->getCursor(); |
50
|
|
|
$nextChar = $cursor->peek(); |
51
|
|
|
if ($nextChar !== '[') { |
52
|
|
|
return false; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$state = $cursor->saveState(); |
56
|
|
|
|
57
|
|
|
$m = $cursor->match('/\^\[[^\n^\]]+\]/'); |
58
|
18 |
|
if ($m !== null) { |
59
|
|
|
if (\preg_match('#\^\[([^\]]+)\]#', $m, $matches) > 0) { |
60
|
18 |
|
$reference = $this->createReference($matches[1]); |
61
|
18 |
|
$container->appendChild(new FootnoteRef($reference, $matches[1])); |
62
|
18 |
|
|
63
|
|
|
return true; |
64
|
18 |
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$cursor->restoreState($state); |
|
|
|
|
68
|
|
|
|
69
|
|
|
return false; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @psalm-pure |
74
|
|
|
*/ |
75
|
|
|
private function createReference(string $label): Reference |
76
|
|
|
{ |
77
|
|
|
$refLabel = $this->slugNormalizer->normalize($label); |
78
|
|
|
$refLabel = \mb_substr($refLabel, 0, 20); |
79
|
|
|
|
80
|
|
|
return new Reference($refLabel, '#fn:' . $refLabel, $label); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
PHP Analyzer performs a side-effects analysis of your code. A side-effect is basically anything that might be visible after the scope of the method is left.
Let’s take a look at an example:
If we look at the
getEmail()
method, we can see that it has no side-effect. Whether you call this method or not, no future calls to other methods are affected by this. As such code as the following is useless:On the hand, if we look at the
setEmail()
, this method _has_ side-effects. In the following case, we could not remove the method call: