From 73770c085e6403f04ad05c55bb843109d34e310b Mon Sep 17 00:00:00 2001 From: Brown Date: Tue, 8 Jan 2019 15:11:57 -0500 Subject: [PATCH] Prevent crash when @template as type is missing --- .../Internal/Visitor/ReflectorVisitor.php | 52 +++++++++++++------ tests/AnnotationTest.php | 16 ++++++ 2 files changed, 52 insertions(+), 16 deletions(-) diff --git a/src/Psalm/Internal/Visitor/ReflectorVisitor.php b/src/Psalm/Internal/Visitor/ReflectorVisitor.php index 573cf9069..e65ae12d9 100644 --- a/src/Psalm/Internal/Visitor/ReflectorVisitor.php +++ b/src/Psalm/Internal/Visitor/ReflectorVisitor.php @@ -717,14 +717,24 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse foreach ($docblock_info->templates as $template_type) { if (count($template_type) === 3) { - $storage->template_types[$template_type[0]] = Type::parseTokens( - Type::fixUpLocalType( - $template_type[2], - $this->aliases, - null, - $this->type_aliases - ) - ); + if (trim($template_type[2])) { + $storage->template_types[$template_type[0]] = Type::parseTokens( + Type::fixUpLocalType( + $template_type[2], + $this->aliases, + null, + $this->type_aliases + ) + ); + } else { + if (IssueBuffer::accepts( + new InvalidDocblock( + 'Template missing as type', + new CodeLocation($this->file_scanner, $node, null, true) + ) + )) { + } + } } else { $storage->template_types[$template_type[0]] = Type::getMixed(); } @@ -1337,14 +1347,24 @@ class ReflectorVisitor extends PhpParser\NodeVisitorAbstract implements PhpParse foreach ($docblock_info->templates as $template_type) { if (count($template_type) === 3) { - $storage->template_types[$template_type[0]] = Type::parseTokens( - Type::fixUpLocalType( - $template_type[2], - $this->aliases, - null, - $this->type_aliases - ) - ); + if (trim($template_type[2])) { + $storage->template_types[$template_type[0]] = Type::parseTokens( + Type::fixUpLocalType( + $template_type[2], + $this->aliases, + null, + $this->type_aliases + ) + ); + } else { + if (IssueBuffer::accepts( + new InvalidDocblock( + 'Template missing as type', + new CodeLocation($this->file_scanner, $stmt, null, true) + ) + )) { + } + } } else { $storage->template_types[$template_type[0]] = Type::getMixed(); } diff --git a/tests/AnnotationTest.php b/tests/AnnotationTest.php index f67a51bd6..3d6bc5ee6 100644 --- a/tests/AnnotationTest.php +++ b/tests/AnnotationTest.php @@ -1137,6 +1137,22 @@ class AnnotationTest extends TestCase }', 'error_message' => 'InvalidDocblock', ], + 'noCrashOnInvalidClassTemplateAsType' => [ + ' 'InvalidDocblock', + ], + 'noCrashOnInvalidFunctionTemplateAsType' => [ + ' 'InvalidDocblock', + ], ]; } }