Skip to content

Commit 813a175

Browse files
authored
Merge pull request #1248 from phpDocumentor/optimizations
Micro optimizations
2 parents 52b0d6b + 4708f1d commit 813a175

File tree

15 files changed

+185
-42
lines changed

15 files changed

+185
-42
lines changed

packages/guides-cli/src/DevServer/RerenderListener.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,9 @@ public function __invoke(FileModifiedEvent $event): void
7070
);
7171
assert($document instanceof DocumentNode);
7272

73-
$documents = $this->documents;
74-
$documents[$file] = $document;
75-
7673
/** @var array<string, DocumentNode> $documents */
77-
$documents = $this->commandBus->handle(new CompileDocumentsCommand($documents, new CompilerContext($this->projectNode)));
78-
$this->documents = $documents;
74+
$documents = $this->commandBus->handle(new CompileDocumentsCommand([$file => $document], new CompilerContext($this->projectNode)));
75+
$this->documents[$file] = $documents[$file];
7976
$destinationFileSystem = FlySystemAdapter::createForPath($this->settings->getOutput());
8077

8178
$documentIterator = DocumentListIterator::create(

packages/guides-restructured-text/src/RestructuredText/Directives/CsvTableDirective.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
use function explode;
3434
use function implode;
3535
use function is_string;
36+
use function method_exists;
3637
use function strval;
3738
use function trim;
3839

@@ -78,10 +79,18 @@ public function processNode(
7879
return new GenericNode('csv-table');
7980
}
8081

81-
$csv = Reader::createFromStream($csvStream);
82+
if (method_exists(Reader::class, 'from')) {
83+
$csv = Reader::from($csvStream);
84+
} else {
85+
$csv = Reader::createFromStream($csvStream);
86+
}
8287
} else {
8388
$lines = $blockContext->getDocumentIterator()->toArray();
84-
$csv = Reader::createFromString(implode("\n", $lines));
89+
if (method_exists(Reader::class, 'fromString')) {
90+
$csv = Reader::fromString(implode("\n", $lines));
91+
} else {
92+
$csv = Reader::createFromString(implode("\n", $lines));
93+
}
8594
}
8695

8796
if ($directive->getOption('header-rows')->getValue() !== null) {
@@ -90,7 +99,12 @@ public function processNode(
9099

91100
$header = null;
92101
if ($directive->hasOption('header')) {
93-
$headerCsv = Reader::createFromString($directive->getOption('header')->toString());
102+
if (method_exists(Reader::class, 'fromString')) {
103+
$headerCsv = Reader::fromString($directive->getOption('header')->toString());
104+
} else {
105+
$headerCsv = Reader::createFromString($directive->getOption('header')->toString());
106+
}
107+
94108
$header = new TableRow();
95109
foreach ($headerCsv->first() as $column) {
96110
$columnNode = new TableColumn($column, 1, []);

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

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
namespace phpDocumentor\Guides\RestructuredText\Parser;
1515

1616
use function array_pop;
17-
use function array_walk;
1817
use function count;
1918
use function implode;
2019
use function ltrim;
@@ -103,21 +102,29 @@ public function clear(): void
103102

104103
public function trimLines(): void
105104
{
106-
array_walk($this->lines, static function (&$value): void {
107-
$value = trim($value);
108-
});
105+
foreach ($this->lines as $i => $line) {
106+
$this->lines[$i] = trim($line);
107+
}
109108
}
110109

111110
private function unIndent(): void
112111
{
112+
if ($this->unindentStrategy === UnindentStrategy::NONE) {
113+
return;
114+
}
115+
113116
$indentation = $this->detectIndentation();
114-
array_walk($this->lines, static function (&$value) use ($indentation): void {
115-
if (strlen($value) < $indentation) {
116-
return;
117+
if ($indentation === 0) {
118+
return;
119+
}
120+
121+
foreach ($this->lines as $i => $line) {
122+
if (strlen($line) < $indentation) {
123+
continue;
117124
}
118125

119-
$value = substr($value, $indentation);
120-
});
126+
$this->lines[$i] = substr($line, $indentation);
127+
}
121128
}
122129

123130
private function detectIndentation(): int

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

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,14 @@
1919

2020
use function array_column;
2121
use function array_flip;
22+
use function ctype_alnum;
23+
use function ctype_space;
2224
use function parse_url;
2325
use function preg_match;
26+
use function str_ends_with;
27+
use function str_replace;
28+
use function strlen;
29+
use function substr;
2430

2531
use const PHP_URL_SCHEME;
2632

@@ -111,6 +117,7 @@ protected function getType(string &$value)
111117
{
112118
$type = match ($value) {
113119
'`' => self::BACKTICK,
120+
'``' => self::DOUBLE_BACKTICK,
114121
'**' => self::STRONG_DELIMITER,
115122
'*' => self::EMPHASIS_DELIMITER,
116123
'|' => self::VARIABLE_DELIMITER,
@@ -130,10 +137,22 @@ protected function getType(string &$value)
130137
}
131138

132139
// $value is already a tokenized part. Therefore, we have to match against the complete String here.
133-
if (preg_match('/^\\\\[\s\S]$/i', $value)) {
140+
if (str_ends_with($value, '__') && ctype_alnum(str_replace('-', '', substr($value, 0, -2)))) {
141+
return self::ANONYMOUSE_REFERENCE;
142+
}
143+
144+
if (str_ends_with($value, '_') && ctype_alnum(str_replace('-', '', substr($value, 0, -1)))) {
145+
return self::NAMED_REFERENCE;
146+
}
147+
148+
if (strlen($value) === 2 && $value[0] === '\\') {
134149
return self::ESCAPED_SIGN;
135150
}
136151

152+
if (strlen($value) === 1 && ctype_space($value)) {
153+
return self::WHITESPACE;
154+
}
155+
137156
if (preg_match('/^``.+``(?!`)$/i', $value)) {
138157
return self::LITERAL;
139158
}
@@ -146,18 +165,6 @@ protected function getType(string &$value)
146165
return self::EMAIL;
147166
}
148167

149-
if (preg_match('/^[a-z0-9-]+_{2}$/i', $value)) {
150-
return self::ANONYMOUSE_REFERENCE;
151-
}
152-
153-
if (preg_match('/^[a-z0-9-]+_{1}$/i', $value)) {
154-
return self::NAMED_REFERENCE;
155-
}
156-
157-
if (preg_match('/^\s$/i', $value)) {
158-
return self::WHITESPACE;
159-
}
160-
161168
return self::WORD;
162169
}
163170
}

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

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@
1616
use Exception;
1717
use phpDocumentor\Guides\Nodes\Inline\PlainTextInlineNode;
1818
use phpDocumentor\Guides\Nodes\InlineCompoundNode;
19+
use phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules\CachableInlineRule;
1920
use phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules\InlineRule;
2021

22+
use function array_filter;
23+
use function array_key_exists;
2124
use function usort;
2225

2326
/** @internal */
@@ -26,11 +29,21 @@ class InlineParser
2629
/** @var InlineRule[] */
2730
private array $rules;
2831

32+
/** @var array<InlineLexer::*, CachableInlineRule> */
33+
private array $cache = [];
34+
2935
/** @param iterable<InlineRule> $inlineRules */
3036
public function __construct(iterable $inlineRules)
3137
{
32-
$this->rules = [...$inlineRules];
38+
$this->rules = array_filter([...$inlineRules], static fn ($rule) => $rule instanceof CachableInlineRule === false);
3339
usort($this->rules, static fn (InlineRule $a, InlineRule $b): int => $a->getPriority() > $b->getPriority() ? -1 : 1);
40+
foreach ($inlineRules as $rule) {
41+
if (!($rule instanceof CachableInlineRule)) {
42+
continue;
43+
}
44+
45+
$this->cache[$rule->getToken()] = $rule;
46+
}
3447
}
3548

3649
public function parse(string $content, BlockContext $blockContext): InlineCompoundNode
@@ -44,7 +57,9 @@ public function parse(string $content, BlockContext $blockContext): InlineCompou
4457
while ($lexer->token !== null) {
4558
foreach ($this->rules as $inlineRule) {
4659
$node = null;
47-
if ($inlineRule->applies($lexer)) {
60+
if (array_key_exists($lexer->token->type ?? -1, $this->cache)) {
61+
$node = $this->cache[$lexer->token->type]->apply($blockContext, $lexer);
62+
} elseif ($inlineRule->applies($lexer)) {
4863
$node = $inlineRule->apply($blockContext, $lexer);
4964
}
5065

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@
2828
*
2929
* @see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#anonymous-hyperlinks
3030
*/
31-
final class AnonymousReferenceRule extends ReferenceRule
31+
final class AnonymousReferenceRule extends ReferenceRule implements CachableInlineRule
3232
{
33+
public function getToken(): int
34+
{
35+
return InlineLexer::ANONYMOUSE_REFERENCE;
36+
}
37+
3338
public function applies(InlineLexer $lexer): bool
3439
{
3540
return $lexer->token?->type === InlineLexer::ANONYMOUSE_REFERENCE;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of phpDocumentor.
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*
11+
* @link https://phpdoc.org
12+
*/
13+
14+
namespace phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules;
15+
16+
use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;
17+
18+
interface CachableInlineRule extends InlineRule
19+
{
20+
/** @return InlineLexer::* */
21+
public function getToken(): int;
22+
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@
2323
/**
2424
* Rule for literals such as ``something``
2525
*/
26-
final class LiteralRule extends AbstractInlineRule
26+
final class LiteralRule extends AbstractInlineRule implements CachableInlineRule
2727
{
28+
public function getToken(): int
29+
{
30+
return InlineLexer::LITERAL;
31+
}
32+
2833
public function applies(InlineLexer $lexer): bool
2934
{
3035
return $lexer->token?->type === InlineLexer::LITERAL;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@
2828
*
2929
* @see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#hyperlink-references
3030
*/
31-
final class NamedReferenceRule extends ReferenceRule
31+
final class NamedReferenceRule extends ReferenceRule implements CachableInlineRule
3232
{
33+
public function getToken(): int
34+
{
35+
return InlineLexer::NAMED_REFERENCE;
36+
}
37+
3338
public function applies(InlineLexer $lexer): bool
3439
{
3540
return $lexer->token?->type === InlineLexer::NAMED_REFERENCE;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,13 @@
2020
/**
2121
* Rule to parse for non-breaking spaces: a~b
2222
*/
23-
final class NbspRule extends ReferenceRule
23+
final class NbspRule extends AbstractInlineRule implements CachableInlineRule
2424
{
25+
public function getToken(): int
26+
{
27+
return InlineLexer::NBSP;
28+
}
29+
2530
public function applies(InlineLexer $lexer): bool
2631
{
2732
return $lexer->token?->type === InlineLexer::NBSP;

0 commit comments

Comments
 (0)