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

Add void variable detection

This commit is contained in:
Matthew Brown 2016-04-20 11:55:26 +01:00
parent 2e6f518e48
commit c770146bac
4 changed files with 14 additions and 11 deletions

View File

@ -52,12 +52,6 @@ class ClassChecker implements StatementsSource
$leftover_stmts = [];
try {
new ReflectionMethod($this->_absolute_class . '::__get');
$this->_has_custom_get = true;
} catch (ReflectionException $e) {}
$method_checkers = [];
self::$_class_methods[$this->_absolute_class] = [];
@ -77,6 +71,10 @@ class ClassChecker implements StatementsSource
}
}
if (method_exists($this->_absolute_class, '__get')) {
$this->_has_custom_get = true;
}
if ($leftover_stmts) {
$scope_vars = [];
$possibly_in_scope_vars = [];

View File

@ -12,6 +12,7 @@ use CodeInspector\Exception\ParentNotFoundException;
use CodeInspector\Exception\ScopeException;
use CodeInspector\Exception\StaticInvocationException;
use CodeInspector\Exception\StaticVariableException;
use CodeInspector\Exception\TypeResolutionException;
use CodeInspector\Exception\UndefinedConstantException;
use CodeInspector\Exception\UndefinedFunctionException;
use CodeInspector\Exception\UndefinedPropertyException;
@ -752,9 +753,7 @@ class StatementsChecker
if ($stmt->var instanceof PhpParser\Node\Expr\Variable) {
if ($stmt->var->name === 'this') {
$class_checker = $this->_source->getClassChecker();
if (!FileChecker::shouldCheckClassProperties($this->_file_name, $class_checker)) {
// do nothing
} elseif ($class_checker) {
if ($class_checker) {
if (is_string($stmt->name)) {
$property_names = $class_checker->getPropertyNames();
@ -1054,6 +1053,8 @@ class StatementsChecker
$vars_in_scope[$var_name] = 'mixed';
}
} elseif ($expr->returnType === 'void') {
throw new TypeResolutionException('Cannot assign $' . $var_name . ' to type void', $this->_file_name, $expr->getLine());
} elseif (isset($vars_in_scope[$var_name]) && is_string($vars_in_scope[$var_name])) {
$existing_type = $vars_in_scope[$var_name];
@ -1327,7 +1328,7 @@ class StatementsChecker
if ($method_id && isset($arg->value->returnType)) {
foreach (explode('|', $arg->value->returnType) as $return_type) {
TypeChecker::check($return_type, $method_id, $i, $this->_absolute_class, $this->_file_name, $arg->value->getLine());
TypeChecker::checkMethodParam($return_type, $method_id, $i, $this->_absolute_class, $this->_file_name, $arg->value->getLine());
}
}

View File

@ -22,12 +22,16 @@ class TypeChecker
$this->_checker = $statements_checker;
}
public static function check($return_type, $method_id, $arg_offset, $current_class, $file_name, $line_number)
public static function checkMethodParam($return_type, $method_id, $arg_offset, $current_class, $file_name, $line_number)
{
if ($return_type === 'mixed') {
return true;
}
if ($return_type === 'void') {
throw new TypeResolutionException('Argument ' . ($arg_offset + 1) . ' of ' . $method_id . ' cannot be void, but possibly void value was supplied', $file_name, $line_number);
}
$method_params = ClassMethodChecker::getMethodParams($method_id);
if ($arg_offset >= count($method_params)) {