Skip to content

Prefer array key exists over in array array keys#2837

Merged
Crabcyborg merged 1 commit into
masterfrom
prefer_array_key_exists_over_in_array_array_keys
Jan 19, 2026
Merged

Prefer array key exists over in array array keys#2837
Crabcyborg merged 1 commit into
masterfrom
prefer_array_key_exists_over_in_array_array_keys

Conversation

@Crabcyborg

@Crabcyborg Crabcyborg commented Jan 19, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • Refactor

    • Optimized array key existence checks throughout the codebase for improved performance and clarity.
  • Tests

    • Updated test utilities to use improved array key checking methods.
  • Chores

    • Added new code analysis rule to enforce best practices for array key existence validation.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai

coderabbitai Bot commented Jan 19, 2026

Copy link
Copy Markdown
Contributor
📝 Walkthrough

Walkthrough

This PR introduces a custom PHP_CodeSniffer sniff that detects and auto-fixes the pattern of using in_array() with array_keys() to check key existence, recommending the more efficient array_key_exists() instead. The sniff is registered in the ruleset, and existing code instances are updated to conform.

Changes

Cohort / File(s) Summary
Custom PHPCS Sniff Implementation
phpcs-sniffs/Formidable/Sniffs/CodeAnalysis/PreferArrayKeyExistsSniff.php, phpcs-sniffs/Formidable/ruleset.xml
New sniff class detects in_array($needle, array_keys($haystack)) patterns and provides auto-fix to replace with array_key_exists($needle, $haystack). Includes token parsing helpers and fix application logic. Sniff registered in ruleset under Code Analysis section.
Code Refactoring Updates
classes/helpers/FrmFormsHelper.php, classes/views/shared/mb_adv_info.php, tests/phpunit/entries/test_FrmShowEntryShortcode.php
Applied the sniff's recommendation: replaced in_array() with array_keys() checks with direct array_key_exists() calls in three locations. Behavior semantics remain equivalent with slightly improved efficiency.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Possibly related PRs

Poem

🐰 A sniff so keen, it spots with care,
When in_array dances in the air,
With array_keys it shouldn't play,
Use array_key_exists the better way! 🌟

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 75.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly summarizes the main change: replacing in_array(needle, array_keys(haystack)) with array_key_exists(needle, haystack) across multiple files and adding a PHP_CodeSniffer sniff to enforce this pattern.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 $phpcsFile parameter.

The $phpcsFile parameter 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 $tokens parameter.

The $tokens parameter 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 );

@Crabcyborg
Crabcyborg merged commit 62ef8fb into master Jan 19, 2026
38 checks passed
@Crabcyborg
Crabcyborg deleted the prefer_array_key_exists_over_in_array_array_keys branch January 19, 2026 21:56
stephywells pushed a commit that referenced this pull request Apr 4, 2026
…_in_array_array_keys

Prefer array key exists over in array array keys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant