Skip to content

Commit

Permalink
More null check on $token->getNextMeaningfulToken() on BlockFinder (#46)
Browse files Browse the repository at this point in the history
* More null check on $token->getNextMeaningfulToken() on BlockFinder

* fix phpstan

* fix phpstan
  • Loading branch information
samsonasik committed May 29, 2024
1 parent abd81d4 commit af40328
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 deletions.
1 change: 0 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -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<PhpCsFixer\\Tokenizer\\Token> but returns array<int, PhpCsFixer\\Tokenizer\\Token\|null>#'

# false positive
Expand Down
12 changes: 6 additions & 6 deletions src/TokenRunner/Analyzer/FixerAnalyzer/BlockFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand All @@ -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];
Expand All @@ -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);
Expand Down

0 comments on commit af40328

Please sign in to comment.