Skip to content

Fix undefined array key 0 in Query/Filter isEmpty#881

Open
lohanidamodar wants to merge 1 commit into
mainfrom
fix-filter-isempty-undefined-key
Open

Fix undefined array key 0 in Query/Filter isEmpty#881
lohanidamodar wants to merge 1 commit into
mainfrom
fix-filter-isempty-undefined-key

Conversation

@lohanidamodar
Copy link
Copy Markdown
Contributor

@lohanidamodar lohanidamodar commented May 24, 2026

Summary

Filter::isEmpty() accesses $values[0] after only checking count($values) === 0. When the values array is non-empty but not zero-indexed (sparse arrays, associative arrays), PHP emits Warning: Undefined array key 0. Replaced the direct index access with array_key_first() so the first element is read regardless of key layout. Behaviour for zero-indexed arrays is unchanged.

Test plan

  • Added unit test in tests/unit/Validator/Query/FilterTest.php covering sparse and associative value arrays.
  • composer lint clean.
  • composer check (PHPStan level 7) clean.

Summary by CodeRabbit

  • Bug Fixes
    • Fixed query filter validation to correctly handle arrays with non-zero starting indices and associative array keys. The validator now properly detects empty value arrays regardless of their indexing structure, improving reliability of query filtering across different data formats.

Review Change Stack

isEmpty() called $values[0] when the array was non-empty but not zero-indexed (sparse or associative), emitting Warning: Undefined array key 0. Use array_key_first() to address the first element regardless of key layout.
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 24, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro

Run ID: 4e7636e4-d04f-4f18-8f6d-271cd66870ce

📥 Commits

Reviewing files that changed from the base of the PR and between 5679019 and 1d8c8a4.

📒 Files selected for processing (2)
  • src/Database/Validator/Query/Filter.php
  • tests/unit/Validator/Query/FilterTest.php

📝 Walkthrough

Walkthrough

Filter validator's isEmpty() method now uses array_key_first() to safely access the first element of sparse or associative arrays instead of assuming numeric index 0, with a new test verifying both sparse-indexed and associative value arrays are handled correctly.

Changes

Sparse Array Index Handling

Layer / File(s) Summary
Array index detection using array_key_first()
src/Database/Validator/Query/Filter.php, tests/unit/Validator/Query/FilterTest.php
Filter::isEmpty() replaces hard-coded $values[0] with array_key_first() to safely determine the first key in sparse or associative arrays. New test testIsEmptyHandlesNonZeroIndexedValues() verifies equality and contains queries with sparse-indexed [1 => ...] and associative ['foo' => ...] value arrays are treated as valid.

🐰 Array keys need gentle care,
No more assuming they start at zero,
With first() we're aware—
Sparse maps now pass the hero!

🎯 1 (Trivial) | ⏱️ ~5 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 25.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title directly and clearly describes the main fix: using array_key_first() instead of direct index access to resolve undefined array key 0 warnings in Filter::isEmpty().
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

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

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch fix-filter-isempty-undefined-key

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.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented May 24, 2026

Greptile Summary

Fixes a PHP warning in Filter::isEmpty() where $values[0] was accessed without confirming the array is zero-indexed, causing an Undefined array key 0 notice on sparse or associative value arrays.

  • Replaces $values[0] with array_key_first($values) so the first element is read safely regardless of key layout; behaviour for zero-indexed arrays is unchanged.
  • Adds a focused unit test for both a sparse ([1 => 'super']) and an associative (['foo' => 'super']) values array to confirm neither path triggers the warning.

Confidence Score: 5/5

Safe to merge — the change is a one-line targeted fix with no functional side effects on normal zero-indexed arrays.

The change is minimal and correct: array_key_first() is the idiomatic PHP solution, the prior count === 0 guard ensures the function never returns null at that call site, and the added tests directly exercise the two newly-safe code paths.

No files require special attention.

Important Files Changed

Filename Overview
src/Database/Validator/Query/Filter.php Replaces direct $values[0] access in isEmpty() with array_key_first() to safely handle sparse/associative arrays without triggering an undefined-key warning.
tests/unit/Validator/Query/FilterTest.php Adds a new test covering sparse (integer-keyed starting at 1) and associative value arrays to verify the fix does not regress or crash.

Reviews (1): Last reviewed commit: "Fix undefined array key 0 in Query/Filte..." | Re-trigger Greptile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant