Skip to content

🐛 fix(helpers): wpbones_modules() loads a module whose name ends with 'p' or 'h' - #144

Merged
gfazioli merged 1 commit into
masterfrom
fix/modules-suffix
Nov 23, 2026
Merged

gfazioli merged 1 commit into
masterfrom
fix/modules-suffix

Conversation

@gfazioli

@gfazioli gfazioli commented Nov 23, 2026 •

Copy link
Copy Markdown
Collaborator

What was wrong

wpbones_modules() (and its alias import()) built the file name with rtrim($file, '.php') . '.php'. The second argument of rtrim() is a set of characters, not a suffix, so every trailing ., p and h was stripped: wpbones_modules('shop') required plugin/modules/sho.php, 'graph' required gra.php, 'Help.php' required Hel.php, and each ended in a fatal "Failed opening required". Reported by @bardrop in #142. The bug dates back to the helper's introduction (47a7904, 1.8).

What changed

  • src/helpers.php: a bytewise str_ends_with($file, '.php') check appends .php only when it is missing. Not Str::finish(): its /u regex makes preg_replace() return null on a name that is not valid UTF-8, and the helper would require plugin/modules/.php (Codex review, round 1).
  • tests/Unit/ModulesHelperTest.php: five names through both wpbones_modules() and import() (ends with p, ends with h, Help.php, and two that end with neither as guards).

Proof

composer test: 373 tests, 1034 assertions, green. The 3 deprecations are the existing setAccessible() calls in RestRouteTest/ViewAssetRoutingTest on local PHP 8.6, untouched here (CI runs 8.1–8.4). The new test on the unfixed helper: 3 of 5 cases error (shop, graph, Help.php).

Live in WPKirk-Developing (.claude/scripts/modules-live-smoke.sh: five modules written into the bench, the bench active alone, the calls run through WP-CLI, debug.log watched):

On v3.3.0 (63c99a7), red:

  ✓ wpbones_modules() comes from the bench: WPKirk-Developing/vendor/wpbones/wpbones/src/helpers.php
  ✗ wpbones_modules('shop') → '<fatal>', expected 'shop'
  ✗ wpbones_modules('map') → '<fatal>', expected 'map'
  ✗ wpbones_modules('graph') → '<fatal>', expected 'graph'
  ✗ wpbones_modules('Help.php') → '<fatal>', expected 'Help'
  ✓ wpbones_modules('config') → 'config'
  ✓ wpbones_modules('config.php') → 'config'
  ✗ import('map') → '<fatal>', expected 'map'
  ✗ debug.log: 10 new PHP error lines   (Failed opening required '…/plugin/modules/sho.php', '…/ma.php', …)

On this branch (542a67c, the same tree as the squashed head 0ba5e05), green:

  ✓ wpbones_modules() comes from the bench: WPKirk-Developing/vendor/wpbones/wpbones/src/helpers.php
  ✓ wpbones_modules('shop') → 'shop'
  ✓ wpbones_modules('map') → 'map'
  ✓ wpbones_modules('graph') → 'graph'
  ✓ wpbones_modules('Help.php') → 'Help'
  ✓ wpbones_modules('config') → 'config'
  ✓ wpbones_modules('config.php') → 'config'
  ✓ import('map') → 'map'
  ✓ debug.log: no new PHP error from the plugin
✓ modules smoke passed on 542a67c

Fixes #142

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues block approval of the module filename handling fix.

No open issues 🎉

What changed in this PR

Fixes #142 by correcting module filename handling in wpbones_modules() and its import() alias, so names ending in p or h load the intended file.

Changes:

  • Replaces character-based trimming with Str::finish() to ensure the .php suffix without truncating module names.
  • Adds five regression cases covering both helpers, including names with and without the extension.
File Description
tests/​Unit/​ModulesHelperTest.php Tests module loading through both helpers for affected names and control cases.
src/​helpers.php Uses Str::finish() to construct the module filename correctly.

Review effort: Balanced

… 'p' or 'h'

rtrim($file, '.php') strips any trailing '.', 'p' and 'h', so wpbones_modules('shop')
required plugin/modules/sho.php. A bytewise str_ends_with() check appends '.php' only
when it is missing (not Str::finish(): its /u regex turns a name that is not valid UTF-8
into '.php'). import() is an alias and is fixed with it.

Fixes #142
@gfazioli
gfazioli merged commit 0ba5e05 into master Nov 23, 2026
8 checks passed
@gfazioli
gfazioli deleted the fix/modules-suffix branch November 23, 2026 10:42
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.

wpbones_modules() cannot load a module whose name ends with 'p' or 'h'

2 participants