From 6bd7f5b867d34df400b82e0788d185d56ebee2ff Mon Sep 17 00:00:00 2001 From: Bruce Weirdan Date: Mon, 29 Mar 2021 22:10:04 +0300 Subject: [PATCH] Forbid template annotation on closures (#5499) They don't work properly anyway. Fixes vimeo/psalm#5472 --- .../Reflector/FunctionLikeNodeScanner.php | 12 ++++++++++++ tests/ClosureTest.php | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php index 525de0e29..1758736b4 100644 --- a/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php +++ b/src/Psalm/Internal/PhpVisitor/Reflector/FunctionLikeNodeScanner.php @@ -455,6 +455,18 @@ class FunctionLikeNodeScanner } } + if ($stmt instanceof PhpParser\Node\Expr\Closure + || $stmt instanceof PhpParser\Node\Expr\ArrowFunction + ) { + if ($docblock_info->templates !== []) { + $docblock_info->templates = []; + $storage->docblock_issues[] = new InvalidDocblock( + 'Templated closures are not supported', + new CodeLocation($this->file_scanner, $stmt, null, true) + ); + } + } + FunctionLikeDocblockScanner::addDocblockInfo( $this->codebase, $this->file_scanner, diff --git a/tests/ClosureTest.php b/tests/ClosureTest.php index 5e464c1df..6049607db 100644 --- a/tests/ClosureTest.php +++ b/tests/ClosureTest.php @@ -926,6 +926,23 @@ class ClosureTest extends TestCase false, '7.4' ], + 'forbidTemplateAnnotationOnClosure' => [ + ' 'InvalidDocblock', + ], + 'forbidTemplateAnnotationOnShortClosure' => [ + ' false; + ', + 'error_message' => 'InvalidDocblock', + [], + false, + '7.4' + ], ]; } }