Skip to content

Commit ecd40fd

Browse files
authored
Merge pull request #546 from phpDocumentor/bugfix/code-with_underscore
[BUGFIX] Make underscores work as first sign in a code role
2 parents f26fd3f + 7c3ac5d commit ecd40fd

File tree

12 files changed

+70
-40
lines changed

12 files changed

+70
-40
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ fix-code-style:
1515
.PHONY: static-code-analysis
1616
static-code-analysis: vendor phpstan psalm test-architecture ## Runs a static code analysis with phpstan/phpstan and vimeo/psalm
1717

18+
.PHONY: phpstan-baseline
19+
phpstan-baseline:
20+
$(PHP_BIN) -d memory_limit=1024M vendor/bin/phpstan --configuration=phpstan.neon --generate-baseline
21+
1822
.PHONY: phpstan
1923
phpstan:
2024
$(PHP_BIN) -d memory_limit=1024M vendor/bin/phpstan --configuration=phpstan.neon

packages/guides-restructured-text/src/RestructuredText/Parser/InlineLexer.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,8 @@ final class InlineLexer extends AbstractLexer
2121
public const WORD = 1;
2222
public const UNDERSCORE = 2;
2323
public const ANONYMOUS_END = 3;
24-
public const PHRASE_ANONYMOUS_END = 4;
2524
public const LITERAL = 5;
2625
public const BACKTICK = 6;
27-
public const NAMED_REFERENCE_END = 7;
28-
public const INTERNAL_REFERENCE_START = 8;
2926
public const EMBEDED_URL_START = 9;
3027
public const EMBEDED_URL_END = 10;
3128
public const NAMED_REFERENCE = 11;
@@ -63,14 +60,11 @@ protected function getCatchablePatterns(): array
6360
'[a-z0-9-]+_{2}', //Inline href.
6461
'[a-z0-9-]+_{1}(?=[\s\.+]|$)', //Inline href.
6562
'``.+?``(?!`)',
66-
'`__',
67-
'`_',
68-
'`~',
63+
'_{2}',
64+
'_',
6965
'<',
7066
'>',
71-
'_`',
7267
'`',
73-
'_{2}',
7468
':',
7569
'|',
7670
'\\*\\*',
@@ -160,18 +154,9 @@ protected function getType(string &$value)
160154
case '_':
161155
return self::UNDERSCORE;
162156

163-
case '`_':
164-
return self::NAMED_REFERENCE_END;
165-
166-
case '_`':
167-
return self::INTERNAL_REFERENCE_START;
168-
169157
case '__':
170158
return self::ANONYMOUS_END;
171159

