Skip to content

[4.x] Compare strings with a locale using Collator and keep the process locale - #77

Open
Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-collator
Open

Hackwar wants to merge 1 commit into
joomla-framework:4.x-devfrom
Hackwar:4.x-collator

Conversation

@Hackwar

@Hackwar Hackwar commented Nov 13, 2026

Copy link
Copy Markdown
Contributor

Summary of Changes

StringHelper::strcmp() and StringHelper::strcasecmp() with a locale call setlocale(LC_COLLATE, $locale) and never restore the previous locale. setlocale() is process-wide, not per thread, so every comparison changes the collation of unrelated code. ArrayHelper::sortObjects() calls these methods inside a usort() comparator, so the locale changes on every comparison:

setlocale(LC_COLLATE, 'de_DE.UTF-8');
StringHelper::strcmp('a', 'b', 'C');
setlocale(LC_COLLATE, '0'); // 'C'

The methods also rely on strcoll(), which is deprecated as of PHP 8.6 (#71) and will be removed in PHP 9.

With a locale, both methods now compare with Collator::compare() from ext-intl. They do not touch the process locale and no longer need strcoll() or transcode(). One collator is created per locale and cached, because sortObjects() compares many times. strcasecmp() still lowercases both strings with mb_strtolower() before it compares them.

Without ext-intl, the old setlocale()/strcoll() code runs as a fallback. It now restores the previous LC_COLLATE locale in a finally block, and it triggers a deprecation (trigger_deprecation('joomla/string', '__DEPLOY_VERSION__', …)) that asks for the intl extension. From PHP 8.6 on, the fallback compares bytes with strcmp() instead of calling the deprecated strcoll(). ext-intl is added to suggest.

Behaviour changes for callers that pass a locale:

  • Results come from ICU collation instead of the C library. For the French test data in the suite, both give the same order.
  • The locale is an ICU identifier. fr_FR, fr_FR.UTF-8 and fr_FR.utf8 all map to fr. Names that ICU does not know, such as the Windows name French_Standard, use the root collation.
  • If $locale is an array, as setlocale() accepts, the collator uses its first entry.

Testing Instructions

New tests in StringHelperTest:

  • testStrcmpWithCollator and testStrcasecmpWithCollator (with #[RequiresPhpExtension('intl')]) compare French accented strings and ligatures with fr_FR and fr_FR.UTF-8, plus strcmp('a', 'B', 'fr_FR') < 0, where a byte comparison gives > 0. Before the change, 8 of the 16 cases fail on Windows, where setlocale() does not know fr_FR. The existing testStrcmp/testStrcasecmp cases with a locale are skipped on every platform, because they pass the list of locales as a single quoted string to setlocale().
  • testComparisonWithLocaleKeepsProcessLocale sets LC_COLLATE to an available non-C locale, calls both methods with 'C', and asserts that LC_COLLATE is unchanged. It fails before the change and passes after it.

The fallback without intl was checked by hand with php -n, which has no intl but does have iconv. Both methods return the old results, trigger the deprecation, and leave LC_COLLATE at German_Germany.1252.

vendor/bin/phpunit is green, phpcs is clean, and PHPStan reports no errors locally. The PHPStan job in CI fails on 4.x-dev as well (mb_trim and the other PHP 8.4 polyfill functions are not found), and #66 fixes that.

Documentation Changes Required

docs/overview.md (still "TODO" on 4.x-dev) gets a section "Comparing strings with a locale" with the intl requirement, the locale format, and the deprecated fallback.

The owner's docs PR #72 rewrites docs/overview.md. Whichever of the two is merged second has to fold this section into #72's structure and replace the strcmp()/strcasecmp() paragraph under "Notes on individual methods", which says they use strcoll() and depend on the process-wide locale. #73 and #75 also append sections to the docs/overview.md stub, so expect small conflicts there.

Fixes #71

🤖 Generated with Claude Code

StringHelper::strcmp() and strcasecmp() with a locale called
setlocale(LC_COLLATE, $locale) and never restored the previous locale.
setlocale() is process-wide, so every comparison changed the collation
of unrelated code, including other threads under ZTS. strcoll() is also
deprecated as of PHP 8.6.

Compare with the Collator class of ext-intl instead, which neither
touches the process locale nor needs strcoll(). Without ext-intl, keep
the setlocale()/strcoll() path but restore the locale afterwards, and
deprecate it. From PHP 8.6 on, the fallback compares bytes instead of
calling the deprecated strcoll().

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Hackwar

Hackwar commented Nov 13, 2026 •

Copy link
Copy Markdown
Contributor Author

CI results: unit tests pass on 8.3, 8.4 and 8.5, and so does code style. The PHPStan job fails with Function mb_trim/mb_ltrim/mb_rtrim/mb_str_pad/mb_ucfirst not found in code this PR does not touch. The same failure happens on 4.x-dev, and #66 fixes it.

The CI images do not have ext-intl, so CI skips testStrcmpWithCollator and testStrcasecmpWithCollator (288 tests, 16 skipped instead of 14). testComparisonWithLocaleKeepsProcessLocale does run there, on the deprecated setlocale()/strcoll() fallback, and passes, so the locale restore works on Linux. The Collator path was tested locally with intl on PHP 8.3.

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.

[PHP8.6] Deprecation strcoll

1 participant