Repository navigation
Report InvalidDocblock for a @template whose as/of/super has no bound type - #15499
shu-chen-dot-ca wants to merge 1 commit into
Conversation
✅ Performance: unchanged · Types: unchanged
How to read this table
head 35f964b · base 55885bf (merge-base) · workflow run |
|
bugbot review |
There was a problem hiding this comment.
✅ Bugbot reviewed your changes and found no new issues!
Comment @cursor review or bugbot run to trigger another review on this PR
Reviewed by Cursor Bugbot for commit 35f964b. Configure here.
psalm-bot
left a comment
There was a problem hiding this comment.
| Status | 🟡 Needs changes (1 should-fix) |
|---|---|
| Approach | ✅ Root cause: template tag parsing in TemplateTagParser::parse() |
| Correctness | ✅ No problems found: Empty bound reaches existing scanner checks; keyword-named templates unaffected. |
| Tests | |
| Docs & comments | ✅ OK: The @return description now covers an empty bound. |
TemplateTagParser::parse() read as/of/super as a modifier only when another word followed it, so @template T as became a template named T with the keyword as its description and no bound. The PR now reads the keyword as a modifier whenever it follows the name, which yields an empty bound that ClassLikeNodeScanner and FunctionLikeDocblockScanner already report as Template ... missing as type. The fix is in the parser, where the keyword was misread, and templates named of/as/super still parse because the name is shifted off first. The fix is correct; the tests add three invalid cases that follow the same path as the two re-enabled ones.
Should fix (1)
tests/AnnotationTest.php:3576-3605: The three new invalid cases follow the same code path as the two re-enabled ones.
Non-blocking suggestions (1)
tests/AnnotationTest.php:1099:templatesNamedLikeBoundKeywordsneeds only one template, e.g.@template ofwith@param of $a,@return of. All three names take the same$parts === []path, so the other two templates and their params add nothing.
Automated review generated by AI; posted by psalm-bot.
| 'classTemplateOfWithoutType' => [ | ||
| 'code' => '<?php | ||
| /** | ||
| * @template-covariant T of | ||
| */ | ||
| interface A {}', | ||
| 'error_message' => 'InvalidDocblock - src' . DIRECTORY_SEPARATOR . 'somefile.php:5:31' | ||
| . ' - Template missing as type', | ||
| ], | ||
| 'methodTemplateSuperWithoutType' => [ | ||
| 'code' => '<?php | ||
| final class A { | ||
| /** | ||
| * @psalm-template T SUPER | ||
| */ | ||
| public function foo() : void {} | ||
| }', | ||
| 'error_message' => 'InvalidDocblock - src' . DIRECTORY_SEPARATOR . 'somefile.php:6:25' | ||
| . ' - Template T missing as type', | ||
| ], | ||
| 'secondFunctionTemplateAsWithoutType' => [ | ||
| 'code' => '<?php | ||
| /** | ||
| * @template TFirst of int | ||
| * @template TSecond as | ||
| */ | ||
| function foo() : void {}', | ||
| 'error_message' => 'InvalidDocblock - src' . DIRECTORY_SEPARATOR . 'somefile.php:6:21' | ||
| . ' - Template TSecond missing as type', | ||
| ], |
There was a problem hiding this comment.
Should fix: The three new invalid cases follow the same code path as the two re-enabled ones.
The only behaviour change is the $parts !== [] check at TemplateTagParser.php:137. After that, every form ends at the same empty-bound branch: ClassLikeNodeScanner.php:620-624 for classes and FunctionLikeDocblockScanner.php:2187-2193 for functions and methods.
classTemplateOfWithoutTyperuns the same class path asnoCrashOnInvalidClassTemplateAsType.ofand-covariantwere already handled: the keyword list is unchanged and the variance comes from the tag key inparseTags().methodTemplateSuperWithoutTyperuns the sameFunctionLikeDocblockScannerpath asnoCrashOnInvalidFunctionTemplateAsType.@psalm-templateand the upper-caseSUPERwere already handled byparseTags()andstrtolower().secondFunctionTemplateAsWithoutTypechecks the per-template report, which this PR doesn't change.
Drop these three cases. To pin the message and location, move the pinned error_message strings into noCrashOnInvalidClassTemplateAsType and noCrashOnInvalidFunctionTemplateAsType, e.g. 'InvalidDocblock - src' . DIRECTORY_SEPARATOR . 'somefile.php:5:27 - Template missing as type' for the class case. Adjust the column to where A sits in that snippet.
orklah
left a comment
There was a problem hiding this comment.
Thanks for the fix, the parser change is right and the re-enabled cases fail on master for the right reason.
One problem on the class side, though: when a class template ends up with an empty bound, ClassLikeNodeScanner (around line 620) reports Template missing as type but then doesn't register the template at all, unlike FunctionLikeDocblockScanner, which falls back to mixed. Now that @template T as reaches that branch, a class using T gets a cascade of errors, and the following templates shift position:
<?php
/**
* @template-covariant U of
* @template V
*/
interface Pair {
/** @return U */ public function first(): mixed;
/** @return V */ public function second(): mixed;
}
/** @implements Pair<int, string> */
final class P implements Pair {
public function first(): int { return 1; }
public function second(): string { return 'a'; }
}On this branch, besides the expected InvalidDocblock, this reports UndefinedDocblockClass for U, TooManyTemplateParams ... expecting 1 and ImplementedReturnTypeMismatch: The inherited return type 'int' for Pair::second, because V gets U's argument. Could you register the template as mixed in that else branch ($storage->template_types[$template_name] = [$fq_classlike_name => Type::getMixed()];), as the function scanner does, and add a case like the one above that expects only InvalidDocblock?
While you're at it, the bot's point on the tests still applies: the three new invalid cases go through the same path as the two re-enabled ones, so I'd drop them and pin the message and location in the re-enabled cases instead; that new class case would replace them nicely.
Psalm accepts
@template T as(orof,super) with no bound type:TemplateTagParser::parse()only reads the keyword as a modifier when something follows it, so the dangling keyword becomes the template's description.Before
After
The parser now returns the modifier with an empty bound, which the class and function scanners already report. The same holds for
ofandsuperin any case, for the-covariant/-contravariant,@psalm-and@phpstan-forms, for methods, and for a keyword on the line after the name. Templates namedof,asorsuper(@template of) stay valid.noCrashOnInvalidClassTemplateAsTypeandnoCrashOnInvalidFunctionTemplateAsTypeare re-enabled.Fixes #13928