|
| 1 | +<?php declare(strict_types=1); |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of PHP CS Fixer: custom fixers. |
| 5 | + * |
| 6 | + * (c) 2018 Kuba Werłos |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view |
| 9 | + * the LICENSE file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace PhpCsFixerCustomFixers\Fixer; |
| 13 | + |
| 14 | +use PhpCsFixer\FixerDefinition\CodeSample; |
| 15 | +use PhpCsFixer\FixerDefinition\FixerDefinition; |
| 16 | +use PhpCsFixer\FixerDefinition\FixerDefinitionInterface; |
| 17 | +use PhpCsFixer\Tokenizer\Token; |
| 18 | +use PhpCsFixer\Tokenizer\Tokens; |
| 19 | +use PhpCsFixerCustomFixers\Analyzer\FunctionAnalyzer; |
| 20 | + |
| 21 | +final class IssetToArrayKeyExistsFixer extends AbstractFixer |
| 22 | +{ |
| 23 | + public function getDefinition(): FixerDefinitionInterface |
| 24 | + { |
| 25 | + return new FixerDefinition( |
| 26 | + 'Function `array_key_exists` must be used over `isset` when possible.', |
| 27 | + [ |
| 28 | + new CodeSample( |
| 29 | + '<?php |
| 30 | +if (isset($array[$key])) { |
| 31 | + echo $array[$key]; |
| 32 | +} |
| 33 | +' |
| 34 | + ), |
| 35 | + ], |
| 36 | + null, |
| 37 | + 'when array is not defined, is multi-dimensional or behaviour is relying on the null value' |
| 38 | + ); |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Must run before NativeFunctionInvocationFixer. |
| 43 | + */ |
| 44 | + public function getPriority(): int |
| 45 | + { |
| 46 | + return 2; |
| 47 | + } |
| 48 | + |
| 49 | + public function isCandidate(Tokens $tokens): bool |
| 50 | + { |
| 51 | + return $tokens->isTokenKindFound(\T_ISSET); |
| 52 | + } |
| 53 | + |
| 54 | + public function isRisky(): bool |
| 55 | + { |
| 56 | + return true; |
| 57 | + } |
| 58 | + |
| 59 | + public function fix(\SplFileInfo $file, Tokens $tokens): void |
| 60 | + { |
| 61 | + for ($index = $tokens->count() - 1; $index > 0; $index--) { |
| 62 | + if (!$tokens[$index]->isGivenKind(\T_ISSET)) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + if (\count(FunctionAnalyzer::getFunctionArguments($tokens, $index)) !== 1) { |
| 67 | + continue; |
| 68 | + } |
| 69 | + |
| 70 | + $openParenthesis = $tokens->getNextMeaningfulToken($index); |
| 71 | + \assert(\is_int($openParenthesis)); |
| 72 | + |
| 73 | + $closeParenthesis = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $openParenthesis); |
| 74 | + |
| 75 | + $closeBrackets = $tokens->getPrevMeaningfulToken($closeParenthesis); |
| 76 | + \assert(\is_int($closeBrackets)); |
| 77 | + if (!$tokens[$closeBrackets]->equals(']')) { |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + $openBrackets = $tokens->findBlockStart(Tokens::BLOCK_TYPE_ARRAY_SQUARE_BRACE, $closeBrackets); |
| 82 | + |
| 83 | + $keyStartIndex = $tokens->getNextMeaningfulToken($openBrackets); |
| 84 | + \assert(\is_int($keyStartIndex)); |
| 85 | + $keyEndIndex = $tokens->getPrevMeaningfulToken($closeBrackets); |
| 86 | + |
| 87 | + $keyTokens = []; |
| 88 | + for ($i = $keyStartIndex; $i <= $keyEndIndex; $i++) { |
| 89 | + if ($tokens[$i]->equals('')) { |
| 90 | + continue; |
| 91 | + } |
| 92 | + $keyTokens[] = $tokens[$i]; |
| 93 | + } |
| 94 | + $keyTokens[] = new Token(','); |
| 95 | + $keyTokens[] = new Token([\T_WHITESPACE, ' ']); |
| 96 | + |
| 97 | + $tokens->clearRange($openBrackets, $closeBrackets); |
| 98 | + $tokens->insertAt($openParenthesis + 1, $keyTokens); |
| 99 | + $tokens[$index] = new Token([\T_STRING, 'array_key_exists']); |
| 100 | + } |
| 101 | + } |
| 102 | +} |
0 commit comments