Skip to content
Permalink
Browse files

Allow static::class to be used in array assertions

  • Loading branch information
muglug committed Dec 4, 2019
1 parent d5f5c74 commit e1dd22ef6c34ed44ca080a9c641180989c1fdda5
@@ -889,7 +889,9 @@ public static function getArrayAccessTypeGivenOffset(

$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();
}

@@ -1028,6 +1028,12 @@ public static function getArrayVarId(
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))) {
@@ -498,7 +498,7 @@ private static function getValueForKey(
}
} 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;
}
@@ -651,6 +651,49 @@ function foo(string $s) : string {
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 {}'
],
];
}

0 comments on commit e1dd22e

Please sign in to comment.
You can’t perform that action at this time.