Skip to content

fix(json): Preserve nested empty arrays when rewriting package.json.#110

Merged
terabytesoftw merged 1 commit intomainfrom
fix_mini_4
Jan 22, 2026
Merged

fix(json): Preserve nested empty arrays when rewriting package.json.#110
terabytesoftw merged 1 commit intomainfrom
fix_mini_4

Conversation

@terabytesoftw
Copy link
Contributor

Q A
Is bugfix? ✔️
New feature?
Breaks BC?

@terabytesoftw terabytesoftw added the bug Something isn't working label Jan 22, 2026
@coderabbitai
Copy link

coderabbitai bot commented Jan 22, 2026

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Resolved issue where nested empty arrays in package.json files were not preserved during rewrites.
    • Enhanced formatting tolerance to correctly handle variations in whitespace around empty arrays and brackets.
  • Tests

    • Added test coverage for nested empty array preservation scenarios.

✏️ Tip: You can customize this high-level summary in your review settings.

Walkthrough

This PR fixes a bug where nested empty arrays in package.json files were not being preserved during rewrites. The regex pattern used to detect array keys was updated to accommodate flexible whitespace around colons and brackets, and the replacement logic was refactored to use regex-based matching instead of direct string substitution.

Changes

Cohort / File(s) Summary
Regex Pattern & Logic Updates
src/Json/JsonFormatter.php
Updated ARRAY_KEYS_REGEX constant to allow optional whitespace around colons and inside empty brackets ([ ]). Modified replaceArrayByMap() to use regex-based replacement instead of literal '[]' matching and added null-check before applying replacements to prevent null handling issues.
Test Coverage
tests/Json/JsonFileTest.php, tests/Json/JsonFormatterTest.php
Added testWritePreservesNestedEmptyArraysWithoutSpaces() in JsonFileTest to verify preservation of nested empty arrays/objects with indentation. Added testGetArrayKeysWithoutSpacesBeforeArray() in JsonFormatterTest to validate regex correctly identifies keys when arrays lack preceding spaces. Updated existing test expectations for the "contributors" field.
Changelog
CHANGELOG.md
Added entry for Bug #110 documenting the fix for preserving nested empty arrays when rewriting package.json files.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Through brackets with spaces, both narrow and wide,
Empty arrays preserved with bunny-hop pride,
Whitespace-tolerant regex to save the day,
Nested structures kept empty—hip hip hooray! 🎉
A bug fixed in hops! ✨

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately describes the main change: fixing preservation of nested empty arrays in package.json rewriting, which aligns with all file changes across JsonFormatter, tests, and documentation.
Description check ✅ Passed The description provides classification metadata (bugfix, no new feature, no BC breaks) that is relevant to the changeset purpose, though minimal in detail.

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

✨ Finishing touches
  • 📝 Generate docstrings

📜 Recent review details

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 648db91 and 7d1c4a8.

📒 Files selected for processing (4)
  • CHANGELOG.md
  • src/Json/JsonFormatter.php
  • tests/Json/JsonFileTest.php
  • tests/Json/JsonFormatterTest.php
🧰 Additional context used
🧠 Learnings (1)
📓 Common learnings
Learnt from: terabytesoftw
Repo: php-forge/support PR: 12
File: src/TestSupport.php:0-0
Timestamp: 2025-08-18T20:13:33.518Z
Learning: User terabytesoftw prefers clean trait-based designs over maintaining backward compatibility wrappers when refactoring utility classes in the php-forge/support project.
Learnt from: terabytesoftw
Repo: php-forge/actions PR: 65
File: .github/workflows/composer-require-checker.yml:80-83
Timestamp: 2025-09-28T15:12:48.345Z
Learning: The user terabytesoftw prefers using floating tags like v1 for third-party actions in GitHub workflows instead of pinning to specific commit SHAs, even when it's a security best practice to pin to immutable commits.
🧬 Code graph analysis (1)
tests/Json/JsonFormatterTest.php (1)
src/Json/JsonFormatter.php (3)
  • JsonFormatter (21-148)
  • format (68-83)
  • getArrayKeys (34-39)
🪛 PHPMD (2.15.0)
tests/Json/JsonFormatterTest.php

48-48: Avoid using static access to class '\PHPForge\Support\Assert' in method 'testFormat'. (undefined)

(StaticAccess)


48-48: Avoid using static access to class '\Foxy\Json\JsonFormatter' in method 'testFormat'. (undefined)

(StaticAccess)


75-75: Avoid using static access to class '\Foxy\Json\JsonFormatter' in method 'testGetArrayKeysWithoutSpacesBeforeArray'. (undefined)

(StaticAccess)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (1)
  • GitHub Check: phpunit / PHP 8.1-ubuntu-latest
🔇 Additional comments (6)
CHANGELOG.md (1)

13-13: Changelog entry aligns with the bugfix scope.
LGTM.

src/Json/JsonFormatter.php (2)

24-24: Regex update improves whitespace tolerance.
Looks good for handling compact and spaced JSON.


139-142: Replacement now safely handles spaced empty arrays.
Null-check + regex-based replacement is a solid robustness tweak.

tests/Json/JsonFormatterTest.php (2)

30-48: Test update matches new array-preservation behavior.
Passing ['contributors'] and expecting [] is consistent with the formatter changes.


70-76: Good coverage for compact JSON arrays.
The no-space case is a helpful regression test.

tests/Json/JsonFileTest.php (1)

166-190: Nested empty arrays are well covered here.
Assertions for arrays, objects, and indentation make this a strong regression test.

✏️ Tip: You can disable this entire section by setting review_details to false in your review settings.


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.

@codecov
Copy link

codecov bot commented Jan 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (648db91) to head (7d1c4a8).
⚠️ Report is 1 commits behind head on main.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@             Coverage Diff             @@
##                main      #110   +/-   ##
===========================================
  Coverage     100.00%   100.00%           
- Complexity       342       343    +1     
===========================================
  Files             26        26           
  Lines            786       787    +1     
===========================================
+ Hits             786       787    +1     

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

@terabytesoftw terabytesoftw merged commit 583be98 into main Jan 22, 2026
24 checks passed
@terabytesoftw terabytesoftw deleted the fix_mini_4 branch January 22, 2026 11:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant