1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Separate iterator issues

This commit is contained in:
muglug 2017-05-22 11:59:58 -04:00
parent 96e3ad02fc
commit 65d86d35ad
4 changed files with 43 additions and 1 deletions

View File

@ -139,6 +139,7 @@
<xs:element name="NullArgument" type="IssueHandlerType" minOccurs="0" />
<xs:element name="NullArrayAccess" type="IssueHandlerType" minOccurs="0" />
<xs:element name="NullFunctionCall" type="IssueHandlerType" minOccurs="0" />
<xs:element name="NullIterator" type="IssueHandlerType" minOccurs="0" />
<xs:element name="NullOperand" type="IssueHandlerType" minOccurs="0" />
<xs:element name="NullPropertyAssignment" type="IssueHandlerType" minOccurs="0" />
<xs:element name="NullPropertyFetch" type="IssueHandlerType" minOccurs="0" />
@ -150,6 +151,7 @@
<xs:element name="PossiblyNullArgument" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyNullArrayAccess" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyNullFunctionCall" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyNullIterator" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyNullOperand" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyNullPropertyAssignment" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyNullPropertyFetch" type="IssueHandlerType" minOccurs="0" />

View File

@ -13,6 +13,9 @@ use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Issue\InvalidIterator;
use Psalm\IssueBuffer;
use Psalm\Issue\NullIterator;
use Psalm\Issue\NullReference;
use Psalm\Issue\PossiblyNullIterator;
use Psalm\Type;
class ForeachChecker
@ -54,12 +57,38 @@ class ForeachChecker
}
if ($iterator_type) {
if ($iterator_type->isNull()) {
if (IssueBuffer::accepts(
new NullIterator(
'Cannot iterate over null',
new CodeLocation($statements_checker->getSource(), $stmt->expr)
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
} elseif ($iterator_type->isNullable() && !$iterator_type->ignore_nullable_issues) {
if (IssueBuffer::accepts(
new PossiblyNullIterator(
'Cannot iterate over nullable var ' . $iterator_type,
new CodeLocation($statements_checker->getSource(), $stmt->expr)
),
$statements_checker->getSuppressedIssues()
)) {
return false;
}
}
foreach ($iterator_type->types as $iterator_type) {
// if it's an empty array, we cannot iterate over it
if ((string) $iterator_type === 'array<empty, empty>') {
continue;
}
if ($iterator_type instanceof Type\Atomic\TNull) {
continue;
}
if ($iterator_type instanceof Type\Atomic\TArray) {
if (!$value_type) {
$value_type = $iterator_type->type_params[1];
@ -78,7 +107,6 @@ class ForeachChecker
}
if ($iterator_type instanceof Type\Atomic\Scalar ||
$iterator_type instanceof Type\Atomic\TNull ||
$iterator_type instanceof Type\Atomic\TVoid
) {
if (IssueBuffer::accepts(

View File

@ -0,0 +1,6 @@
<?php
namespace Psalm\Issue;
class NullIterator extends CodeError
{
}

View File

@ -0,0 +1,6 @@
<?php
namespace Psalm\Issue;
class PossiblyNullIterator extends CodeError
{
}