mirror of
https://github.com/danog/psalm.git
synced 2024-11-30 04:39:00 +01:00
Allow suppression of null checks for certain files
This commit is contained in:
parent
7fd8955fea
commit
59c9ae6174
@ -27,6 +27,9 @@ class FileChecker implements StatementsSource
|
||||
protected static $_functions = [];
|
||||
protected static $_includes_to_ignore = [];
|
||||
|
||||
protected static $_ignore_check_nulls_pattern = null;
|
||||
protected static $_ignore_check_variables_pattern = null;
|
||||
|
||||
public static $show_notices = true;
|
||||
|
||||
public function __construct($file_name, array $preloaded_statements = [])
|
||||
@ -295,4 +298,24 @@ class FileChecker implements StatementsSource
|
||||
{
|
||||
return self::$_includes_to_ignore;
|
||||
}
|
||||
|
||||
public static function ignoreNullChecksFor($pattern)
|
||||
{
|
||||
self::$_ignore_check_nulls_pattern = $pattern;
|
||||
}
|
||||
|
||||
public static function ignoreVariableChecksFor($pattern)
|
||||
{
|
||||
self::$_ignore_check_variables_pattern = $pattern;
|
||||
}
|
||||
|
||||
public static function shouldCheckVariables($file_name)
|
||||
{
|
||||
return !self::$_ignore_check_variables_pattern || !preg_match(self::$_ignore_check_variables_pattern, $file_name);
|
||||
}
|
||||
|
||||
public static function shouldCheckNulls($file_name)
|
||||
{
|
||||
return !self::$_ignore_check_nulls_pattern || !preg_match(self::$_ignore_check_nulls_pattern, $file_name);
|
||||
}
|
||||
}
|
||||
|
@ -38,9 +38,7 @@ class FunctionChecker implements StatementsSource
|
||||
$vars_in_scope = $extra_scope_vars;
|
||||
$vars_possibly_in_scope = $extra_scope_vars;
|
||||
|
||||
$check_variables = substr($this->_file_name, -4) === '.php' || $this->_function->params;
|
||||
|
||||
$statements_checker = new StatementsChecker($this, $check_variables);
|
||||
$statements_checker = new StatementsChecker($this, !empty($this->_function->params));
|
||||
|
||||
foreach ($this->_function->params as $param) {
|
||||
if ($param->type) {
|
||||
|
@ -55,12 +55,12 @@ class StatementsChecker
|
||||
protected static $_check_string_fn = null;
|
||||
protected static $_mock_interfaces = [];
|
||||
|
||||
public function __construct(StatementsSource $source, $check_variables = true)
|
||||
public function __construct(StatementsSource $source, $enforce_variable_checks = false)
|
||||
{
|
||||
$this->_source = $source;
|
||||
$this->_check_classes = true;
|
||||
$this->_check_methods = true;
|
||||
$this->_check_variables = $check_variables;
|
||||
|
||||
$this->_check_consts = true;
|
||||
|
||||
$this->_file_name = $this->_source->getFileName();
|
||||
@ -71,6 +71,9 @@ class StatementsChecker
|
||||
$this->_class_name = $this->_source->getClassName();
|
||||
$this->_class_extends = $this->_source->getParentClass();
|
||||
|
||||
$this->_check_variables = FileChecker::shouldCheckVariables($this->_file_name) || $enforce_variable_checks;
|
||||
$this->_check_nulls = FileChecker::shouldCheckNulls($this->_file_name);
|
||||
|
||||
$this->_type_checker = new TypeChecker($source, $this);
|
||||
}
|
||||
|
||||
@ -1304,7 +1307,11 @@ class StatementsChecker
|
||||
$absolute_class = preg_replace('/\<[A-Za-z0-9' . '\\\\' . ']+\>/', '', $absolute_class);
|
||||
|
||||
if ($absolute_class === 'null') {
|
||||
throw new InvalidArgumentException('Cannot call method ' . $stmt->name . ' on nullable variable ' . $class_type, $this->_file_name, $stmt->getLine());
|
||||
if ($this->_check_nulls) {
|
||||
throw new InvalidArgumentException('Cannot call method ' . $stmt->name . ' on nullable variable ' . $class_type, $this->_file_name, $stmt->getLine());
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (in_array($absolute_class, ['int', 'bool', 'array'])) {
|
||||
|
Loading…
Reference in New Issue
Block a user