Skip to content

Commit

Permalink
chore: aim to not rely on internal array pointer but use array_key_fi…
Browse files Browse the repository at this point in the history
…rst (#7613)
  • Loading branch information
keradus authored Dec 24, 2023
1 parent 8058b05 commit 968e789
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/Fixer/Alias/BacktickToShellExecFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ private function fixBackticks(Tokens $tokens, array $backtickTokens): void
{
// Track indices for final override
ksort($backtickTokens);
$openingBacktickIndex = key($backtickTokens);
$openingBacktickIndex = array_key_first($backtickTokens);
$closingBacktickIndex = array_key_last($backtickTokens);

// Strip enclosing backticks
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/Alias/EregToPregFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,6 @@ private function getBestDelimiter(string $pattern): string
return $a[0] <=> $b[0];
});

return key($delimiters);
return array_key_first($delimiters);
}
}
4 changes: 2 additions & 2 deletions src/Fixer/ClassNotation/ClassDefinitionFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,11 @@ private function getClassyDefinitionInfo(Tokens $tokens, int $classyIndex): arra

if (!$tokens[$classyIndex]->isGivenKind(T_TRAIT)) {
$extends = $tokens->findGivenKind(T_EXTENDS, $classyIndex, $openIndex);
$def['extends'] = [] !== $extends ? $this->getClassyInheritanceInfo($tokens, key($extends), 'numberOfExtends') : false;
$def['extends'] = [] !== $extends ? $this->getClassyInheritanceInfo($tokens, array_key_first($extends), 'numberOfExtends') : false;

if (!$tokens[$classyIndex]->isGivenKind(T_INTERFACE)) {
$implements = $tokens->findGivenKind(T_IMPLEMENTS, $classyIndex, $openIndex);
$def['implements'] = [] !== $implements ? $this->getClassyInheritanceInfo($tokens, key($implements), 'numberOfImplements') : false;
$def['implements'] = [] !== $implements ? $this->getClassyInheritanceInfo($tokens, array_key_first($implements), 'numberOfImplements') : false;
$def['anonymousClass'] = $tokensAnalyzer->isAnonymousClass($classyIndex);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/ControlStructure/SimplifiedIfReturnFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
continue;
}

$firstSequenceIndex = key($sequenceFound);
$firstSequenceIndex = array_key_first($sequenceFound);

if ($firstSequenceIndex !== $firstCandidateIndex) {
continue;
Expand Down
4 changes: 2 additions & 2 deletions src/Fixer/FunctionNotation/ImplodeCallFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
$argumentsIndices = $this->getArgumentIndices($tokens, $index);

if (1 === \count($argumentsIndices)) {
$firstArgumentIndex = key($argumentsIndices);
$firstArgumentIndex = array_key_first($argumentsIndices);
$tokens->insertAt($firstArgumentIndex, [
new Token([T_CONSTANT_ENCAPSED_STRING, "''"]),
new Token(','),
Expand All @@ -103,7 +103,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
// collect tokens from first argument
$firstArgumentEndIndex = $argumentsIndices[key($argumentsIndices)];
$newSecondArgumentTokens = [];
for ($i = key($argumentsIndices); $i <= $firstArgumentEndIndex; ++$i) {
for ($i = array_key_first($argumentsIndices); $i <= $firstArgumentEndIndex; ++$i) {
$newSecondArgumentTokens[] = clone $tokens[$i];
$tokens->clearAt($i);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/FunctionNotation/PhpdocToPropertyTypeFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ private function resolveApplicableType(array $propertyIndices, array $annotation
continue;
}

$propertyName = key($propertyIndices);
$propertyName = array_key_first($propertyIndices);
}

if (!isset($propertyIndices[$propertyName])) {
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/PhpTag/NoClosingTagFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
}

$closeTags = $tokens->findGivenKind(T_CLOSE_TAG);
$index = key($closeTags);
$index = array_key_first($closeTags);

if (isset($tokens[$index - 1]) && $tokens[$index - 1]->isWhitespace()) {
$tokens->clearAt($index - 1);
Expand Down
2 changes: 1 addition & 1 deletion src/Fixer/StringNotation/ExplicitStringVariableFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ protected function applyFix(\SplFileInfo $file, Tokens $tokens): void

foreach ($variableTokens as $distinctVariableSet) {
if (1 === \count($distinctVariableSet['tokens'])) {
$singleVariableIndex = key($distinctVariableSet['tokens']);
$singleVariableIndex = array_key_first($distinctVariableSet['tokens']);
$singleVariableToken = current($distinctVariableSet['tokens']);
$tokens->overrideRange($singleVariableIndex, $singleVariableIndex, [
new Token([T_CURLY_OPEN, '{']),
Expand Down
4 changes: 2 additions & 2 deletions src/Tokenizer/Tokens.php
Original file line number Diff line number Diff line change
Expand Up @@ -773,7 +773,7 @@ public function findSequence(array $sequence, int $start = 0, ?int $end = null,

// remove the first token from the sequence, so we can freely iterate through the sequence after a match to
// the first one is found
$key = key($sequence);
$key = array_key_first($sequence);
$firstCs = self::isKeyCaseSensitive($caseSensitive, $key);
$firstToken = $sequence[$key];
unset($sequence[$key]);
Expand Down Expand Up @@ -872,7 +872,7 @@ public function insertSlices(array $slices): void
$this->setSize($oldSize + $itemsCount);

krsort($slices);
$farthestSliceIndex = key($slices);
$farthestSliceIndex = array_key_first($slices);

// We check only the farthest index, if it's within the size of collection, other indices will be valid too.
if (!\is_int($farthestSliceIndex) || $farthestSliceIndex > $oldSize) {
Expand Down

0 comments on commit 968e789

Please sign in to comment.