Skip to content

Commit

Permalink
Add specific NullArrayAccess error
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Nov 22, 2016
1 parent 89ea438 commit 134bc95
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/Psalm/Checker/Statements/Expression/FetchChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Psalm\Issue\MixedArrayOffset;
use Psalm\Issue\MixedPropertyFetch;
use Psalm\Issue\NoInterfaceProperties;
use Psalm\Issue\NullArrayAccess;
use Psalm\Issue\NullPropertyFetch;
use Psalm\Issue\NullReference;
use Psalm\Issue\ParentNotFound;
Expand Down Expand Up @@ -93,7 +94,7 @@ public static function checkPropertyFetch(

if ($stmt_var_type->isNull()) {
if (IssueBuffer::accepts(
new NullReference(
new NullPropertyFetch(
'Cannot get property on null variable ' . $stmt_var_id,
$statements_checker->getCheckedFileName(),
$stmt->getLine()
Expand All @@ -108,7 +109,7 @@ public static function checkPropertyFetch(

if ($stmt_var_type->isEmpty()) {
if (IssueBuffer::accepts(
new NullReference(
new MixedPropertyFetch(
'Cannot fetch property on empty var ' . $stmt_var_id,
$statements_checker->getCheckedFileName(),
$stmt->getLine()
Expand Down Expand Up @@ -906,15 +907,20 @@ public static function checkArrayAccess(
$stmt->inferredType = Type::getString();
} elseif ($type->isNull()) {
if (IssueBuffer::accepts(
new NullReference(
new NullArrayAccess(
'Cannot access array value on possibly null variable ' . $array_var_id . ' of type ' . $var_type,
$statements_checker->getCheckedFileName(),
$stmt->getLine()
),
$statements_checker->getSuppressedIssues()
)) {
$stmt->inferredType = Type::getMixed();
break;
if (isset($stmt->inferredType)) {
$stmt->inferredType = Type::combineUnionTypes($stmt->inferredType, Type::getNull());
}
else {
$stmt->inferredType = Type::getNull();
}
continue;
}
} elseif ($type->isMixed() || $type->isEmpty()) {
if (IssueBuffer::accepts(
Expand Down
11 changes: 11 additions & 0 deletions src/Psalm/Issue/NullArrayAccess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace Psalm\Issue;

/**
* This is different from NullReference, as PHP throws a notice (vs the possibility of a fatal error with a null
* reference)
*
*/
class NullArrayAccess extends CodeError
{
}

0 comments on commit 134bc95

Please sign in to comment.