From b627bdf9c6645157919baf667c1c62162e2b8b49 Mon Sep 17 00:00:00 2001 From: Matthew Brown Date: Fri, 28 Oct 2016 10:54:20 -0400 Subject: [PATCH] Catch bad docblock issue --- src/Psalm/Checker/FunctionChecker.php | 87 ++++++++++++++++----------- src/Psalm/Checker/MethodChecker.php | 73 +++++++++++++--------- 2 files changed, 98 insertions(+), 62 deletions(-) diff --git a/src/Psalm/Checker/FunctionChecker.php b/src/Psalm/Checker/FunctionChecker.php index e78f6a126..a0dae407a 100644 --- a/src/Psalm/Checker/FunctionChecker.php +++ b/src/Psalm/Checker/FunctionChecker.php @@ -167,47 +167,66 @@ class FunctionChecker extends FunctionLikeChecker $config = Config::getInstance(); $return_type = null; - $docblock_info = CommentChecker::extractDocblockInfo((string)$function->getDocComment()); + $docblock_info = null; - if ($docblock_info['deprecated']) { - self::$deprecated_functions[$file_name][$function_id] = true; + $this->suppressed_issues = []; + + try { + $docblock_info = CommentChecker::extractDocblockInfo((string)$function->getDocComment()); + } + catch (\Psalm\Exception\DocblockParseException $e) { + if (IssueBuffer::accepts( + new InvalidDocblock( + 'Invalid type passed in docblock for ' . $this->getMethodId(), + $this->getCheckedFileName(), + $function->getLine() + ) + )) { + return false; + } } - if ($docblock_info['variadic']) { - self::$variadic_functions[$file_name][$function_id] = true; - } - - $this->suppressed_issues = $docblock_info['suppress']; - - if ($function->returnType) { - $return_type = Type::parseString( - is_string($function->returnType) - ? $function->returnType - : ClassLikeChecker::getAbsoluteClassFromName($function->returnType, $this->namespace, $this->getAliasedClasses()) - ); - } - - if ($config->use_docblock_types) { - if ($docblock_info['return_type']) { - $return_type = - Type::parseString( - self::fixUpLocalType( - (string)$docblock_info['return_type'], - null, - $this->namespace, - $this->getAliasedClasses() - ) - ); + if ($docblock_info) { + if ($docblock_info['deprecated']) { + self::$deprecated_functions[$file_name][$function_id] = true; } - if ($docblock_info['params']) { - $this->improveParamsFromDocblock( - $docblock_info['params'], - $function_param_names, - self::$file_function_params[$file_name][$function_id], - $function->getLine() + if ($docblock_info['variadic']) { + self::$variadic_functions[$file_name][$function_id] = true; + } + + $this->suppressed_issues = $docblock_info['suppress']; + + if ($function->returnType) { + $return_type = Type::parseString( + is_string($function->returnType) + ? $function->returnType + : ClassLikeChecker::getAbsoluteClassFromName($function->returnType, $this->namespace, $this->getAliasedClasses()) ); } + + if ($config->use_docblock_types) { + if ($docblock_info['return_type']) { + $return_type = + Type::parseString( + self::fixUpLocalType( + (string)$docblock_info['return_type'], + null, + $this->namespace, + $this->getAliasedClasses() + ) + ); + } + + if ($docblock_info['params']) { + $this->improveParamsFromDocblock( + $docblock_info['params'], + $function_param_names, + self::$file_function_params[$file_name][$function_id], + $function->getLine() + ); + } + } } self::$function_return_types[$file_name][$function_id] = $return_type ?: false; diff --git a/src/Psalm/Checker/MethodChecker.php b/src/Psalm/Checker/MethodChecker.php index be05d73d8..745d7bf56 100644 --- a/src/Psalm/Checker/MethodChecker.php +++ b/src/Psalm/Checker/MethodChecker.php @@ -260,39 +260,56 @@ class MethodChecker extends FunctionLikeChecker } if ($doc_comment) { - $docblock_info = CommentChecker::extractDocblockInfo((string)$doc_comment); + $docblock_info = null; - if ($docblock_info['deprecated']) { - self::$deprecated_methods[$method_id] = true; + try { + $docblock_info = CommentChecker::extractDocblockInfo((string)$doc_comment); + } + catch (\Psalm\Exception\DocblockParseException $e) { + if (IssueBuffer::accepts( + new InvalidDocblock( + 'Invalid type passed in docblock for ' . $this->getMethodId(), + $this->getCheckedFileName(), + $function->getLine() + ) + )) { + return false; + } } - if ($docblock_info['variadic']) { - self::$variadic_methods[$method_id] = true; - } - - $this->suppressed_issues = $docblock_info['suppress']; - self::$method_suppress[$method_id] = $this->suppressed_issues; - - if ($config->use_docblock_types) { - if ($docblock_info['return_type']) { - $return_type = - Type::parseString( - $this->fixUpLocalType( - (string)$docblock_info['return_type'], - $this->absolute_class, - $this->namespace, - $this->getAliasedClasses() - ) - ); + if ($docblock_info) { + if ($docblock_info['deprecated']) { + self::$deprecated_methods[$method_id] = true; } - if ($docblock_info['params']) { - $this->improveParamsFromDocblock( - $docblock_info['params'], - $method_param_names, - self::$method_params[$method_id], - $method->getLine() - ); + if ($docblock_info['variadic']) { + self::$variadic_methods[$method_id] = true; + } + + $this->suppressed_issues = $docblock_info['suppress']; + self::$method_suppress[$method_id] = $this->suppressed_issues; + + if ($config->use_docblock_types) { + if ($docblock_info['return_type']) { + $return_type = + Type::parseString( + $this->fixUpLocalType( + (string)$docblock_info['return_type'], + $this->absolute_class, + $this->namespace, + $this->getAliasedClasses() + ) + ); + } + + if ($docblock_info['params']) { + $this->improveParamsFromDocblock( + $docblock_info['params'], + $method_param_names, + self::$method_params[$method_id], + $method->getLine() + ); + } } } }