Repository navigation
fix: class-string with template contains non-object type - #11755
Conversation
|
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
{
} |
alies-dev
left a comment
There was a problem hiding this comment.
🤖 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 passesallow_skip_invalid_type = true. The other caller ofgetGenericParamClass(theT::classbranch,TypeParser.php:~430) has the samearray|objectproblem and is not changed: with this PR applied,@template T as array|object+@return T::classstill reportsInvalid 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|boolthe 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 withT as array|int+T::class.
Recommendations (non-blocking)
- Document in the PR description why a template bounded by
array|objectis accepted byclass-string<T>(only the object part contributes).
orklah
left a comment
There was a problem hiding this comment.
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:
- The other caller of
getGenericParamClass(), theT::classbranch ofgetTypeFromTree()(~line 415), has the same bug and is not fixed: with this branch,@template T as array|object+@return T::classstill reportsInvalid templated classname 'array<array-key, mixed>'. Please drop the$allow_skip_invalid_typeparameter and always skip non-object atomics, so both syntaxes behave the same, and add a test for theT::classcase. - When no atomic is usable, report the first invalid one, as before:
T as string|boolused to say'string'and now says'bool'. genericTypeWithoutAnyValidTypealready 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.
- 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
|
| Project | Memory (MB) | Wall (s) | Issues | Weighted |
|---|---|---|---|---|
| – | – | – | – | |
| ✅ laravel | 1 006 | 75.5 |
25 554 | 51 698 |
| ✅ symfony | 1 512 | 91.7 |
44 001 | 89 327 |
| ✅ doctrine-orm | 260 | 10.5 |
3 538 | 5 602 |
| ✅ psl | 570 | 15.2 |
1 583 | 6 572 |
| ✅ phpunit | 340 | 10.3 |
1 628 | 1 351 |
| ✅ php-parser | 199 | 10.7 |
17 186 | 20 675 |
| ✅ amphp-amp | 52 | 1.6 |
32 | 79 |
| ✅ webmozarts-assert | 213 | 3.3 |
577 | 2 705 |
| ✅ guzzle-promises | 47 | 1.7 | 164 | 405 |
| Total | 4 199 | 220.4 |
94 263 | 178 414 |
How to read this table
-
Memory: Psalm's own peak figure,
threads=1. More than +5 % fails, unless the PR changescomposer.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.
head 2cbdaf8 · base 7c854e9 (merge-base) · workflow run
orklah
left a comment
There was a problem hiding this comment.
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.
When using
class-string<T>with generic typeTwhich has union type constraint with some non-object type (for example:@template T as array|object), psalm will raiseInvalidDocblockerror 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.