Skip to content

fix: class-string with template contains non-object type - #11755

Merged
orklah merged 5 commits into
vimeo:6.xfrom
b2pweb:fix-class-string-array
Nov 23, 2026
Merged

orklah merged 5 commits into
vimeo:6.xfrom
b2pweb:fix-class-string-array

Conversation

@vincent4vx

Copy link
Copy Markdown
Contributor

When using class-string<T> with generic type T which has union type constraint with some non-object type (for example: @template T as array|object), psalm will raise InvalidDocblock error if the first type of the union is non-object.

When the template has no constraint, it will be asumed as mixed and ignore the error.

See: https://psalm.dev/r/2cc5078909

This fix will simply skip invalid (i.e. non-object) types in case of class like string parsing.

@psalm-github-bot

Copy link
Copy Markdown

I found these snippets:

https://psalm.dev/r/2cc5078909
<?php

/**
 * @param T|class-string<T> $p
 * @template T as array|object
 * @psalm-mutation-free
 */
function f(array|object|string $p): void
{
}

/**
 * @param T|class-string<T> $p
 * @template T
 * @psalm-mutation-free
 */
function f2(array|object|string $p): void
{
}

/**
 * @param T|class-string<T> $p
 * @template T as object|bool
 * @psalm-mutation-free
 */
function f3(bool|object|string $p): void
{
}

/**
 * @param T|class-string<T> $p
 * @template T as bool|object
 * @psalm-mutation-free
 */
function f4(bool|object|string $p): void
{
}
Psalm output (using commit 7d002e3):

ERROR: InvalidDocblock - 4:11 - Invalid templated classname 'array<array-key, mixed>' in docblock for f

ERROR: InvalidDocblock - 31:11 - Invalid templated classname 'bool' in docblock for f4

@alies-dev alies-dev 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.

🤖 The bug is real and still present on both 6.x and master, but the fix is applied to only one of the two callers of getGenericParamClass, and it makes the error report a different atomic type.

Already fixed on master? No. Repro on current master and on 6.x with @template T as array|object + @param class-string<T> $class: InvalidDocblock - Invalid templated classname 'array<array-key, mixed>'. With this PR's src/ applied to a scratch 6.x, class-string<T> works ($r is class-string<A>), so master needs the same fix.

Findings

  • TypeParser.php:~783 (getTypeFromGenericTree) is the only caller that passes allow_skip_invalid_type = true. The other caller of getGenericParamClass (the T::class branch, TypeParser.php:~430) has the same array|object problem and is not changed: with this PR applied, @template T as array|object + @return T::class still reports Invalid templated classname 'array<array-key, mixed>'. A boolean that selects the behaviour per call site is the symptom; the function itself should decide (skip non-object atomics, and throw only when nothing object-like was found), so that both syntaxes behave the same and the new parameter disappears.
  • Behaviour change for the still-invalid case: for @template T as string|bool the message used to name 'string' (first atomic) and now names 'bool' (the last one, via $last_invalid_type). Prefer reporting the first invalid type, or the whole bound, so messages do not change for existing users.
  • Test genericTypeWithoutAnyValidType (string|bool -> InvalidDocblock) already passes on the base branch, so it does not guard the new behaviour; it would only fail if the skip logic were removed entirely. Consider a case with T as array|int + T::class.

Recommendations (non-blocking)

  • Document in the PR description why a template bounded by array|object is accepted by class-string<T> (only the object part contributes).

@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 bug is real and still present on both 6.x and master, and classStringOfUnionTypeArrayOrObject does fail on 6.x and pass here. A few changes before merging:

  1. The other caller of getGenericParamClass(), the T::class branch of getTypeFromTree() (~line 415), has the same bug and is not fixed: with this branch, @template T as array|object + @return T::class still reports Invalid templated classname 'array<array-key, mixed>'. Please drop the $allow_skip_invalid_type parameter and always skip non-object atomics, so both syntaxes behave the same, and add a test for the T::class case.
  2. When no atomic is usable, report the first invalid one, as before: T as string|bool used to say 'string' and now says 'bool'.
  3. genericTypeWithoutAnyValidType already passes on 6.x, so it doesn't check the new code. Pinning the message ('error_message' => 'Invalid templated classname \'string\'') would cover point 2.

Optional: a case with a nullable bound (@template T as null|Foo, also rejected on 6.x and accepted here) would be nice.

Comment thread src/Psalm/Internal/Type/TypeParser.php Resolved
Comment thread src/Psalm/Internal/Type/TypeParser.php Resolved
- Use same implemention as for class-string on ::class syntax
- Keep same error as before when all types on the union are non-object
- Add test for union with null
@github-actions

github-actions Bot commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

⚠️ Performance: unchanged · Issues: unchanged

Project Memory (MB) Wall (s) Issues Weighted
⚠️ drupal – – – –
✅ laravel 1 006 75.5 $\color{gray}{\textsf{+1.0\%}}$ 25 554 51 698
✅ symfony 1 512 91.7 $\color{gray}{\textsf{+1.4\%}}$ 44 001 89 327
✅ doctrine-orm 260 10.5 $\color{gray}{\textsf{+0.3\%}}$ 3 538 5 602
✅ psl 570 15.2 $\color{gray}{\textsf{-0.5\%}}$ 1 583 6 572
✅ phpunit 340 10.3 $\color{gray}{\textsf{-0.2\%}}$ 1 628 1 351
✅ php-parser 199 10.7 $\color{gray}{\textsf{+0.5\%}}$ 17 186 20 675
✅ amphp-amp 52 1.6 $\color{gray}{\textsf{-1.2\%}}$ 32 79
✅ webmozarts-assert 213 3.3 $\color{gray}{\textsf{+1.2\%}}$ 577 2 705
✅ guzzle-promises 47 1.7 164 405
Total 4 199 220.4 $\color{gray}{\textsf{+0.9\%}}$ 94 263 178 414
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, then $\color{red}{\textsf{+added}}$ $\color{green}{\textsf{-removed}}$ against base.
  • Weighted: issues weighted by error level (levels 1–8 as is, always-errors 10, feature-specific and purity suggestions 0). 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.
  • 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{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 ⚠️).
  • Issues (heading): the total weighted change, increased · decreased · unchanged. Informational, never fails the check.

⚠️ drupal: base: install failed: - Root composer.json requires drupal/core 11.4.11 (exact version match: 11.4.11 or 11.4.11.0), found drupal/core[11.4.11] but these were not loaded, because they are affected by security advisories ("SA-CORE-2026-017", "SA-CORE-2026-018"). Review the advisory details above for more information.

head 2cbdaf8 · base 7c854e9 (merge-base) · workflow run

@vincent4vx
vincent4vx requested a review from orklah November 19, 2026 10:26

@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, all three points are addressed and the new cases fail on 6.x and pass here.

One last thing: ci/circleci: test-with-real-projects runs on the branch itself, which is still based on 6.x from March, and that base already fails this job (SplObjectStorage::offsetGet expects never on PHPUnit). Could you merge or rebase on current 6.x so it goes green? Then this is good to merge.

@vincent4vx
vincent4vx requested a review from orklah November 23, 2026 09:00
@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 @vincent4vx!

@orklah
orklah merged commit 9fbfcc1 into vimeo:6.x Nov 23, 2026
69 of 70 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.

3 participants