From 9ff8736e53220eec466234b0c36c304bb6199075 Mon Sep 17 00:00:00 2001 From: "github@muglug.com" Date: Tue, 9 May 2017 16:38:58 -0400 Subject: [PATCH] Add the means to ignore PossiblyNull* errors from the type itself --- .../Checker/Statements/Expression/AssignmentChecker.php | 2 +- src/Psalm/Checker/Statements/Expression/CallChecker.php | 8 ++++++-- src/Psalm/Checker/Statements/Expression/FetchChecker.php | 2 +- src/Psalm/Checker/Statements/ExpressionChecker.php | 4 ++-- src/Psalm/Type/Union.php | 7 +++++++ 5 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/Psalm/Checker/Statements/Expression/AssignmentChecker.php b/src/Psalm/Checker/Statements/Expression/AssignmentChecker.php index 7cfc869c9..e7052b80f 100644 --- a/src/Psalm/Checker/Statements/Expression/AssignmentChecker.php +++ b/src/Psalm/Checker/Statements/Expression/AssignmentChecker.php @@ -511,7 +511,7 @@ class AssignmentChecker return null; } - if ($lhs_type->isNullable()) { + if ($lhs_type->isNullable() && !$lhs_type->ignore_nullable_issues) { if (IssueBuffer::accepts( new PossiblyNullPropertyAssignment( $lhs_var_id . ' with possibly null type \'' . $lhs_type . '\' cannot be assigned to', diff --git a/src/Psalm/Checker/Statements/Expression/CallChecker.php b/src/Psalm/Checker/Statements/Expression/CallChecker.php index b9e6a2c5a..b43732899 100644 --- a/src/Psalm/Checker/Statements/Expression/CallChecker.php +++ b/src/Psalm/Checker/Statements/Expression/CallChecker.php @@ -694,7 +694,11 @@ class CallChecker return null; } - if ($class_type && is_string($stmt->name) && $class_type->isNullable()) { + if ($class_type && + is_string($stmt->name) && + $class_type->isNullable() && + !$class_type->ignore_nullable_issues + ) { if (IssueBuffer::accepts( new PossiblyNullReference( 'Cannot call method ' . $stmt->name . ' on possibly null variable ' . $var_id, @@ -1942,7 +1946,7 @@ class CallChecker return null; } - if ($input_type->isNullable()) { + if ($input_type->isNullable() && !$input_type->ignore_nullable_issues) { if (IssueBuffer::accepts( new PossiblyNullArgument( 'Argument ' . ($argument_offset + 1) . $method_identifier . ' cannot be null, possibly ' . diff --git a/src/Psalm/Checker/Statements/Expression/FetchChecker.php b/src/Psalm/Checker/Statements/Expression/FetchChecker.php index 01b26664e..61804e4d2 100644 --- a/src/Psalm/Checker/Statements/Expression/FetchChecker.php +++ b/src/Psalm/Checker/Statements/Expression/FetchChecker.php @@ -153,7 +153,7 @@ class FetchChecker return null; } - if ($stmt_var_type->isNullable()) { + if ($stmt_var_type->isNullable() && !$stmt_var_type->ignore_nullable_issues) { if (IssueBuffer::accepts( new PossiblyNullPropertyFetch( 'Cannot get property on possibly null variable ' . $stmt_var_id . ' of type ' . $stmt_var_type, diff --git a/src/Psalm/Checker/Statements/ExpressionChecker.php b/src/Psalm/Checker/Statements/ExpressionChecker.php index f65421eb3..6dbd54563 100644 --- a/src/Psalm/Checker/Statements/ExpressionChecker.php +++ b/src/Psalm/Checker/Statements/ExpressionChecker.php @@ -1326,7 +1326,7 @@ class ExpressionChecker return; } - if ($left_type->isNullable()) { + if ($left_type->isNullable() && !$left_type->ignore_nullable_issues) { if (IssueBuffer::accepts( new PossiblyNullOperand( 'Cannot concatenate with a possibly null ' . $left_type, @@ -1338,7 +1338,7 @@ class ExpressionChecker } } - if ($right_type->isNullable()) { + if ($right_type->isNullable() && !$right_type->ignore_nullable_issues) { if (IssueBuffer::accepts( new PossiblyNullOperand( 'Cannot concatenate with a possibly null ' . $right_type, diff --git a/src/Psalm/Type/Union.php b/src/Psalm/Type/Union.php index baa681251..7e8339cdd 100644 --- a/src/Psalm/Type/Union.php +++ b/src/Psalm/Type/Union.php @@ -38,6 +38,13 @@ class Union */ public $failed_reconciliation = false; + /** + * Whether or not to ignore issues with possibly-null values + * + * @var boolean + */ + public $ignore_nullable_issues = false; + /** * Constructs an Union instance * @param array $types