1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the league/commonmark package. |
7
|
|
|
* |
8
|
|
|
* (c) Colin O'Dell <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace League\CommonMark\Extension\Mention; |
15
|
|
|
|
16
|
|
|
use League\CommonMark\Extension\Mention\Generator\CallbackGenerator; |
17
|
|
|
use League\CommonMark\Extension\Mention\Generator\MentionGeneratorInterface; |
18
|
|
|
use League\CommonMark\Extension\Mention\Generator\StringTemplateLinkGenerator; |
19
|
|
|
use League\CommonMark\Parser\Inline\InlineParserInterface; |
20
|
|
|
use League\CommonMark\Parser\InlineParserContext; |
21
|
|
|
|
22
|
|
|
final class MentionParser implements InlineParserInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var string |
26
|
|
|
* |
27
|
|
|
* @psalm-readonly |
28
|
|
|
*/ |
29
|
|
|
private $symbol; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
* |
34
|
|
|
* @psalm-readonly |
35
|
|
|
*/ |
36
|
|
|
private $mentionRegex; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var MentionGeneratorInterface |
40
|
|
|
* |
41
|
|
|
* @psalm-readonly |
42
|
|
|
*/ |
43
|
|
|
private $mentionGenerator; |
44
|
|
|
|
45
|
30 |
|
public function __construct(string $symbol, string $mentionRegex, MentionGeneratorInterface $mentionGenerator) |
46
|
|
|
{ |
47
|
30 |
|
$this->symbol = $symbol; |
48
|
30 |
|
$this->mentionRegex = $mentionRegex; |
49
|
30 |
|
$this->mentionGenerator = $mentionGenerator; |
50
|
30 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* {@inheritDoc} |
54
|
|
|
*/ |
55
|
30 |
|
public function getCharacters(): array |
56
|
|
|
{ |
57
|
30 |
|
return [$this->symbol]; |
58
|
|
|
} |
59
|
|
|
|
60
|
27 |
|
public function parse(InlineParserContext $inlineContext): bool |
61
|
|
|
{ |
62
|
27 |
|
$cursor = $inlineContext->getCursor(); |
63
|
|
|
|
64
|
|
|
// The symbol must not have any other characters immediately prior |
65
|
27 |
|
$previousChar = $cursor->peek(-1); |
66
|
27 |
|
if ($previousChar !== null && $previousChar !== ' ') { |
67
|
|
|
// peek() doesn't modify the cursor, so no need to restore state first |
68
|
3 |
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
// Save the cursor state in case we need to rewind and bail |
72
|
24 |
|
$previousState = $cursor->saveState(); |
73
|
|
|
|
74
|
|
|
// Advance past the symbol to keep parsing simpler |
75
|
24 |
|
$cursor->advance(); |
76
|
|
|
|
77
|
|
|
// Parse the identifier |
78
|
24 |
|
$identifier = $cursor->match($this->mentionRegex); |
79
|
24 |
|
if ($identifier === null) { |
80
|
|
|
// Regex failed to match; this isn't a valid mention |
81
|
3 |
|
$cursor->restoreState($previousState); |
|
|
|
|
82
|
|
|
|
83
|
3 |
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
21 |
|
$mention = $this->mentionGenerator->generateMention(new Mention($this->symbol, $identifier)); |
87
|
|
|
|
88
|
21 |
|
if ($mention === null) { |
89
|
3 |
|
$cursor->restoreState($previousState); |
|
|
|
|
90
|
|
|
|
91
|
3 |
|
return false; |
92
|
|
|
} |
93
|
|
|
|
94
|
18 |
|
$inlineContext->getContainer()->appendChild($mention); |
95
|
|
|
|
96
|
18 |
|
return true; |
97
|
|
|
} |
98
|
|
|
|
99
|
3 |
|
public static function createWithStringTemplate(string $symbol, string $mentionRegex, string $urlTemplate): MentionParser |
100
|
|
|
{ |
101
|
3 |
|
return new self($symbol, $mentionRegex, new StringTemplateLinkGenerator($urlTemplate)); |
102
|
|
|
} |
103
|
|
|
|
104
|
9 |
|
public static function createWithCallback(string $symbol, string $mentionRegex, callable $callback): MentionParser |
105
|
|
|
{ |
106
|
9 |
|
return new self($symbol, $mentionRegex, new CallbackGenerator($callback)); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
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: