1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +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> * @return list<string>
* @psalm-suppress MixedAssignment * @psalm-suppress MixedAssignment
* @psalm-suppress MixedArgument * @psalm-suppress MixedArgument
* @psalm-suppress PossiblyUndefinedArrayOffset
*/ */
private static function getPsr4Or0Paths(string $current_dir, array $composer_json) : array private static function getPsr4Or0Paths(string $current_dir, array $composer_json) : array
{ {

View File

@ -285,7 +285,7 @@ class ArrayFetchAnalyzer
if (IssueBuffer::accepts( if (IssueBuffer::accepts(
new PossiblyUndefinedArrayOffset( new PossiblyUndefinedArrayOffset(
'Possibly undefined array key ' . $keyed_array_var_id '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) new CodeLocation($statements_analyzer->getSource(), $stmt)
), ),
$statements_analyzer->getSuppressedIssues() $statements_analyzer->getSuppressedIssues()

View File

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

View File

@ -156,7 +156,8 @@ function getVendorDir(string $current_dir): string
exit(1); exit(1);
} }
if (isset($composer_json['config']) if (is_array($composer_json)
&& isset($composer_json['config'])
&& is_array($composer_json['config']) && is_array($composer_json['config'])
&& isset($composer_json['config']['vendor-dir']) && isset($composer_json['config']['vendor-dir'])
&& is_string($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(); $a = new A();
/** /**
* @psalm-suppress UndefinedClass * @psalm-suppress UndefinedClass
* @psalm-suppress PossiblyUndefinedArrayOffset
*/ */
if (!isset($a->arr["bat"]) || strlen($a->arr["bat"])) { }', if (!isset($a->arr["bat"]) || strlen($a->arr["bat"])) { }',
'assertions' => [], 'assertions' => [],
@ -664,6 +663,13 @@ class ArrayAccessTest extends TestCase
], ],
'arrayAccessOnObjectWithNullGet' => [ 'arrayAccessOnObjectWithNullGet' => [
'<?php '<?php
$array = new C([]);
$array["key"] = [];
/** @psalm-suppress PossiblyInvalidArrayAssignment */
$array["key"][] = "testing";
$c = isset($array["foo"]) ? $array["foo"] : null;
class C implements ArrayAccess class C implements ArrayAccess
{ {
/** /**
@ -738,14 +744,7 @@ class ArrayAccessTest extends TestCase
{ {
$this->__unset($offset); $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', '$c' => 'C|null|scalar',
] ]

View File

@ -1006,7 +1006,6 @@ class IssetTest extends \Psalm\Tests\TestCase
if (isset($param["name"])) { if (isset($param["name"])) {
/** /**
* @psalm-suppress MixedArgument * @psalm-suppress MixedArgument
* @psalm-suppress PossiblyUndefinedArrayOffset
*/ */
echo $param["name"]; echo $param["name"];
} }
@ -1029,6 +1028,18 @@ class IssetTest extends \Psalm\Tests\TestCase
return $a ?? $b; 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;
}'
],
]; ];
} }