Skip to content

fix(stubs): strtolower and mb_strtolower of string literals are the lowercased literals - #14321

Merged
orklah merged 5 commits into
vimeo:masterfrom
jirkace:fix/14258-strtolower-literals
Nov 22, 2026
Merged

orklah merged 5 commits into
vimeo:masterfrom
jirkace:fix/14258-strtolower-literals

Conversation

@jirkace

@jirkace jirkace commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

strtolower() and mb_strtolower() of string literals return non-empty-lowercase-string instead of the lowercased literals.

Before

<?php
/** @return 'Interface'|'Trait'|'Enum'|'Class' */
function kind(): string {
    return 'Class';
}

/** @param 'interface'|'trait'|'enum'|'class' $kind */
function check(string $kind): void {}

check(strtolower(kind())); // ArgumentTypeCoercion: expects 'class'|'enum'|'interface'|'trait', but parent type non-empty-lowercase-string provided

After

check(strtolower(kind())); // no issue ('interface'|'trait'|'enum'|'class')

Fixes #14258

…owercased literals vimeo#14258

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@github-actions

github-actions Bot commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

✅ Performance: unchanged · Types: increased

Project Memory (MB) Wall (s) Issues Weighted (types) Taint
✅ laravel 1 751 71.7 $\color{gray}{\textsf{-0.2\%}}$ 25 908 53 462 682
✅ symfony 3 600 164.4 $\color{gray}{\textsf{+5.5\%}}$ 50 612 $\color{red}{\textsf{+1}}$ $\color{gray}{\textsf{-0}}$ 98 409 $\color{red}{\textsf{+4}}$ 1 528
✅ doctrine-orm 381 21.7 $\color{gray}{\textsf{+0.4\%}}$ 3 481 6 143 0
✅ psl 617 27.2 $\color{gray}{\textsf{-0.9\%}}$ 1 375 5 615 1
✅ phpunit 455 20.5 $\color{gray}{\textsf{+2.4\%}}$ 1 731 3 410 18
✅ php-parser 226 13.7 $\color{gray}{\textsf{-1.4\%}}$ 17 132 21 025 0
✅ tempest 895 38.3 $\color{gray}{\textsf{+1.0\%}}$ 5 047 10 528 252
✅ amphp-amp 68 1.9 $\color{gray}{\textsf{+0.5\%}}$ 29 49 0
✅ webmozarts-assert 272 4.7 $\color{gray}{\textsf{-1.3\%}}$ 577 2 720 0
✅ guzzle-promises 44 1.6 $\color{gray}{\textsf{+1.3\%}}$ 155 434 2
✅ monolog 135 5.7 $\color{gray}{\textsf{-3.7\%}}$ 913 2 479 10
✅ drupal 4 841 237.9 $\color{gray}{\textsf{+0.1\%}}$ 97 591 169 293 583
Total 13 285 609.2 $\color{gray}{\textsf{+1.5\%}}$ 204 551 $\color{red}{\textsf{+1}}$ $\color{gray}{\textsf{-0}}$ 373 567 $\color{red}{\textsf{+4}}$ 3 076
How to read this table
  • Memory: Psalm's own peak figure, threads=1. More than +5 % fails, unless the PR changes composer.json.
  • Wall: base and head run on the same runner; ±10 % is noise. Above +20 % warns, never fails. re-run: a reversed second run was taken and the faster kept.
  • Issues: head count over all categories, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted (types): type issues weighted by error level (levels 1–8 as is, always-errors 10, strictness checks 1). Taint and dead-code issues are not weighed. The projects' own code is pinned and their dependencies resolve identically for base and head, so a change means Psalm reports more, fewer or differently serious issues. A rise is not necessarily a regression.
  • Taint: head count of Tainted* issues, then $\color{orange}{\textsf{+added}}$ $\color{orange}{\textsf{-removed}}$ against base. Both are orange as either direction needs review: more can be new detection or false positives, fewer can be fixed false positives or lost detection.
  • Dead code: head count of unused-code issues, shown only when a run reported some (findUnusedCode). More is red, fewer is green.
  • Total: sum over the projects compared on both sides. Thresholds apply per project, not to the total.
  • Colors: $\color{red}{\textsf{red}}$ past a fail or warn threshold (or more issues), $\color{green}{\textsf{green}}$ an improvement past noise (or fewer issues), $\color{orange}{\textsf{orange}}$ needs review, $\color{gray}{\textsf{gray}}$ noise.
  • Performance: regressed if a head run crashed or timed out or a memory or wall delta is red, else improved if one is green, else unchanged. Its icon is the check result: ❌ fail · ⚠️ warn · ✅ pass; the icon next to each project is that project's result, 💥 = not comparable: base crashed or timed out (counts as ⚠️).
  • Types (heading): direction of the total weighted change, increased · decreased · unchanged. Taint and Dead code (heading): added and removed counts, present only when non-zero. Informational, never fails the check.
