Skip to content

fix(types): a literal class name can be identical to a template class-string - #14365

Merged
orklah merged 3 commits into
vimeo:masterfrom
jirkace:fix/14271-template-class-string-literal
Nov 23, 2026
Merged

orklah merged 3 commits into
vimeo:masterfrom
jirkace:fix/14271-template-class-string-literal

Conversation

@jirkace

@jirkace jirkace commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

A literal class name compared with a template class-string<T> is reported as DocblockTypeContradiction, although the template can stand for that class.

Before

final class Special {}

/**
 * @template T of object
 * @param class-string<T> $name
 */
function isSpecial(string $name): bool {
    return $name === "Special"; // DocblockTypeContradiction: 'Special' does not contain class-string<T:fn-isspecial as object>
}

After

    return $name === "Special"; // no DocblockTypeContradiction (InvalidStringClass remains)

A class name that does not exist is still reported (new test).

Fixes #14271

…-string vimeo#14271

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: unchanged

Project Memory (MB) Wall (s) Issues Weighted (types) Taint Dead code
✅ laravel 1 768 97.8 $\color{gray}{\textsf{+0.9\%}}$ 25 691 52 994 682 0
✅ symfony 3 596 259.2 $\color{gray}{\textsf{-0.4\%}}$ 50 604 98 386 1 528 0
✅ doctrine-orm 380 12.0 $\color{gray}{\textsf{+2.2\%}}$ 3 476 6 143 0 0
✅ psl 617 15.2 $\color{gray}{\textsf{+0.6\%}}$ 1 375 5 612 1 0
✅ phpunit 455 19.7 $\color{gray}{\textsf{+1.0\%}}$ 1 731 3 410 18 0
✅ php-parser 226 13.4 $\color{gray}{\textsf{+0.8\%}}$ 17 118 21 193 0 0
✅ tempest 895 35.7 $\color{gray}{\textsf{+0.3\%}}$ 5 047 10 528 252 0
✅ amphp-amp 45 1.4 $\color{gray}{\textsf{-1.4\%}}$ 29 49 0 0
✅ webmozarts-assert 272 4.6 575 2 714 0 0
✅ guzzle-promises 44 1.6 $\color{gray}{\textsf{-1.3\%}}$ 155 434 2 0
✅ monolog 135 5.6 $\color{gray}{\textsf{-0.2\%}}$ 913 2 479 10 0
✅ drupal 4 839 188.1 $\color{gray}{\textsf{+0.3\%}}$ 97 575 169 202 583 0
✅ wordpress 2 038 192.0 $\color{gray}{\textsf{-6.1\%}}$ 142 681 676 628 461 1
Total 15 312 846.3 $\color{gray}{\textsf{-1.3\%}}$ 346 970 1 049 772 3 537 1
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 +10 % 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. Its wall time warns above +5 % (±5 % is noise); the other thresholds apply per project.
  • 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.

head 9a9aa62 · base 28e192b (merge-base) · workflow run

@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 fix is in the right place. Two changes:

  1. The TTemplateParamClass → new TClassString($as, $as_type) rewrite isn't needed: canLiteralNameBeClassString() already handles a TTemplateParamClass against its bound (BinaryOpAnalyzer passes it in as is for InvalidStringClass). Dropping the exclusion and passing $type1_part / $type2_part unchanged gives the same results on bounded, interface, nested (T of U) and class templates, so the rewrite and its comment can go:
if (($type1_part instanceof TClassString || $type1_part instanceof TDependentGetClass)
    && $type2_part::class === TLiteralString::class
) {
    return ClassLikeStringComparator::canLiteralNameBeClassString(
        $codebase,
        $type2_part,
        $type1_part,
        $allow_interface_equality,
    );
}

(and the same for the mirrored branch).

  1. Both tests use @template T of object, where the bound plays no role: they'd still pass if the bound were ignored. Please add a bounded case, e.g.:
'boundedTemplateClassStringIsComparedWithASubclassName' => [
    'code' => '<?php
        abstract class Base {}
        final class Child extends Base {}

        /**
         * @template T of Base
         * @param class-string<T> $name
         */
        function isChild(string $name): bool {
            return $name === "Child";
        }',
    'assertions' => [],
    'ignored_issues' => ['InvalidStringClass'],
],
'boundedTemplateClassStringIsNeverAnUnrelatedClass' => [
    'code' => '<?php
        abstract class Base {}
        final class Other {}

        /**
         * @template T of Base
         * @param class-string<T> $name
         */
        function isOther(string $name): bool {
            return $name === "Other";
        }',
    'error_message' => 'DocblockTypeContradiction',
],

…d, with bounded cases vimeo#14271

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

jirkace commented Nov 21, 2026

Copy link
Copy Markdown
Contributor Author

@orklah thanks, both done in 55a4eaf:

  1. The TTemplateParamClass → new TClassString($as, $as_type) rewrite and its comment are gone; both branches of canBeIdentical() pass $type1_part / $type2_part to canLiteralNameBeClassString() unchanged (the TTemplateParamClass import went with it).
  2. Your bounded cases are in ClassLikeStringTest: boundedTemplateClassStringIsComparedWithASubclassName (valid) and boundedTemplateClassStringIsNeverAnUnrelatedClass (DocblockTypeContradiction). They pass on the current head and also on the commit before 55a4eaf, i.e. the removed rewrite and the bound handling of canLiteralNameBeClassString() give the same result for them, as you said; they pin the bound now.

CI is green apart from label. PHPUnit suite not run locally.

@orklah

orklah commented Nov 22, 2026

Copy link
Copy Markdown
Collaborator

Thanks, both points are addressed and this is ready to merge, but it now conflicts with master after today's merges. Could you merge master (keeping both sides)? I'll merge it once CI is green.

# Conflicts:
#	tests/ClassLikeStringTest.php
@orklah orklah added the release:fix The PR will be included in 'Fixes' section of the release notes label Nov 23, 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 b692795 into vimeo:master Nov 23, 2026
116 of 118 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.

A literal class name compared with a template class-string<T> is a DocblockTypeContradiction

2 participants