Skip to content

Commit

Permalink
Forbid template annotation on closures
Browse files Browse the repository at this point in the history
They don't work properly anyway.

Fixes vimeo#5472
  • Loading branch information
weirdan committed Mar 29, 2021
1 parent d57dde0 commit 0a036b6
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,18 @@ public function start(PhpParser\Node\FunctionLike $stmt, bool $fake_method = fal
}
}

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,
Expand Down
17 changes: 17 additions & 0 deletions tests/ClosureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,23 @@ function () {
false,
'7.4'
],
'forbidTemplateAnnotationOnClosure' => [
'<?php
/** @template T */
function (): void {};
',
'error_message' => 'InvalidDocblock',
],
'forbidTemplateAnnotationOnShortClosure' => [
'<?php
/** @template T */
fn(): bool => false;
',
'error_message' => 'InvalidDocblock',
[],
false,
'7.4'
],
];
}
}

0 comments on commit 0a036b6

Please sign in to comment.