Repository navigation
Conversation
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>
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 The CI images do not have |
This was referenced Nov 13, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary of Changes
StringHelper::strcmp()andStringHelper::strcasecmp()with a locale callsetlocale(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 ausort()comparator, so the locale changes on every comparison: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()fromext-intl. They do not touch the process locale and no longer needstrcoll()ortranscode(). One collator is created per locale and cached, becausesortObjects()compares many times.strcasecmp()still lowercases both strings withmb_strtolower()before it compares them.Without
ext-intl, the oldsetlocale()/strcoll()code runs as a fallback. It now restores the previousLC_COLLATElocale in afinallyblock, 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 withstrcmp()instead of calling the deprecatedstrcoll().ext-intlis added tosuggest.Behaviour changes for callers that pass a locale:
fr_FR,fr_FR.UTF-8andfr_FR.utf8all map tofr. Names that ICU does not know, such as the Windows nameFrench_Standard, use the root collation.$localeis an array, assetlocale()accepts, the collator uses its first entry.Testing Instructions
New tests in
StringHelperTest:testStrcmpWithCollatorandtestStrcasecmpWithCollator(with#[RequiresPhpExtension('intl')]) compare French accented strings and ligatures withfr_FRandfr_FR.UTF-8, plusstrcmp('a', 'B', 'fr_FR') < 0, where a byte comparison gives> 0. Before the change, 8 of the 16 cases fail on Windows, wheresetlocale()does not knowfr_FR. The existingtestStrcmp/testStrcasecmpcases with a locale are skipped on every platform, because they pass the list of locales as a single quoted string tosetlocale().testComparisonWithLocaleKeepsProcessLocalesetsLC_COLLATEto an available non-Clocale, calls both methods with'C', and asserts thatLC_COLLATEis 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 leaveLC_COLLATEatGerman_Germany.1252.vendor/bin/phpunitis green, phpcs is clean, and PHPStan reports no errors locally. The PHPStan job in CI fails on4.x-devas well (mb_trimand the other PHP 8.4 polyfill functions are not found), and #66 fixes that.Documentation Changes Required
docs/overview.md(still "TODO" on4.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 thestrcmp()/strcasecmp()paragraph under "Notes on individual methods", which says they usestrcoll()and depend on the process-wide locale. #73 and #75 also append sections to thedocs/overview.mdstub, so expect small conflicts there.Fixes #71
🤖 Generated with Claude Code