Repository navigation
Only insert at the first occurrence of the placement anchor - #66
Conversation
`WPConfigTransformer::add()` used `str_replace()` to swap the anchor for the anchor plus the new config. `str_replace()` replaces every occurrence, so a `wp-config.php` in which the anchor string appears more than once — for instance a duplicated `/* That's all, stop editing! */` line left behind by a provisioning script or a merged config fragment — got the config defined once per occurrence, triggering `Constant XXX already defined` notices. Use `substr_replace()` at the offset already computed for the anchor lookup so only the first occurrence is replaced. See wp-cli/config-command#231 Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NU3fkN1bbTGQXuTFWBsBcr
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (3)
Included review availability: Your plan provides up to 1 included review per hour; 0 remain after this review. 📝 WalkthroughWalkthrough
ChangesAnchor replacement behavior
Estimated code review effort: 2 (Simple) | ~10 minutes Merge Risk: ⚪ Minimal · up to The change prevents duplicate configuration entries when the placement anchor appears more than once while preserving existing behavior for normal configurations and missing anchors. No actionable merge-blocking risk remains beyond normal checks and review. Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
Full details: Docstring CoverageExplanation Docstring coverage is 22.22% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 9 functions across 2 files. (1 skipped: 1 unsupported.)
✨ Finishing Touches 💡 2📝 Generate docstrings 💡
🛠️ Fix failing CI checks 💡
🧪 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 |
The added inline comment splits the assignment block, so the two `$new_src` assignments form their own alignment group. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NU3fkN1bbTGQXuTFWBsBcr
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Fixes wp-cli/config-command#231.
Problem
WPConfigTransformer::add()replaced the placement anchor usingstr_replace():str_replace()replaces every occurrence. So if the anchor string appears more than once inwp-config.php— e.g. a/* That's all, stop editing! */line duplicated by a provisioning script, a copy/paste edit, or a merged config fragment —wp config add/wp config setinserted thedefine()once per occurrence, and WordPress then emittedConstant XXX already definednotices.Running
wp config add FOO baragainst such a file produced:Fix
Reuse the offset from the anchor lookup that
add()already performs, and replace only that one occurrence withsubstr_replace():The config is now inserted exactly once, at the first occurrence, for both
beforeandafterplacement. Behavior is unchanged for the common single-anchor case, for theEOFanchor, and for a missing anchor (which still throwsUnable to locate placement anchor.).Tests
New
tests/DuplicateAnchorTest.phpbuilds awp-config.phpwhose anchor comment appears twice and covers:beforeplacement lands at the first anchor, not the lastafterplacement lands at the first anchor and inserts onceThe three duplication assertions fail against
mainand pass with this change, so they guard the regression rather than passing either way.Note: this only affects
add().update()matches on the existing config source rather than the anchor, so it was not affected.Follow-up
wp-cli/config-command pins
wp-cli/wp-config-transformer: ^1.4.0, so config-command picks this up once a new release is tagged; no change is needed there.🤖 Generated with Claude Code
https://claude.ai/code/session_01NU3fkN1bbTGQXuTFWBsBcr