✅ symfony: issues +1 −0

Memory 3 600 → 3 600 MB (<0.1 %) · Wall 155.8 → 164.4 s (+5.5 %)

Weighted 98 405 → 98 409 (+4, <0.01 %): L4 +1

Issue type added removed
TypeDoesNotContainType 1 0

Added (1)

  • src/Symfony/Component/HttpFoundation/Cookie.php:299 TypeDoesNotContainType (error): Type null for $sameSite is always null, so its negation can never be true

head bb13eb6 · base c5f4dfd (merge-base) · workflow run

@jirkace

jirkace commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Behaviour change

  • strtolower() and mb_strtolower() (without an encoding argument) of string literals are the lowercased literals ('interface'|'trait') instead of non-empty-lowercase-string; other inputs are unchanged. strtoupper() and the other case functions are not covered.
  • Benchmarks (local, psalm_full.xml, --threads=1, this branch vs master): laravel 28176 vs 28176, no difference; symfony one added line, HttpFoundation/Cookie.php:299 (TypeDoesNotContainType), where after strtolower of 'lax'|'strict'|'none' the following in_array check is now known to always hold. Not measured: other projects.
  • strtolowerEmptiness, strtolowerOfAStringableObjectIsNotRedundant, mb_strtolowerProducesStringWithSecondArgument and the mb_strtolower StubTest cases ran alone and pass. Not run: the full suite (CI).

…o#14258

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@jirkace

jirkace commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Pushed c4b9663, a test fix (no source change), for the CI failure of FunctionCallTest case mb_strtolowerProducesLowercaseStringWithNullOrAbsentEncoding (shard 6, all PHP versions), which I had missed (I ran the other strtolower cases, not this one).

  • It pinned mb_strtolower("AAA") as non-empty-lowercase-string and mb_strtolower("") as lowercase-string; with this PR they are the lowercased literals 'aaa' and '', so I changed those four expectations ($e, $f, $g, $h). The non-literal ones ($a to $d) are unchanged.
  • Run alone: that case passes. Not run: the full suite (CI).

# Conflicts:
#	tests/FunctionCallTest.php

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks, the direction looks right to me. One issue:

lowercaseLiterals() builds new Union($literals), which drops the argument's from_docblock. So a
defensive check after strtolower() of a docblock-typed parameter is now reported as a hard
TypeDoesNotContainType instead of DocblockTypeContradiction. That's the symfony +1 in the perf report
(Cookie.php:299, whose $sameSite is @param self::SAMESITE_*|''|null):

/** @param 'A'|'B' $k */
function eq(string $k): void {
    $l = strtolower($k);
    if ($l === 'c') {}  // TypeDoesNotContainType, expected DocblockTypeContradiction
    $m = $k;
    if ($m === 'c') {}  // DocblockTypeContradiction
}

return new Union($literals, ['from_docblock' => $type->from_docblock]); fixes it (checked locally). Please
add an invalid-code case for it, e.g.:

'strtolowerOfDocblockLiteralsKeepsFromDocblock' => [
    'code' => '<?php
        /** @param \'A\'|\'B\' $k */
        function f(string $k): void {
            $l = strtolower($k);
            if ($l === \'c\') {}
        }',
    'error_message' => 'DocblockTypeContradiction',
],

jirkace and others added 2 commits November 21, 2026 16:20
@jirkace

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

Pushed bb13eb6. lowercaseLiterals() keeps from_docblock of the argument. Invalid-code cases: strtolowerOfDocblockLiteralsKeepsFromDocblock and the mb_strtolower twin (both give TypeDoesNotContainType without the change), and strtolowerOfNativeLiteralsIsStillATypeContradiction for a literal that is not from a docblock. @orklah

@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 22, 2026

@orklah orklah left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @jirkace!

@orklah
orklah merged commit 1b5d5df into vimeo:master Nov 22, 2026
71 of 72 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release:fix The PR will be included in 'Fixes' section of the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

strtolower() on a union of string literals returns non-empty-lowercase-string instead of the lowercased literals

2 participants