1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-30 04:39:00 +01:00

Allow static::class to be used in array assertions

This commit is contained in:
Brown 2019-12-04 12:23:26 -05:00
parent d5f5c742ce
commit e1dd22ef6c
4 changed files with 53 additions and 2 deletions

View File

@ -889,7 +889,9 @@ class ArrayFetchAnalyzer
$has_valid_offset = true;
} else {
if (!$context->inside_isset || $type->sealed) {
if (!$context->inside_isset
|| ($type->sealed && !$union_comparison_results->type_coerced)
) {
$expected_offset_types[] = (string)$generic_key_type->getId();
}

View File

@ -1028,6 +1028,12 @@ class ExpressionAnalyzer
if ($object_id && $stmt->dim->name instanceof PhpParser\Node\Identifier) {
$offset = $object_id . '->' . $stmt->dim->name;
}
} elseif ($stmt->dim instanceof PhpParser\Node\Expr\ClassConstFetch
&& $stmt->dim->name instanceof PhpParser\Node\Identifier
&& $stmt->dim->class instanceof PhpParser\Node\Name
&& $stmt->dim->class->parts[0] === 'static'
) {
$offset = 'static::' . $stmt->dim->name;
} elseif ($stmt->dim
&& $source instanceof StatementsAnalyzer
&& ($stmt_dim_type = $source->node_data->getType($stmt->dim))) {

View File

@ -498,7 +498,7 @@ class Reconciler
}
} elseif (!$existing_key_type_part instanceof Type\Atomic\ObjectLike) {
return Type::getMixed();
} elseif ($array_key[0] === '$') {
} elseif ($array_key[0] === '$' || ($array_key[0] !== '\'' && !is_numeric($array_key[0]))) {
if ($has_empty) {
return null;
}

View File

@ -651,6 +651,49 @@ class IssetTest extends TestCase
return $port;
}',
],
'accessAfterArrayExistsVariable' => [
'<?php
abstract class P {
const MAP = [
A::class => 1,
B::class => 2,
C::class => 3,
];
public function foo(string $s) : int {
$a = static::class;
if (!isset(self::MAP[$a])) {
throw new \Exception("bad");
}
return self::MAP[$a];
}
}
class A extends P {}
class B extends P {}
class C extends P {}'
],
'accessAfterArrayExistsStaticClass' => [
'<?php
abstract class P {
const MAP = [
A::class => 1,
B::class => 2,
C::class => 3,
];
public function foo(string $s) : int {
if (!isset(self::MAP[static::class])) {
throw new \Exception("bad");
}
return self::MAP[static::class];
}
}
class A extends P {}
class B extends P {}
class C extends P {}'
],
];
}