Skip to content

Commit

Permalink
chore: TernaryToElvisOperatorFixer - improvements based on PHPStan de…
Browse files Browse the repository at this point in the history
…tections (#8345)
  • Loading branch information
keradus authored Jan 4, 2025
1 parent 2142cd9 commit 3c82851
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 17 deletions.
12 changes: 0 additions & 12 deletions dev-tools/phpstan/baseline.php
Original file line number Diff line number Diff line change
Expand Up @@ -1555,18 +1555,6 @@
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Operator/BinaryOperatorSpacesFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Method PhpCsFixer\\\\Fixer\\\\Operator\\\\TernaryToElvisOperatorFixer\\:\\:getAfterOperator\\(\\) should return array\\{start\\: int, end\\: int\\} but returns array\\{start\\: int\\|null, end\\?\\: int\\|null\\}\\.$#',
'identifier' => 'return.type',
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Operator/TernaryToElvisOperatorFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Offset 1\\|2\\|3\\|4\\|5\\|6\\|7\\|8\\|9\\|10\\|11\\|12\\|13\\|14\\|15 might not exist on array\\<1\\|2\\|3\\|4\\|5\\|6\\|7\\|8\\|9\\|10\\|11\\|12\\|13\\|14\\|15, array\\{start\\: array\\{int, string\\}\\|string, end\\: array\\{int, string\\}\\|string\\}\\>\\.$#',
'identifier' => 'offsetAccess.notFound',
'count' => 1,
'path' => __DIR__ . '/../../src/Fixer/Operator/TernaryToElvisOperatorFixer.php',
];
$ignoreErrors[] = [
'message' => '#^Method PhpCsFixer\\\\Fixer\\\\PhpUnit\\\\PhpUnitAttributesFixer\\:\\:createAttributeTokens\\(\\) should return list\\<PhpCsFixer\\\\Tokenizer\\\\Token\\> but returns non\\-empty\\-array\\<int\\|string, PhpCsFixer\\\\Tokenizer\\\\Token\\>\\.$#',
'identifier' => 'return.type',
Expand Down
11 changes: 6 additions & 5 deletions src/Fixer/Operator/TernaryToElvisOperatorFixer.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,17 @@ private function getBeforeOperator(Tokens $tokens, int $index): ?array
return null;
}

$blockType = Tokens::detectBlockType($tokens[$index]);
$detectedBlockType = Tokens::detectBlockType($tokens[$index]);

if (null === $blockType || $blockType['isStart']) {
if (null === $detectedBlockType || $detectedBlockType['isStart']) {
$before['start'] = $index;
$index = $tokens->getPrevMeaningfulToken($index);

continue;
}

$blockType = $blockEdgeDefinitions[$blockType['type']];
/** @phpstan-ignore-next-line offsetAccess.notFound (we just detected block type, we know it's definition exists under given PHP runtime) */
$blockType = $blockEdgeDefinitions[$detectedBlockType['type']];
$openCount = 1;

do {
Expand Down Expand Up @@ -189,7 +190,7 @@ private function getAfterOperator(Tokens $tokens, int $index): array
$index = $tokens->getNextMeaningfulToken($index);
$after = ['start' => $index];

while (!$tokens[$index]->equals(':')) {
do {
$blockType = Tokens::detectBlockType($tokens[$index]);

if (null !== $blockType) {
Expand All @@ -198,7 +199,7 @@ private function getAfterOperator(Tokens $tokens, int $index): array

$after['end'] = $index;
$index = $tokens->getNextMeaningfulToken($index);
}
} while (!$tokens[$index]->equals(':'));

return $after;
}
Expand Down

0 comments on commit 3c82851

Please sign in to comment.