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

Add ability to stub constants

This commit is contained in:
Matt Brown 2017-07-26 19:54:22 -04:00
parent 0e67565b2c
commit 7b178a2b25
2 changed files with 17 additions and 0 deletions

View File

@ -40,6 +40,11 @@ class StatementsChecker extends SourceChecker implements StatementsSource
*/
public static $user_constants = [];
/**
* @var array<string, Type\Union>
*/
public static $stub_constants = [];
/**
* @param StatementsSource $source
*/
@ -770,6 +775,10 @@ class StatementsChecker extends SourceChecker implements StatementsSource
return ClassLikeChecker::getTypeFromValue($predefined_constants[$fq_const_name ?: $const_name]);
}
if (isset(self::$stub_constants[$fq_const_name ?: $const_name])) {
return self::$stub_constants[$fq_const_name ?: $const_name];
}
return null;
}
@ -1054,6 +1063,7 @@ class StatementsChecker extends SourceChecker implements StatementsSource
public static function clearCache()
{
self::$user_constants = [];
self::$stub_constants = [];
ExpressionChecker::clearCache();
}

View File

@ -355,6 +355,13 @@ class DependencyFinderVisitor extends PhpParser\NodeVisitorAbstract implements P
$var_type->queueClassLikesForScanning($this->project_checker);
}
}
} elseif ($node instanceof PhpParser\Node\Stmt\Const_) {
if ($this->project_checker->register_global_functions) {
foreach ($node->consts as $const) {
StatementsChecker::$stub_constants[$const->name] =
StatementsChecker::getSimpleType($const->value) ?: Type::getMixed();
}
}
}
}