Skip to content
Merged
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
33 changes: 33 additions & 0 deletions src/Parser/TokenIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
use function assert;
use function count;
use function in_array;
use function preg_match_all;
use function strlen;
use function strpos;
use function substr;

class TokenIterator
Expand All @@ -34,6 +36,9 @@ class TokenIterator
/** @var list<int>|null */
private ?array $offsets = null;

/** @var array<string, int>|null the last index of a token containing "/name>", by the name */
private ?array $endTagIndexes = null;

/**
* @param list<array{string, int, int}> $tokens
*/
Expand Down Expand Up @@ -390,6 +395,34 @@ public function hasTokenImmediatelyAfter(int $pos, int $expectedTokenType): bool
return false;
}

/**
* Whether a token at the current index or after it contains "/$name>",
* which every end tag of an HTML tag of that name does.
*
* The tokens are searched once, so that asking about every "<" of a
* PHPDoc does not walk to its end every time.
*
* @internal
*/
public function hasEndTagCandidateAhead(string $name): bool
{
if ($this->endTagIndexes === null) {
$this->endTagIndexes = [];
foreach ($this->tokens as $index => $token) {
$value = $token[Lexer::VALUE_OFFSET];
if (strpos($value, '/') === false || preg_match_all('~/([^/>]++)>~', $value, $matches) === 0) {
continue;
}

foreach ($matches[1] as $candidate) {
$this->endTagIndexes[$candidate] = $index;
}
}
}

return ($this->endTagIndexes[$name] ?? -1) >= $this->index;
}

public function getDetectedNewline(): ?string
{
return $this->newline;
Expand Down
5 changes: 5 additions & 0 deletions src/Parser/TypeParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ public function isHtml(TokenIterator $tokens): bool
return false;
}

// every end tag the walk below looks for contains this
if (!$tokens->hasEndTagCandidateAhead($htmlTagName)) {
return false;
}

$endTag = '</' . $htmlTagName . '>';
$endTagSearchOffset = - strlen($endTag);

Expand Down
Loading