Skip to content

Commit

Permalink
chore: Tokens should be always a list (#7698)
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Apr 22, 2024
1 parent 4edae7d commit 37d8d45
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/Fixer/ClassNotation/OrderedInterfacesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}

foreach ($interfaces as $interfaceIndex => $interface) {
$interfaceTokens = Tokens::fromArray($interface, false);
$interfaceTokens = Tokens::fromArray($interface);
$normalized = '';
$actualInterfaceIndex = $interfaceTokens->getNextMeaningfulToken(-1);

Expand Down
48 changes: 32 additions & 16 deletions src/Tokenizer/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@

namespace PhpCsFixer\Tokenizer;

use PhpCsFixer\Console\Application;
use PhpCsFixer\Preg;
use PhpCsFixer\Tokenizer\Analyzer\Analysis\NamespaceAnalysis;
use PhpCsFixer\Tokenizer\Analyzer\NamespacesAnalyzer;
use PhpCsFixer\Utils;

/**
* Collection of code tokens.
Expand Down Expand Up @@ -164,13 +166,17 @@ public static function fromArray($array, $saveIndices = null): self
{
$tokens = new self(\count($array));

if ($saveIndices ?? true) {
if (false !== $saveIndices && !array_is_list($array)) {
Utils::triggerDeprecation(new \InvalidArgumentException(sprintf(
'Parameter "array" should be a list. This will be enforced in version %d.0.',
Application::getMajorVersion() + 1
)));

foreach ($array as $key => $val) {
$tokens[$key] = $val;
}
} else {
$index = 0;

foreach ($array as $val) {
$tokens[$index++] = $val;
}
Expand Down Expand Up @@ -290,7 +296,7 @@ public static function getBlockEdgeDefinitions(): array
#[\ReturnTypeWillChange]
public function setSize($size): bool
{
if ($this->getSize() !== $size) {
if (\count($this) !== $size) {
$this->changed = true;
$this->namespaceDeclarations = null;

Expand All @@ -307,6 +313,13 @@ public function setSize($size): bool
*/
public function offsetUnset($index): void
{
if (\count($this) - 1 !== $index) {
Utils::triggerDeprecation(new \InvalidArgumentException(sprintf(
'Tokens should be a list - only the last index can be unset. This will be enforced in version %d.0.',
Application::getMajorVersion() + 1
)));
}

if (isset($this[$index])) {
if (isset($this->blockStartCache[$index])) {
unset($this->blockEndCache[$this->blockStartCache[$index]], $this->blockStartCache[$index]);
Expand Down Expand Up @@ -334,6 +347,13 @@ public function offsetUnset($index): void
*/
public function offsetSet($index, $newval): void
{
if (0 > $index || \count($this) <= $index) {
Utils::triggerDeprecation(new \InvalidArgumentException(sprintf(
'Tokens should be a list - index must be within the existing range. This will be enforced in version %d.0.',
Application::getMajorVersion() + 1
)));
}

if (!isset($this[$index]) || !$this[$index]->equals($newval)) {
if (isset($this[$index])) {
if (isset($this->blockStartCache[$index])) {
Expand Down Expand Up @@ -370,7 +390,7 @@ public function clearChanged(): void
*/
public function clearEmptyTokens(): void
{
$limit = $this->count();
$limit = \count($this);

for ($index = 0; $index < $limit; ++$index) {
if ($this->isEmptyAt($index)) {
Expand Down Expand Up @@ -470,7 +490,7 @@ public function ensureWhitespaceAtIndex(int $index, int $indexOffset, string $wh
* @param self::BLOCK_TYPE_* $type type of block
* @param int $searchIndex index of opening brace
*
* @return int index of closing brace
* @return int<0, max> index of closing brace
*/
public function findBlockEnd(int $type, int $searchIndex): int
{
Expand All @@ -481,7 +501,7 @@ public function findBlockEnd(int $type, int $searchIndex): int
* @param self::BLOCK_TYPE_* $type type of block
* @param int $searchIndex index of closing brace
*
* @return int index of opening brace
* @return int<0, max> index of opening brace
*/
public function findBlockStart(int $type, int $searchIndex): int
{
Expand All @@ -493,12 +513,12 @@ public function findBlockStart(int $type, int $searchIndex): int
* @param int $start optional offset
* @param null|int $end optional limit
*
* @return ($possibleKind is int ? array<int, Token> : array<int, array<int, Token>>)
* @return ($possibleKind is int ? array<int<0, max>, Token> : array<int, array<int<0, max>, Token>>)
*/
public function findGivenKind($possibleKind, int $start = 0, ?int $end = null): array
{
if (null === $end) {
$end = $this->count();
$end = \count($this);
}

$elements = [];
Expand Down Expand Up @@ -593,7 +613,6 @@ public function getNonWhitespaceSibling(int $index, int $direction, ?string $whi
{
while (true) {
$index += $direction;

if (!$this->offsetExists($index)) {
return null;
}
Expand Down Expand Up @@ -648,7 +667,6 @@ public function getTokenOfKindSibling(int $index, int $direction, array $tokens

while (true) {
$index += $direction;

if (!$this->offsetExists($index)) {
return null;
}
Expand Down Expand Up @@ -716,7 +734,6 @@ public function getNonEmptySibling(int $index, int $direction): ?int
{
while (true) {
$index += $direction;

if (!$this->offsetExists($index)) {
return null;
}
Expand Down Expand Up @@ -757,7 +774,7 @@ public function getPrevMeaningfulToken(int $index): ?int
* the ones used in $sequence. If any is missing, the default case-sensitive
* comparison is used
*
* @return null|non-empty-array<int, Token> an array containing the tokens matching the sequence elements, indexed by their position
* @return null|non-empty-array<int<0, max>, Token> an array containing the tokens matching the sequence elements, indexed by their position
*/
public function findSequence(array $sequence, int $start = 0, ?int $end = null, $caseSensitive = true): ?array
{
Expand Down Expand Up @@ -924,7 +941,7 @@ public function insertSlices(array $slices): void
$sliceCount = \count($slice);

for ($i = $previousSliceIndex - 1; $i >= $index; --$i) {
parent::offsetSet($i + $itemsCount, parent::offsetGet($i));
parent::offsetSet($i + $itemsCount, $this[$i]);
}

$previousSliceIndex = $index;
Expand Down Expand Up @@ -1261,7 +1278,7 @@ private function removeWhitespaceSafely(int $index, int $direction, ?string $whi
* @param int $searchIndex index of starting brace
* @param bool $findEnd if method should find block's end or start
*
* @return int index of opposite brace
* @return int<0, max> index of opposite brace
*/
private function findOppositeBlockEdge(int $type, int $searchIndex, bool $findEnd): int
{
Expand All @@ -1282,7 +1299,7 @@ private function findOppositeBlockEdge(int $type, int $searchIndex, bool $findEn
$startEdge = $blockEdgeDefinitions[$type]['start'];
$endEdge = $blockEdgeDefinitions[$type]['end'];
$startIndex = $searchIndex;
$endIndex = $this->count() - 1;
$endIndex = \count($this) - 1;
$indexOffset = 1;

if (!$findEnd) {
Expand Down Expand Up @@ -1448,7 +1465,6 @@ private function getTokenNotOfKind(int $index, int $direction, callable $filter)
{
while (true) {
$index += $direction;

if (!$this->offsetExists($index)) {
return null;
}
Expand Down

0 comments on commit 37d8d45

Please sign in to comment.