From af40328f126f4ad3ef9c8ad891502666c365178c Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Thu, 30 May 2024 04:56:33 +0700 Subject: [PATCH] More null check on $token->getNextMeaningfulToken() on BlockFinder (#46) * More null check on $token->getNextMeaningfulToken() on BlockFinder * fix phpstan * fix phpstan --- phpstan.neon | 1 - .../Analyzer/FixerAnalyzer/BlockFinder.php | 12 ++++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/phpstan.neon b/phpstan.neon index fba06832..c9126f00 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -19,7 +19,6 @@ parameters: message: '#Instantiated class PHP_CodeSniffer\\Util\\Tokens not found#' - '#Constant T_OPEN_CURLY_BRACKET|T_START_NOWDOC not found#' - - '#Parameter \#1 \$index of method PhpCsFixer\\Tokenizer\\Tokens\:\:getNextTokenOfKind\(\) expects int, int\|null given#' - '#Method Symplify\\CodingStandard\\TokenRunner\\Traverser\\ArrayBlockInfoFinder\:\:reverseTokens\(\) should return array but returns array#' # false positive diff --git a/src/TokenRunner/Analyzer/FixerAnalyzer/BlockFinder.php b/src/TokenRunner/Analyzer/FixerAnalyzer/BlockFinder.php index 9f296234..8373f7bf 100644 --- a/src/TokenRunner/Analyzer/FixerAnalyzer/BlockFinder.php +++ b/src/TokenRunner/Analyzer/FixerAnalyzer/BlockFinder.php @@ -48,6 +48,11 @@ public function findInTokensByEdge(Tokens $tokens, int $position): ?BlockInfo // shift "array" to "(", event its position if ($token->isGivenKind(T_ARRAY)) { $position = $tokens->getNextMeaningfulToken($position); + + if ($position === null) { + return null; + } + /** @var Token $token */ $token = $tokens[$position]; } @@ -67,7 +72,7 @@ public function findInTokensByEdge(Tokens $tokens, int $position): ?BlockInfo return null; } - if ($position !== null && $token->equals('(')) { + if ($token->equals('(')) { $closingPosition = $tokens->getNextMeaningfulToken($position); if ($closingPosition !== null) { $closingToken = $tokens[$closingPosition]; @@ -79,11 +84,6 @@ public function findInTokensByEdge(Tokens $tokens, int $position): ?BlockInfo } } - // some invalid code - if ($position === null) { - return null; - } - $blockType = $this->getBlockTypeByToken($token); return $this->createBlockInfo($token, $position, $tokens, $blockType);