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:
parent
96e3ad02fc
commit
65d86d35ad
@ -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" />
|
||||
|
@ -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(
|
||||
|
6
src/Psalm/Issue/NullIterator.php
Normal file
6
src/Psalm/Issue/NullIterator.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
namespace Psalm\Issue;
|
||||
|
||||
class NullIterator extends CodeError
|
||||
{
|
||||
}
|
6
src/Psalm/Issue/PossiblyNullIterator.php
Normal file
6
src/Psalm/Issue/PossiblyNullIterator.php
Normal file
@ -0,0 +1,6 @@
|
||||
<?php
|
||||
namespace Psalm\Issue;
|
||||
|
||||
class PossiblyNullIterator extends CodeError
|
||||
{
|
||||
}
|
Loading…
Reference in New Issue
Block a user