Skip to content

Use strtr - #800

Open
divinity76 wants to merge 1 commit into
InvoicePlane:developfrom
divinity76:strtr
Open

divinity76 wants to merge 1 commit into
InvoicePlane:developfrom
divinity76:strtr

Conversation

@divinity76

@divinity76 divinity76 commented Oct 14, 2026 •

Copy link
Copy Markdown

Use strtr.
2 places were using an awkward
str_replace(array_keys($replacements), array_values($replacements), $str);
construction in places where strtr() should obviously have been used
(guess the original code author was unfamiliar with strtr)

Pull Request Checklist

Please check the following steps before submitting your PR. If any items are incomplete, consider marking it as [WIP] (Work in Progress).

Checklist

  • My code follows the code formatting guidelines.
  • I have tested my changes locally. docker compose run --rm cli php artisan test --exclude-group failing,troubleshooting
  • I selected the appropriate branch for this PR.
  • I have rebased my changes on top of the selected branch.
  • I included relevant documentation updates if necessary.
  • I have an accompanying issue ID for this pull request.

Description

Provide a brief description of the changes made in this pull request.


Related Issue(s)

List any related issues or discussions. If applicable, link to an accompanying thread on the forums.
Fixes # (example)


Motivation and Context

Why was this change necessary? Does it solve a problem or improve an existing feature? If this PR fixes an open issue, link to it here.


Issue Type (Check one or more)

  • Bugfix
  • Improvement of an existing feature
  • New feature

Screenshots (If Applicable)

Attach relevant screenshots that demonstrate your changes.


Thank you for your contribution to InvoicePlane! We appreciate your time and effort.

Summary by CodeRabbit

  • Bug Fixes
    • Numbering formats now replace placeholders in a single pass, preventing inserted numbering or date values from being treated as additional placeholders. This helps generated numbers and dates remain consistent with the configured format when replacement text contains characters that match another placeholder.

Copilot AI balanced review requested due to automatic review settings October 14, 2026 15:24
@coderabbitai

coderabbitai Bot commented Oct 14, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: defaults
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 203f3582-d3e2-4770-adb5-e6472dd947ef
📥 Commits

Reviewing files that changed from the base of the PR and between 563200e and af96ec5.

📒 Files selected for processing: 2
  • Modules/Core/Models/Numbering.php
  • Modules/Core/Support/NumberGenerator/AbstractNumberGenerator.php

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

📝 Walkthrough

Two number-formatting methods now use strtr for placeholder substitution instead of str_replace.

Changes: Number formatting

Layer / File(s) Summary
Placeholder substitution
Modules/Core/Models/Numbering.php, Modules/Core/Support/NumberGenerator/AbstractNumberGenerator.php
applyFormat and replaceDatePlaceholders now use strtr to substitute placeholders. The date replacements are applied as a single translation rather than sequential replacements.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Refactor

Merge Risk: ⚪ Minimal · up to af96e

No material formatting regression is established by the inspected contracts. The change appears mergeable after normal checks.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title clearly and concisely identifies the main change: replacing existing placeholder substitutions with strtr().
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Fix all pre-merge checks with AI
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The applyFormat() change alters existing behavior for custom prefixes containing numbering tokens.

1 open finding
What changed in this PR

Replaces array-based str_replace() calls with strtr() in numbering formatting.

Changes:

  • Simplifies date-placeholder replacement.
  • Simplifies number-format replacement, but introduces a regression for token-bearing prefixes.
File Description
Modules/​Core/​Support/​NumberGenerator/​AbstractNumberGenerator.php Replaces date placeholders using strtr().
Modules/​Core/​Models/​Numbering.php Replaces numbering tokens using strtr().

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

];

return str_replace(array_keys($replacements), array_values($replacements), $format);
return strtr($format, $replacements);
@nielsdrost7

Copy link
Copy Markdown
Collaborator

@divinity76 thanks man!
I'll take a look when I can!

Keeper coming

@divinity76

divinity76 commented Oct 14, 2026 •

Copy link
Copy Markdown
Author

Hmm, I thought this was straight forward, but the AI review here have a point,

The original implementation supported partial recursive transformation, but only by accident, and only when the deeper translation label comes after the outer translation label in the $translations array table, check this:
https://3v4l.org/XlWDE#v8.4.26

@nielsdrost7 If we don't need to support recursive translation at all, then this PR is already fine.

But if we want proper recursive translation support, where it does not matter what order the translation labels are defined in the array, we can do something like

function strtr_recursive(string $str, array $table, int $maxDepth = 100): string 
{
    $depth = 0;
    while ($depth < $maxDepth) {
        $old = $str;
        $str = strtr($str, $table);
        
        if ($old === $str) {
            return $str;
        }
        ++$depth;
    }
    throw new \RuntimeException("translation exceeded max recursion depth ($maxDepth)");
}

@nielsdrost7

Copy link
Copy Markdown
Collaborator

@divinity76

If we don't need to support recursive translation at all, then this PR is already fine.

Thanks man!
I'll go with that we don't need recursive translation.

I'll take a look when I can.
Consider it merged.

@nielsdrost7

Copy link
Copy Markdown
Collaborator

@divinity76 I've added you to the organization. As soon as you accept I can add you to a project which is an overview of the 200+ issues that are to be done (to-do)

Pick the issue you like, assign yourself to it and see if you can tackle the issue you want to resolve.
It's pretty fun to do actually 😉

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.

3 participants