172-
case '`__':
173-
return self::PHRASE_ANONYMOUS_END;
174-
175160
case ':':
176161
return self::COLON;
177162

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/AnonymousPhraseRule.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ public function apply(BlockContext $blockContext, InlineLexer $lexer): HyperLink
3333
$lexer->moveNext();
3434
while ($lexer->token !== null) {
3535
switch ($lexer->token->type) {
36-
case InlineLexer::PHRASE_ANONYMOUS_END:
36+
case InlineLexer::BACKTICK:
37+
$lexer->moveNext();
38+
if ($lexer->token?->type !== InlineLexer::ANONYMOUS_END) {
39+
$this->rollback($lexer, $initialPosition ?? 0);
40+
41+
return null;
42+
}
43+
3744
$lexer->moveNext();
3845

3946
return $this->createAnonymousReference($blockContext, $text, $embeddedUrl);

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/InternalReferenceRule.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,20 @@ class InternalReferenceRule extends ReferenceRule
1212
{
1313
public function applies(InlineLexer $lexer): bool
1414
{
15-
return $lexer->token?->type === InlineLexer::INTERNAL_REFERENCE_START;
15+
return $lexer->token?->type === InlineLexer::UNDERSCORE;
1616
}
1717

1818
public function apply(BlockContext $blockContext, InlineLexer $lexer): InlineNode|null
1919
{
2020
$text = '';
2121
$initialPosition = $lexer->token?->position;
22+
$lexer->moveNext();
23+
if ($lexer->token?->type !== InlineLexer::BACKTICK) {
24+
$this->rollback($lexer, $initialPosition ?? 0);
25+
26+
return null;
27+
}
28+
2229
$lexer->moveNext();
2330
while ($lexer->token !== null) {
2431
switch ($lexer->token->type) {

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/NamedPhraseRule.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ public function apply(BlockContext $blockContext, InlineLexer $lexer): InlineNod
3333
$lexer->moveNext();
3434
while ($lexer->token !== null) {
3535
switch ($lexer->token->type) {
36-
case InlineLexer::NAMED_REFERENCE_END:
36+
case InlineLexer::BACKTICK:
37+
$lexer->moveNext();
38+
if ($lexer->token?->type !== InlineLexer::UNDERSCORE) {
39+
$this->rollback($lexer, $initialPosition ?? 0);
40+
41+
return null;
42+
}
43+
3744
$lexer->moveNext();
3845
if ($text === '') {
3946
$text = $embeddedUrl ?? '';

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/ReferenceRule.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function parseEmbeddedUrl(InlineLexer $lexer): string|null
4040
while ($lexer->moveNext()) {
4141
$token = $lexer->token;
4242
switch ($token->type) {
43-
case InlineLexer::NAMED_REFERENCE_END:
43+
case InlineLexer::BACKTICK:
4444
//We did not find the expected SpanLexer::EMBEDED_URL_END
4545
$this->rollback($lexer, $startPosition);
4646

phpstan-baseline.neon

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
parameters:
2+
ignoreErrors:
3+
-
4+
message: "#^Using nullsafe property access on non\\-nullable type Doctrine\\\\Common\\\\Lexer\\\\Token\\<int, string\\>\\. Use \\-\\> instead\\.$#"
5+
count: 1
6+
path: packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/AnonymousPhraseRule.php
7+
8+
-
9+
message: "#^Unreachable statement \\- code above always terminates\\.$#"
10+
count: 1
11+
path: packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/InternalReferenceRule.php
12+
13+
-
14+
message: "#^Using nullsafe property access on non\\-nullable type Doctrine\\\\Common\\\\Lexer\\\\Token\\<int, string\\>\\. Use \\-\\> instead\\.$#"
15+
count: 1
16+
path: packages/guides-restructured-text/src/RestructuredText/Parser/Productions/InlineRules/NamedPhraseRule.php

phpstan.neon

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
includes:
2+
- phpstan-baseline.neon
13
parameters:
24
level: max
35
inferPrivatePropertyTypeFromConstructor: true

tests/Integration/tests/code-with underscore/expected/index.html

Lines changed: 0 additions & 18 deletions
This file was deleted.

tests/Integration/tests/code-with underscore/input/skip

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<title>Title</title>
5+
6+
</head>
7+
<body>
8+
<div class="section" id="title">
9+
<h1>Title</h1>
10+
11+
<p>Folder <code>_assets</code> contains symlinks to <code>Resources/Public</code></p>
12+
<p>Folder <code>_assets</code> contains symlinks to <code>Resources/Public</code></p>
13+
<p>Folder <code>\_assets</code> contains symlinks to <code>Resources/Public</code></p>
14+
<p>Folder <code>_assets</code> contains symlinks to <code>Resources/Public</code></p>
15+
<p>Some <code>_.htaccess</code> and <code>_web.config</code>.</p>
16+
</div>
17+
18+
</body>
19+
</html>

tests/Integration/tests/code-with underscore/input/index.rst renamed to tests/Integration/tests/code-with-underscore/input/index.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ Folder :code:`_assets` contains symlinks to :code:`Resources/Public`
88
Folder :code:`\_assets` contains symlinks to :code:`Resources/Public`
99

1010
Folder ``_assets`` contains symlinks to `Resources/Public`
11+
12+
Some `_.htaccess` and `_web.config`.

0 commit comments

Comments
 (0)