Skip to content
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Micro-optimize TokenIterator::joinUntil()
  • Loading branch information
rvanvelzen committed Nov 14, 2022
commit f884b3cd42e03a99f0fd0d8dd1a2fb2127b62abb
5 changes: 4 additions & 1 deletion src/Parser/TokenIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PHPStan\PhpDocParser\Parser;

use PHPStan\PhpDocParser\Lexer\Lexer;
use function array_flip;
use function array_pop;
use function assert;
use function count;
Expand Down Expand Up @@ -160,8 +161,10 @@ public function getSkippedHorizontalWhiteSpaceIfAny(): string
/** @phpstan-impure */
public function joinUntil(int ...$tokenType): string
{
$tokenTypeMap = array_flip($tokenType);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this faster then array_fill_keys($tokenType, true)?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's indistinguishable so changing to array_fill_keys() is fine by me :)


$s = '';
while (!in_array($this->tokens[$this->index][Lexer::TYPE_OFFSET], $tokenType, true)) {
while (!isset($tokenTypeMap[$this->tokens[$this->index][Lexer::TYPE_OFFSET]])) {
$s .= $this->tokens[$this->index++][Lexer::VALUE_OFFSET];
}
return $s;
Expand Down