Prefer array key exists over in array array keys#2837
Conversation
📝 WalkthroughWalkthroughThis PR introduces a custom PHP_CodeSniffer sniff that detects and auto-fixes the pattern of using Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Fix all issues with AI agents
In `@phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferArrayKeyExistsSniff.php`:
- Around line 93-107: The sniff must skip transforms when array_keys() has
multiple arguments; after locating $arrayKeysOpenParen/$arrayKeysCloseParen and
$arrayStart in PreferArrayKeyExistsSniff, check for any comma token between
$arrayStart and $arrayKeysCloseParen (e.g. $phpcsFile->findNext(T_COMMA,
$arrayStart, $arrayKeysCloseParen)) and return early if found, before building
$arrayContent or performing the replacement, so only single-argument
array_keys(...) cases are handled.
🧹 Nitpick comments (2)
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferArrayKeyExistsSniff.php (2)
142-156: Remove unused$phpcsFileparameter.The
$phpcsFileparameter is not used in this method. As flagged by static analysis, it can be removed.♻️ Suggested cleanup
- private function findNextCommaAtSameLevel( File $phpcsFile, array $tokens, $start, $end ) { + private function findNextCommaAtSameLevel( array $tokens, $start, $end ) {And update the caller at line 68:
- $firstComma = $this->findNextCommaAtSameLevel( $phpcsFile, $tokens, $needleStart, $closeParen ); + $firstComma = $this->findNextCommaAtSameLevel( $tokens, $needleStart, $closeParen );
170-185: Remove unused$tokensparameter.The
$tokensparameter is not used in this method. As flagged by static analysis, it can be removed.♻️ Suggested cleanup
- private function applyFix( File $phpcsFile, array $tokens, $stackPtr, $closeParen, $needleContent, $arrayContent ) { + private function applyFix( File $phpcsFile, $stackPtr, $closeParen, $needleContent, $arrayContent ) {And update the caller at line 128:
- $this->applyFix( $phpcsFile, $tokens, $stackPtr, $closeParen, $needleContent, $arrayContent ); + $this->applyFix( $phpcsFile, $stackPtr, $closeParen, $needleContent, $arrayContent );
…_in_array_array_keys Prefer array key exists over in array array keys
Summary by CodeRabbit
Refactor
Tests
Chores
✏️ Tip: You can customize this high-level summary in your review settings.