1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-29 20:28:59 +01:00

Fix assertions on unions

This commit is contained in:
Matt Brown 2020-12-01 21:12:03 -05:00 committed by Daniil Gentili
parent d6b98f2afe
commit 2d1222ddec
Signed by: danog
GPG Key ID: 8C1BE3B34B230CA7
6 changed files with 27 additions and 12 deletions

View File

@ -205,6 +205,7 @@ class Creator
* @return list<string>
* @psalm-suppress MixedAssignment
* @psalm-suppress MixedArgument
* @psalm-suppress PossiblyUndefinedArrayOffset
*/
private static function getPsr4Or0Paths(string $current_dir, array $composer_json) : array
{

View File

@ -285,7 +285,7 @@ class ArrayFetchAnalyzer
if (IssueBuffer::accepts(
new PossiblyUndefinedArrayOffset(
'Possibly undefined array key ' . $keyed_array_var_id
. ($stmt_var_type ? ' on ' . $stmt_var_type->getId() : ''),
. ' on ' . $stmt_var_type->getId(),
new CodeLocation($statements_analyzer->getSource(), $stmt)
),
$statements_analyzer->getSuppressedIssues()

View File

@ -614,6 +614,9 @@ class Reconciler
&& ($has_isset || $has_inverted_isset)
) {
$has_object_array_access = true;
unset($existing_keys[$new_base_key]);
return null;
} elseif (!$existing_key_type_part instanceof Type\Atomic\TKeyedArray) {
return Type::getMixed();

View File

@ -156,7 +156,8 @@ function getVendorDir(string $current_dir): string
exit(1);
}
if (isset($composer_json['config'])
if (is_array($composer_json)
&& isset($composer_json['config'])
&& is_array($composer_json['config'])
&& isset($composer_json['config']['vendor-dir'])
&& is_string($composer_json['config']['vendor-dir'])

View File

@ -368,7 +368,6 @@ class ArrayAccessTest extends TestCase
$a = new A();
/**
* @psalm-suppress UndefinedClass
* @psalm-suppress PossiblyUndefinedArrayOffset
*/
if (!isset($a->arr["bat"]) || strlen($a->arr["bat"])) { }',
'assertions' => [],
@ -664,6 +663,13 @@ class ArrayAccessTest extends TestCase
],
'arrayAccessOnObjectWithNullGet' => [
'<?php
$array = new C([]);
$array["key"] = [];
/** @psalm-suppress PossiblyInvalidArrayAssignment */
$array["key"][] = "testing";
$c = isset($array["foo"]) ? $array["foo"] : null;
class C implements ArrayAccess
{
/**
@ -738,14 +744,7 @@ class ArrayAccessTest extends TestCase
{
$this->__unset($offset);
}
}
$array = new C([]);
$array["key"] = [];
/** @psalm-suppress PossiblyInvalidArrayAssignment */
$array["key"][] = "testing";
$c = isset($array["foo"]) ? $array["foo"] : null;',
}',
[
'$c' => 'C|null|scalar',
]

View File

@ -1006,7 +1006,6 @@ class IssetTest extends \Psalm\Tests\TestCase
if (isset($param["name"])) {
/**
* @psalm-suppress MixedArgument
* @psalm-suppress PossiblyUndefinedArrayOffset
*/
echo $param["name"];
}
@ -1029,6 +1028,18 @@ class IssetTest extends \Psalm\Tests\TestCase
return $a ?? $b;
}'
],
'nullCoalesceSimpleArrayOffset' => [
'<?php
function a(array $arr) : void {
/** @psalm-suppress MixedArgument */
echo isset($arr["a"]["b"]) ? $arr["a"]["b"] : 0;
}
function b(array $arr) : void {
/** @psalm-suppress MixedArgument */
echo $arr["a"]["b"] ?? 0;
}'
],
];
}