Skip to content

Commit

Permalink
minor refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
mvorisek committed Jan 9, 2024
1 parent 28baa2d commit b58bdc7
Showing 1 changed file with 7 additions and 14 deletions.
21 changes: 7 additions & 14 deletions src/Fixer/Import/FullyQualifiedStrictTypesFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -682,11 +682,11 @@ private function replaceByShortType(Tokens $tokens, TypeAnalysis $type, array $u

$types = $this->getTypes($tokens, $typeStartIndex, $type->getEndIndex());

foreach ($types as $typeName => [$startIndex, $endIndex]) {
$shortType = $this->determineShortType($typeName, $uses, $namespaceName);

if (null !== $shortType) {
$tokens->overrideRange($startIndex, $endIndex, $shortType);
foreach ($types as [$startIndex, $endIndex]) {
$content = $tokens->generatePartialCode($startIndex, $endIndex);
$newTokens = $this->determineShortType($content, $uses, $namespaceName);
if (null !== $newTokens) {
$tokens->overrideRange($startIndex, $endIndex, $newTokens);
}
}
}
Expand Down Expand Up @@ -720,18 +720,16 @@ private function determineShortType(string $typeName, array $uses, string $names
}

/**
* @return iterable<string, array{int, int}>
* @return iterable<array{int, int}>
*/
private function getTypes(Tokens $tokens, int $index, int $endIndex): iterable
{
$skipNextYield = false;
$typeStartIndex = $typeEndIndex = null;
$type = null;
while (true) {
if ($tokens[$index]->isGivenKind(CT::T_DISJUNCTIVE_NORMAL_FORM_TYPE_PARENTHESIS_OPEN)) {
$index = $tokens->getNextMeaningfulToken($index);
$typeStartIndex = $typeEndIndex = null;
$type = null;

continue;
}
Expand All @@ -743,19 +741,17 @@ private function getTypes(Tokens $tokens, int $index, int $endIndex): iterable
if (!$skipNextYield && null !== $typeStartIndex) {
$origCount = \count($tokens);

yield $type => [$typeStartIndex, $typeEndIndex];
yield [$typeStartIndex, $typeEndIndex];

$endIndex += \count($tokens) - $origCount;

// type tokens were possibly updated, restart type match
$skipNextYield = true;
$index = $typeEndIndex = $typeStartIndex;
$type = null;
} else {
$skipNextYield = false;
$index = $tokens->getNextMeaningfulToken($index);
$typeStartIndex = $typeEndIndex = null;
$type = null;
}

if ($index > $endIndex) {
Expand All @@ -767,11 +763,8 @@ private function getTypes(Tokens $tokens, int $index, int $endIndex): iterable

if (null === $typeStartIndex) {
$typeStartIndex = $index;
$type = '';
}

$typeEndIndex = $index;
$type .= $tokens[$index]->getContent();

$index = $tokens->getNextMeaningfulToken($index);
}
Expand Down

0 comments on commit b58bdc7

Please sign in to comment.