1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Emit InvalidDocblock for bad static keyword in @var

Ref #623
This commit is contained in:
Matt Brown 2018-03-27 11:34:48 -04:00
parent 5783611366
commit fd9e783a37
3 changed files with 31 additions and 14 deletions

View File

@ -90,21 +90,32 @@ class AssignmentChecker
}
foreach ($var_comments as $var_comment) {
$var_comment_type = ExpressionChecker::fleshOutType(
$statements_checker->getFileChecker()->project_checker,
$var_comment->type,
$context->self,
$context->self
);
try {
$var_comment_type = ExpressionChecker::fleshOutType(
$statements_checker->getFileChecker()->project_checker,
$var_comment->type,
$context->self,
$context->self
);
$var_comment_type->setFromDocblock();
$var_comment_type->setFromDocblock();
if (!$var_comment->var_id || $var_comment->var_id === $var_id) {
$comment_type = $var_comment_type;
continue;
if (!$var_comment->var_id || $var_comment->var_id === $var_id) {
$comment_type = $var_comment_type;
continue;
}
$context->vars_in_scope[$var_comment->var_id] = $var_comment_type;
} catch (\UnexpectedValueException $e) {
if (IssueBuffer::accepts(
new InvalidDocblock(
(string)$e->getMessage(),
new CodeLocation($statements_checker->getSource(), $assign_var)
)
)) {
// fall through
}
}
$context->vars_in_scope[$var_comment->var_id] = $var_comment_type;
}
}

View File

@ -758,7 +758,7 @@ class ExpressionChecker
if ($return_type_lc === 'static' || $return_type_lc === '$this') {
if (!$static_class) {
throw new \InvalidArgumentException(
throw new \UnexpectedValueException(
'Cannot handle ' . $return_type->value . ' when $static_class is empty'
);
}
@ -766,7 +766,7 @@ class ExpressionChecker
$return_type->value = $static_class;
} elseif ($return_type_lc === 'self') {
if (!$self_class) {
throw new \InvalidArgumentException(
throw new \UnexpectedValueException(
'Cannot handle ' . $return_type->value . ' when $self_class is empty'
);
}

View File

@ -1439,6 +1439,12 @@ class AnnotationTest extends TestCase
'error_message' => 'UndefinedClass',
'error_levels' => ['LessSpecificReturnStatement', 'MoreSpecificReturnType'],
],
'badStaticVar' => [
'<?php
/** @var static */
$a = new stdClass();',
'error_message' => 'InvalidDocblock',
],
];
}
}