Fix undefined array key 0 in RW::parseError() when results is empty#99
Fix undefined array key 0 in RW::parseError() when results is empty#99castillo-n wants to merge 1 commit intoSpoje-NET:mainfrom
Conversation
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
📝 WalkthroughWalkthroughThis pull request fixes a bug in Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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.
🧹 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
resultsis 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
📒 Files selected for processing (2)
src/AbraFlexi/RW.phptests/src/AbraFlexi/ParseErrorEmptyResultsTest.php
Vitexus
left a comment
There was a problem hiding this comment.
@castillo-n Thank you for your time.
What's happening
When some API endpoints (like automaticke-parovani) return a successful
response with an empty
resultsarray,parseError()falls into theelse branch on line 183 and tries to access
results[0], which doesn'texist. This throws an
Undefined array key 0warning.Fix
Added a
\count($responseDecoded['results']) > 0check on line 169 sowe 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