1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Prevent mixed array offset in array creation

Fixes #4846
This commit is contained in:
Matt Brown 2020-12-16 08:18:18 -05:00 committed by Daniil Gentili
parent e87617e598
commit 8f192801c1
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
2 changed files with 46 additions and 20 deletions

View File

@ -8,6 +8,7 @@ use Psalm\CodeLocation;
use Psalm\Context;
use Psalm\Issue\DuplicateArrayKey;
use Psalm\Issue\InvalidArrayOffset;
use Psalm\Issue\MixedArrayOffset;
use Psalm\IssueBuffer;
use Psalm\Type;
use Psalm\Internal\Type\TypeCombiner;
@ -130,6 +131,25 @@ class ArrayAnalyzer
$good_types = [];
foreach ($item_key_type->getAtomicTypes() as $atomic_key_type) {
if ($atomic_key_type instanceof Type\Atomic\TMixed) {
if (IssueBuffer::accepts(
new MixedArrayOffset(
'Cannot create mixed offset expecting array-key',
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()
)) {
// do nothing
}
$bad_types[] = $atomic_key_type;
$good_types[] = new Type\Atomic\TArrayKey;
continue;
}
if (!$atomic_key_type instanceof Type\Atomic\TString
&& !$atomic_key_type instanceof Type\Atomic\TInt
&& !$atomic_key_type instanceof Type\Atomic\TArrayKey
@ -146,20 +166,22 @@ class ArrayAnalyzer
),
$statements_analyzer->getSuppressedIssues()
)) {
$bad_types[] = $atomic_key_type;
// do nothing
}
if ($atomic_key_type instanceof Type\Atomic\TFalse) {
$good_types[] = new Type\Atomic\TLiteralInt(0);
} elseif ($atomic_key_type instanceof Type\Atomic\TTrue) {
$good_types[] = new Type\Atomic\TLiteralInt(1);
} elseif ($atomic_key_type instanceof Type\Atomic\TBool) {
$good_types[] = new Type\Atomic\TLiteralInt(0);
$good_types[] = new Type\Atomic\TLiteralInt(1);
} elseif ($atomic_key_type instanceof Type\Atomic\TFloat) {
$good_types[] = new Type\Atomic\TInt;
} else {
$good_types[] = new Type\Atomic\TArrayKey;
}
$bad_types[] = $atomic_key_type;
if ($atomic_key_type instanceof Type\Atomic\TFalse) {
$good_types[] = new Type\Atomic\TLiteralInt(0);
} elseif ($atomic_key_type instanceof Type\Atomic\TTrue) {
$good_types[] = new Type\Atomic\TLiteralInt(1);
} elseif ($atomic_key_type instanceof Type\Atomic\TBool) {
$good_types[] = new Type\Atomic\TLiteralInt(0);
$good_types[] = new Type\Atomic\TLiteralInt(1);
} elseif ($atomic_key_type instanceof Type\Atomic\TFloat) {
$good_types[] = new Type\Atomic\TInt;
} else {
$good_types[] = new Type\Atomic\TArrayKey;
}
}
}

View File

@ -1494,13 +1494,6 @@ class ArrayAssignmentTest extends TestCase
return [...$data];
}'
],
'ArrayCreateOffsetMixed' => [
'<?php
/** @var mixed $b */
$b = "";
$_a = [$b => "a"];
',
],
'ArrayOffsetNumericSupPHPINTMAX' => [
'<?php
$_a = [
@ -1873,6 +1866,17 @@ class ArrayAssignmentTest extends TestCase
}',
'error_message' => 'LessSpecificReturnStatement',
],
'createArrayWithMixedOffset' => [
'<?php
/**
* @param mixed $index
*/
function test($index): array {
$arr = [$index => 5];
return $arr;
}',
'error_message' => 'MixedArrayOffset'
]
];
}
}