Skip to content

[5.x] Add native types to StringHelper and Normalise - #87

Open
Hackwar wants to merge 2 commits into
joomla-framework:5.x-devfrom
Hackwar:5.x-native-types
Open

Hackwar wants to merge 2 commits into
joomla-framework:5.x-devfrom
Hackwar:5.x-native-types

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

The StringHelper and Normalise API is untyped, and optional integer arguments use false as a sentinel (strpos() $offset = false, substr() $length = false). This PR adds native parameter and return types to every public method of both classes and to StringHelper::$incrementStyles, and replaces the sentinels with nullable integers:

  • strpos(string, string, ?int $offset = null): int|false, substr(string, int, ?int $length = null): string, and substr_replace(..., ?int $length = null). strcspn()/strspn() take ?int $start/$length, and strcspn() passes $start ?? 0 to mb_substr().
  • str_ireplace(string|array, string|array, string, ?int $count = null): string.
  • strcmp()/strcasecmp() take string|false $locale = false. ltrim()/rtrim()/trim() keep string|false $charlist = false, because coercing false to '' would silently disable trimming.
  • Return types follow the wrapped functions: ord(), strpos(), strrpos(), stristr() and transcode() can return false. strtolower(), strtoupper() and substr() return string.
  • The docblocks match the native types (PHPStan checks this).

This is a breaking change. Without strict_types, an explicit false/true is coerced to 0/1 for the new ?int parameters, which changes results silently (substr($s, 1, false) now returns ''). The update guide lists these cases.

Testing Instructions

The existing tests pass null instead of the false/true sentinels (12 data rows in StringHelperTest). Otherwise the suite is unchanged and passes. phpcs is clean, and PHPStan reports no errors locally. The PHPStan job in CI fails on 4.x-dev/5.x-dev as well (mb_trim and the other PHP 8.4 polyfill functions are not found), and #66 fixes that on 4.x.

Documentation Changes Required

docs/v4-to-v5-update.md: a section "Native parameter and return types" with the sentinel replacements and the coercion pitfalls.

Depends on #84 (this branch is built on it, so its commit shows up here until #84 is merged). Related to #73, #75, #76, #77, #78, #79 and #81 on 4.x, which change bodies and docblocks of the same methods. Expect conflicts on the signature and docblock lines when 4.x-dev is merged into 5.x-dev. In particular, #73 rewrites the $count docblock of str_ireplace() (keep its wording with the integer|null type here), and #77 rewrites strcmp()/strcasecmp(). Related to #85 and #86, which also append to docs/v4-to-v5-update.md.

🤖 Generated with Claude Code

Hackwar and others added 2 commits November 13, 2026 01:25
Several 5.x changes need an entry in the update guide. Creating the
file once keeps them from conflicting over its creation.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The API was untyped and used false as a sentinel for optional integer
arguments (strpos() $offset, substr() $length). Add native parameter
and return types, replace the sentinels with nullable integers, and
document the changes, including the silent coercion of false/true to
0/1, in the 5.0 update guide.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.

1 participant