Skip to content

Fix undefined array key 0 in RW::parseError() when results is empty#99

Open
castillo-n wants to merge 1 commit intoSpoje-NET:mainfrom
castillo-n:fix/empty-results-parse-error
Open

Fix undefined array key 0 in RW::parseError() when results is empty#99
castillo-n wants to merge 1 commit intoSpoje-NET:mainfrom
castillo-n:fix/empty-results-parse-error

Conversation

@castillo-n
Copy link
Copy Markdown

@castillo-n castillo-n commented Apr 2, 2026

What's happening

When some API endpoints (like automaticke-parovani) return a successful
response with an empty results array, parseError() falls into the
else branch on line 183 and tries to access results[0], which doesn't
exist. This throws an Undefined array key 0 warning.

Fix

Added a \count($responseDecoded['results']) > 0 check on line 169 so
we skip the whole block when results is empty — there's nothing to parse
for errors anyway.

Tests

Added two unit tests:

Both run offline, no AbraFlexi server needed.

Fixes #98

Summary by CodeRabbit

  • Bug Fixes
    • Fixed error handling to properly manage API responses with empty result sets. The system now gracefully processes these edge cases and falls back appropriately, improving stability when encountering such error conditions.

parseError() crashes when the API returns a successful response with an
empty results array. The else branch tries to access results[0] even
though the key check just confirmed it doesn't exist. Added a count
check so we skip the block entirely when there's nothing to parse.

Fixes Spoje-NET#98
@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 2, 2026

📝 Walkthrough

Walkthrough

This pull request fixes a bug in RW::parseError() where an undefined array key error occurred when the API returned a successful response with an empty results array. The fix adds a non-empty check to the existing array validation condition, and includes tests verifying the correction.

Changes

Cohort / File(s) Summary
Bug Fix
src/AbraFlexi/RW.php
Modified parseError() to validate that results is non-empty (count(...) > 0) before accessing its elements, preventing undefined array key errors on empty result sets.
Test Coverage
tests/src/AbraFlexi/ParseErrorEmptyResultsTest.php
Added PHPUnit test suite with two tests: one verifying parseError() returns 0 errors for empty results arrays, and another confirming the method still works correctly with non-empty results.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

A bunny hops through empty arrays,
No crashes, crashes, nary a fray,
With count() guards and tests so bright,
Results flow safely, pure delight! 🐰✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately summarizes the main change: fixing an undefined array key error in RW::parseError() when results is empty.
Linked Issues check ✅ Passed The PR directly addresses issue #98 by adding a count check to prevent accessing empty results array, matching the stated objective to handle empty successful responses safely.
Out of Scope Changes check ✅ Passed All changes are directly scoped to fixing the parseError() issue: one-line fix in RW.php and two unit tests validating the fix behavior.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/AbraFlexi/RW.php (1)

182-190: Pre-existing issue: else branch still assumes numeric key 0 exists.

This is outside the scope of this PR, but note that the else branch can still trigger "Undefined array key 0" if results is a non-empty associative array without numeric keys (e.g., ['someKey' => ...]). The condition \array_key_exists(0, $responseDecoded['results']) at line 170 being false leads here, yet line 183 accesses $responseDecoded['results'][0].

Consider addressing this in a follow-up if the API can return such structures.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/AbraFlexi/RW.php` around lines 182 - 190, The else branch assumes
$responseDecoded['results'][0] always exists which can cause "Undefined array
key 0"; change the logic in RW.php so you iterate over the actual elements of
$responseDecoded['results'] (e.g., foreach ($responseDecoded['results'] as
$resultsItem) { foreach ($resultsItem['result'] as $result) { ... } }) or first
normalize/check for index 0 with array_key_exists before accessing it; update
the block that currently references $responseDecoded['results'][0]['result'] and
the inner error collection (this->errors) to use the results item variable (or a
normalized single-element array) so associative keys won't trigger undefined
index errors.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/AbraFlexi/RW.php`:
- Around line 182-190: The else branch assumes $responseDecoded['results'][0]
always exists which can cause "Undefined array key 0"; change the logic in
RW.php so you iterate over the actual elements of $responseDecoded['results']
(e.g., foreach ($responseDecoded['results'] as $resultsItem) { foreach
($resultsItem['result'] as $result) { ... } }) or first normalize/check for
index 0 with array_key_exists before accessing it; update the block that
currently references $responseDecoded['results'][0]['result'] and the inner
error collection (this->errors) to use the results item variable (or a
normalized single-element array) so associative keys won't trigger undefined
index errors.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: bb7e19e1-3cc6-42f8-8923-4380daf48bbe

📥 Commits

Reviewing files that changed from the base of the PR and between 9fbe0ad and 85cb5d4.

📒 Files selected for processing (2)
  • src/AbraFlexi/RW.php
  • tests/src/AbraFlexi/ParseErrorEmptyResultsTest.php

Copy link
Copy Markdown
Collaborator

@Vitexus Vitexus left a comment

Choose a reason for hiding this comment

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

@castillo-n Thank you for your time.

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.

Undefined array key 0 in RW::parseError() when results is an empty array

2 participants