Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace phpDocumentor\Guides\RestructuredText\Parser;

use function array_pop;
use function array_walk;
use function count;
use function implode;
use function ltrim;
Expand Down Expand Up @@ -103,21 +102,29 @@ public function clear(): void

public function trimLines(): void
{
array_walk($this->lines, static function (&$value): void {
$value = trim($value);
});
foreach ($this->lines as $i => $line) {
$this->lines[$i] = trim($line);
}
}

private function unIndent(): void
{
if ($this->unindentStrategy === UnindentStrategy::NONE) {
return;
}

$indentation = $this->detectIndentation();
array_walk($this->lines, static function (&$value) use ($indentation): void {
if (strlen($value) < $indentation) {
return;
if ($indentation === 0) {
return;
}

foreach ($this->lines as $i => $line) {
if (strlen($line) < $indentation) {
continue;
}

$value = substr($value, $indentation);
});
$this->lines[$i] = substr($line, $indentation);
}
}

private function detectIndentation(): int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
use Exception;
use phpDocumentor\Guides\Nodes\Inline\PlainTextInlineNode;
use phpDocumentor\Guides\Nodes\InlineCompoundNode;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules\CachableInlineRule;
use phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules\InlineRule;

use function array_filter;
use function array_key_exists;
use function usort;

/** @internal */
Expand All @@ -26,11 +29,21 @@ class InlineParser
/** @var InlineRule[] */
private array $rules;

/** @var array<InlineLexer::*, CachableInlineRule> */
private array $cache = [];

/** @param iterable<InlineRule> $inlineRules */
public function __construct(iterable $inlineRules)
{
$this->rules = [...$inlineRules];
$this->rules = array_filter([...$inlineRules], static fn ($rule) => $rule instanceof CachableInlineRule === false);
usort($this->rules, static fn (InlineRule $a, InlineRule $b): int => $a->getPriority() > $b->getPriority() ? -1 : 1);
foreach ($inlineRules as $rule) {
if (!($rule instanceof CachableInlineRule)) {
continue;
}

$this->cache[$rule->getToken()] = $rule;
}
}

public function parse(string $content, BlockContext $blockContext): InlineCompoundNode
Expand All @@ -44,7 +57,9 @@ public function parse(string $content, BlockContext $blockContext): InlineCompou
while ($lexer->token !== null) {
foreach ($this->rules as $inlineRule) {
$node = null;
if ($inlineRule->applies($lexer)) {
if (array_key_exists($lexer->token->type ?? -1, $this->cache)) {
$node = $this->cache[$lexer->token->type]->apply($blockContext, $lexer);
} elseif ($inlineRule->applies($lexer)) {
$node = $inlineRule->apply($blockContext, $lexer);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@
*
* @see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#anonymous-hyperlinks
*/
final class AnonymousReferenceRule extends ReferenceRule
final class AnonymousReferenceRule extends ReferenceRule implements CachableInlineRule
{
public function getToken(): int
{
return InlineLexer::ANONYMOUSE_REFERENCE;
}

public function applies(InlineLexer $lexer): bool
{
return $lexer->token?->type === InlineLexer::ANONYMOUSE_REFERENCE;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

namespace phpDocumentor\Guides\RestructuredText\Parser\Productions\InlineRules;

use phpDocumentor\Guides\RestructuredText\Parser\InlineLexer;

interface CachableInlineRule extends InlineRule
{
/** @return InlineLexer::* */
public function getToken(): int;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,13 @@
/**
* Rule for literals such as ``something``
*/
final class LiteralRule extends AbstractInlineRule
final class LiteralRule extends AbstractInlineRule implements CachableInlineRule
{
public function getToken(): int
{
return InlineLexer::LITERAL;
}

public function applies(InlineLexer $lexer): bool
{
return $lexer->token?->type === InlineLexer::LITERAL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@
*
* @see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#hyperlink-references
*/
final class NamedReferenceRule extends ReferenceRule
final class NamedReferenceRule extends ReferenceRule implements CachableInlineRule
{
public function getToken(): int
{
return InlineLexer::NAMED_REFERENCE;
}

public function applies(InlineLexer $lexer): bool
{
return $lexer->token?->type === InlineLexer::NAMED_REFERENCE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,13 @@
/**
* Rule to parse for non-breaking spaces: a~b
*/
final class NbspRule extends ReferenceRule
final class NbspRule extends AbstractInlineRule implements CachableInlineRule
{
public function getToken(): int
{
return InlineLexer::NBSP;
}

public function applies(InlineLexer $lexer): bool
{
return $lexer->token?->type === InlineLexer::NBSP;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
*
* @see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#standalone-hyperlinks
*/
final class StandaloneEmailRule extends ReferenceRule
final class StandaloneEmailRule extends ReferenceRule implements CachableInlineRule
{
public function getToken(): int
{
return InlineLexer::EMAIL;
}

public function applies(InlineLexer $lexer): bool
{
return $lexer->token?->type === InlineLexer::EMAIL;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@
*
* @see https://docutils.sourceforge.io/docs/ref/rst/restructuredtext.html#standalone-hyperlinks
*/
final class StandaloneHyperlinkRule extends ReferenceRule
final class StandaloneHyperlinkRule extends ReferenceRule implements CachableInlineRule
{
public function getToken(): int
{
return InlineLexer::HYPERLINK;
}

public function applies(InlineLexer $lexer): bool
{
return $lexer->token?->type === InlineLexer::HYPERLINK;
Expand Down
39 changes: 39 additions & 0 deletions packages/guides-restructured-text/tests/benchmarks/BufferBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

/**
* This file is part of phpDocumentor.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @link https://phpdoc.org
*/

namespace phpDocumentor\Guides\RestructuredText;

use PhpBench\Attributes\Iterations;
use PhpBench\Attributes\Revs;
use phpDocumentor\Guides\RestructuredText\Parser\Buffer;

final class BufferBench
{
private Buffer $buffer;

public function __construct()
{
$this->buffer = new Buffer();
$this->buffer->push(' This is a line with leading spaces. ');
$this->buffer->push(' This is another line.');
$this->buffer->push(' Yet another line with spaces. ');
$this->buffer->push(' Final line.');
}

#[Revs([1000, 10_000])]
#[Iterations(5)]
public function benchGetLines(): void
{
$this->buffer->getLines();
}
}
7 changes: 4 additions & 3 deletions packages/guides/src/Files.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use IteratorAggregate;

use function count;
use function in_array;

Check failure on line 22 in packages/guides/src/Files.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Type in_array is not used in this file.
use function sort;

use const SORT_FLAG_CASE;
Expand All @@ -33,17 +33,18 @@

public function add(string $filename): void
{
if (in_array($filename, $this->files, true)) {
if (array_key_exists($filename, $this->files)) {

Check failure on line 36 in packages/guides/src/Files.php

View workflow job for this annotation

GitHub Actions / Coding Standards

Function array_key_exists() should not be referenced via a fallback global name, but via a use statement.
return;
}

$this->files[] = $filename;
sort($this->files, SORT_NATURAL | SORT_FLAG_CASE);
$this->files[$filename] = $filename;
}

/** @return Iterator<string> */
public function getIterator(): Iterator
{
sort($this->files, SORT_NATURAL | SORT_FLAG_CASE);

return new ArrayIterator($this->files);
}

Expand Down
Loading