Skip to content

Commit

Permalink
Merge b0e279f into 5519faf
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Apr 19, 2024
2 parents 5519faf + b0e279f commit 21e68eb
Showing 1 changed file with 23 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/Tokenizer/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,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 @@ -565,7 +569,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 @@ -579,7 +583,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 @@ -614,7 +618,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 @@ -627,7 +631,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 @@ -699,10 +703,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 @@ -734,7 +738,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 @@ -744,7 +752,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 @@ -1004,15 +1016,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 @@ -1189,7 +1201,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

0 comments on commit 21e68eb

Please sign in to comment.