diff --git a/src/Tokenizer/Tokens.php b/src/Tokenizer/Tokens.php index f4c7ff839cc..9d8e4f013d1 100644 --- a/src/Tokenizer/Tokens.php +++ b/src/Tokenizer/Tokens.php @@ -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. * @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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); } /** @@ -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), ); } @@ -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), + ); } /** @@ -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), + ); } /** @@ -1021,7 +1033,7 @@ 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); } /** @@ -1029,7 +1041,7 @@ public function removeLeadingWhitespace(int $index, ?string $whitespaces = null) */ public function removeTrailingWhitespace(int $index, ?string $whitespaces = null): void { - $this->removeWhitespaceSafely($index, 1, $whitespaces); + $this->removeWhitespaceSafely($index, self::DIRECTION_NEXT, $whitespaces); } /** @@ -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()]);