Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove unnecessary method-proxying calls #7959

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 23 additions & 11 deletions src/Tokenizer/Tokens.php
Expand Up @@ -51,6 +51,10 @@ class Tokens extends \SplFixedArray
public const BLOCK_TYPE_DISJUNCTIVE_NORMAL_FORM_TYPE_PARENTHESIS = 12;
public const BLOCK_TYPE_DYNAMIC_CLASS_CONSTANT_FETCH_CURLY_BRACE = 13;

private const NON_MEANINGFUL_TOKENS = [T_WHITESPACE, T_COMMENT, T_DOC_COMMENT];
private const DIRECTION_PREV = -1;
private const DIRECTION_NEXT = 1;

/**
* Static class cache.
*
Expand Down Expand Up @@ -585,7 +589,7 @@ public function getCodeHash(): string
*/
public function getNextNonWhitespace(int $index, ?string $whitespaces = null): ?int
{
return $this->getNonWhitespaceSibling($index, 1, $whitespaces);
return $this->getNonWhitespaceSibling($index, self::DIRECTION_NEXT, $whitespaces);
}

/**
Expand All @@ -599,7 +603,7 @@ public function getNextNonWhitespace(int $index, ?string $whitespaces = null): ?
*/
public function getNextTokenOfKind(int $index, array $tokens = [], bool $caseSensitive = true): ?int
{
return $this->getTokenOfKindSibling($index, 1, $tokens, $caseSensitive);
return $this->getTokenOfKindSibling($index, self::DIRECTION_NEXT, $tokens, $caseSensitive);
}

/**
Expand Down Expand Up @@ -633,7 +637,7 @@ public function getNonWhitespaceSibling(int $index, int $direction, ?string $whi
*/
public function getPrevNonWhitespace(int $index, ?string $whitespaces = null): ?int
{
return $this->getNonWhitespaceSibling($index, -1, $whitespaces);
return $this->getNonWhitespaceSibling($index, self::DIRECTION_PREV, $whitespaces);
}

/**
Expand All @@ -646,7 +650,7 @@ public function getPrevNonWhitespace(int $index, ?string $whitespaces = null): ?
*/
public function getPrevTokenOfKind(int $index, array $tokens = [], bool $caseSensitive = true): ?int
{
return $this->getTokenOfKindSibling($index, -1, $tokens, $caseSensitive);
return $this->getTokenOfKindSibling($index, self::DIRECTION_PREV, $tokens, $caseSensitive);
}

/**
Expand Down Expand Up @@ -717,10 +721,10 @@ public function getTokenNotOfKindsSibling(int $index, int $direction, array $kin
*/
public function getMeaningfulTokenSibling(int $index, int $direction): ?int
{
return $this->getTokenNotOfKindsSibling(
return $this->getTokenNotOfKind(
$index,
$direction,
[T_WHITESPACE, T_COMMENT, T_DOC_COMMENT]
fn (int $index): bool => $this[$index]->isGivenKind(self::NON_MEANINGFUL_TOKENS),
);
}

Expand Down Expand Up @@ -751,7 +755,11 @@ public function getNonEmptySibling(int $index, int $direction): ?int
*/
public function getNextMeaningfulToken(int $index): ?int
{
return $this->getMeaningfulTokenSibling($index, 1);
return $this->getTokenNotOfKind(
$index,
self::DIRECTION_NEXT,
fn (int $index): bool => $this[$index]->isGivenKind(self::NON_MEANINGFUL_TOKENS),
);
}

/**
Expand All @@ -761,7 +769,11 @@ public function getNextMeaningfulToken(int $index): ?int
*/
public function getPrevMeaningfulToken(int $index): ?int
{
return $this->getMeaningfulTokenSibling($index, -1);
return $this->getTokenNotOfKind(
$index,
self::DIRECTION_PREV,
fn (int $index): bool => $this[$index]->isGivenKind(self::NON_MEANINGFUL_TOKENS),
);
}

/**
Expand Down Expand Up @@ -1021,15 +1033,15 @@ public function overrideRange(int $indexStart, int $indexEnd, iterable $items):
*/
public function removeLeadingWhitespace(int $index, ?string $whitespaces = null): void
{
$this->removeWhitespaceSafely($index, -1, $whitespaces);
$this->removeWhitespaceSafely($index, self::DIRECTION_PREV, $whitespaces);
}

/**
* @param null|string $whitespaces optional whitespaces characters for Token::isWhitespace
*/
public function removeTrailingWhitespace(int $index, ?string $whitespaces = null): void
{
$this->removeWhitespaceSafely($index, 1, $whitespaces);
$this->removeWhitespaceSafely($index, self::DIRECTION_NEXT, $whitespaces);
}

/**
Expand Down Expand Up @@ -1206,7 +1218,7 @@ public function clearTokenAndMergeSurroundingWhitespace(int $index): void
return;
}

$prevIndex = $this->getNonEmptySibling($index, -1);
$prevIndex = $this->getNonEmptySibling($index, self::DIRECTION_PREV);

if ($this[$prevIndex]->isWhitespace()) {
$this[$prevIndex] = new Token([T_WHITESPACE, $this[$prevIndex]->getContent().$this[$nextIndex]->getContent()]);
Expand Down