Skip to content

fix(laravel): pair ParentField wildcards by depth in nested arrays - #65

Merged
parsilver merged 2 commits into
mainfrom
fix/59-parentfield-pairs-wrong-index
Nov 16, 2026
Merged

parsilver merged 2 commits into
mainfrom
fix/59-parentfield-pairs-wrong-index

Conversation

@parsilver

@parsilver parsilver commented Nov 16, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #59

What was wrong

ParentField filled each * in the parent field with the attribute's numeric segments in order. A parent with a fixed outer index, such as items.1.addresses.*.country, took the item index (1) for its wildcard, so every address in item 1 was checked against address 1's country.

The fix

Each * now takes the attribute's key at the same depth (items.1.addresses.0.admin1 gives items.1.addresses.0.country). A wildcard deeper than the attribute stays unresolved and reads as no parent, as before. Single-level pairing (addresses.*.country) and plain field names behave the same.

Acceptance

  • Failing test with nested arrays, two items with two addresses each and different countries: it pairs each wildcard with the index at the same depth in nested arrays in tests/Laravel/RulesTest.php. It failed on the first commit of this PR (it reported items.1.addresses.0.admin1 as well as items.1.addresses.1.admin1) and passes with the fix. It also covers two wildcards and a parent one level up (items.*.country).
  • Existing rule tests stay green: pairs wildcard arrays by position and the rest pass.

Checks run

  • vendor/bin/pest: 87 passed, 1 skipped (the live network test), 5569 assertions, after rebasing on main.
  • vendor/bin/phpstan analyse: no errors.
  • vendor/bin/pint --test on the changed files: passed.
  • PHPMD 2.15 (codesize, unusedcode) on ParentField.php: no violations. Cyclomatic complexity of value() is 6 and every line runs under the rule tests, so CRAP is about 6.
  • jscpd 4.0.5 on src: 14 clones, the same 14 as on main; none in ParentField.php.
  • Mutation, by hand (no Infection or coverage driver on this machine): 7 mutants on value(). 6 killed. The survivor turns $field === null || $field === '' into $field === null, which is equivalent: an empty field reads Arr::get($data, ''), which is null too.

Risk

Small. The only behaviour change is which index fills a wildcard when the parent's shape differs from the attribute's numeric segments. A parent that sits at a different path than the attribute (for example attribute addresses.2.city with parent meta.addresses.*.country) used to borrow the first index and now pairs by depth; that layout was never documented.

Not verified

  • Infection and a real coverage run, since neither is installed here; the mutation numbers above are hand-run.

A parent field such as items.1.addresses.*.country takes its wildcard from the first numeric segment of the attribute (the item index), so a row is checked against another row's country. This test fails until the pairing goes by depth.

Refs #59
Each * in the parent field now takes the attribute's key at the same depth. Filling wildcards with numeric segments in order put the outer index into the inner wildcard whenever the parent had a fixed outer index, so items.1.addresses.*.country read item 1's address 1 for every address.

Closes #59
@parsilver
parsilver marked this pull request as ready for review November 16, 2026 11:11
@codecov

codecov Bot commented Nov 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 83.33%. Comparing base (e80e3a9) to head (35e05ab).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main      #65      +/-   ##
============================================
+ Coverage     83.23%   83.33%   +0.10%     
- Complexity      532      533       +1     
============================================
  Files            43       43              
  Lines          1145     1146       +1     
============================================
+ Hits            953      955       +2     
+ Misses          192      191       -1     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@parsilver parsilver left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

No Blocker.

ParentField::value now replaces each * in the parent path with the attribute's key at the same depth, instead of taking the attribute's numeric indexes left to right. So items.1.addresses.0.admin1 with parent items.*.addresses.*.country reads items.1.addresses.0.country, and a parent shallower than the attribute (items.*.country) reads its own row's value. A * with no key at that depth stays and reads null, as before. The new test uses two items with two addresses each and different countries, and fails against the old pairing. It also covers the shallower-parent case and the null and empty parent. CI green on PHP 8.3 to 8.5, prefer-lowest and prefer-stable, codecov patch included.

Nit: the class doc could say the parent path is assumed to share the attribute's prefix up to each wildcard (true for every rule this package documents).

Merging; this ships as 3.0.1.

@parsilver
parsilver merged commit 77a8409 into main Nov 16, 2026
20 checks passed
@parsilver
parsilver deleted the fix/59-parentfield-pairs-wrong-index branch November 16, 2026 11:20
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.

fix(laravel): ParentField pairs the wrong index for nested wildcard parents

1 participant