PathException with "The path does not apply to this data!" #79
PathException with "The path does not apply to this data!" #79aevent-dev2 wants to merge 1 commit into
Conversation
…s like {"+1": ...} because filter_var('+1', FILTER_VALIDATE_INT) is 1.
There was a problem hiding this comment.
AI Code Review by LlamaPReview
🎯 TL;DR & Recommendation
Recommendation: Approve with suggestions
This PR fixes a bug where filter_var('+1', FILTER_VALIDATE_INT) incorrectly returns integer 1, causing a PathException for JSON objects with keys like "+1". However, the fix fails to handle keys starting with '-' (e.g., -1), which could lead to similar path resolution issues.
📄 Documentation Diagram
This diagram illustrates the JSON pointer path resolution logic with the new sign-prefixed key handling.
sequenceDiagram
participant U as User
participant JP as JsonPointer
participant D as Data
U->>JP: resolve(path)
loop each path item
JP->>JP: filter_var(key, FILTER_VALIDATE_INT)
alt intKey is false
JP->>D: access as string key
else intKey is not false
JP->>JP: check for leading '+' or '-'?
note over JP: PR #35;79: added check for sign prefix<br/>to avoid misinterpreting signed integer keys
alt hasSign
JP->>JP: set intKey = false
JP->>D: access as string key
else
JP->>D: access as integer key
end
end
end
🌟 Strengths
- Includes a test case covering both positive and negative signed integer-like keys.
- Directly addresses a real-world bug reported by a user.
💡 Suggestions (P2)
- src/JsonPointer.php: Extend the sign-prefix check to include both
'+'and'-'to preventfilter_var('-1', FILTER_VALIDATE_INT)from returning-1, which could cause path exceptions for objects with negative integer keys.
💡 Have feedback? We'd love to hear it in our GitHub Discussions.
✨ This review was generated by LlamaPReview Advanced, which is free for all open-source projects. Learn more.
| // Avoid PathException with objects like {"+1": ...} | ||
| $hasPlus = $intKey !== false && | ||
| is_string($key) && | ||
| $key !== '' && | ||
| $key[0] === '+'; | ||
| if ($hasPlus) { | ||
| $intKey = false; | ||
| } |
There was a problem hiding this comment.
P2 | Confidence: Medium
The fix correctly prevents filter_var('+1', FILTER_VALIDATE_INT) from returning integer 1, which would otherwise cause a PathException when the target data is an object (or an associative array treated as object). However, the same problem exists for keys that start with '-' (e.g., -1), because filter_var('-1', FILTER_VALIDATE_INT) returns integer -1. This integer can also lead to incorrect path resolution when the data is an object (e.g., $ref[-1] fails on a stdClass and may behave wrongly on arrays with string keys). The PR’s test includes a -1 scenario but does not confirm that the fix works for it; without handling the '-' prefix, the test may fail or behave inconsistently in certain data contexts (e.g., when the actual data is an object or an array with string keys). The impact is moderate: users with JSON objects whose keys are negative integers will still experience the original bug. A more complete fix should also set $intKey = false for keys starting with '-' (or any sign prefix followed by digits).
Code Suggestion:
// Avoid PathException with objects like {"+1": ...} or {"-1": ...}
$hasSign = $intKey !== false &&
is_string($key) &&
$key !== '' &&
($key[0] === '+' || $key[0] === '-');
if ($hasSign) {
$intKey = false;
}
The key in
{"+1": ...}gets cast to 1.