1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Fix #2844 - resolve constant offset references

This commit is contained in:
Brown 2020-02-21 15:56:30 -05:00
parent 292a2359b3
commit 443558ca49
2 changed files with 55 additions and 0 deletions

View File

@ -2022,6 +2022,50 @@ class StatementsAnalyzer extends SourceAnalyzer implements StatementsSource
return $type_to_invert;
}
if ($stmt instanceof PhpParser\Node\Expr\ArrayDimFetch) {
if ($stmt->var instanceof PhpParser\Node\Expr\ClassConstFetch) {
$array_type = self::getSimpleType(
$codebase,
$nodes,
$stmt->var,
$aliases,
$file_source,
$existing_class_constants,
$fq_classlike_name
);
$dim_type = self::getSimpleType(
$codebase,
$nodes,
$stmt->dim,
$aliases,
$file_source,
$existing_class_constants,
$fq_classlike_name
);
if ($array_type !== null && $dim_type !== null) {
if ($dim_type->isSingleStringLiteral()) {
$dim_value = $dim_type->getSingleStringLiteral()->value;
} elseif ($dim_type->isSingleIntLiteral()) {
$dim_value = $dim_type->getSingleIntLiteral()->value;
} else {
return null;
}
foreach ($array_type->getAtomicTypes() as $array_atomic_type) {
if ($array_atomic_type instanceof Type\Atomic\ObjectLike) {
if (isset($array_atomic_type->properties[$dim_value])) {
return clone $array_atomic_type->properties[$dim_value];
}
return null;
}
}
}
}
}
return null;
}

View File

@ -739,6 +739,17 @@ class ConstantTest extends TestCase
}
}'
],
'getClassConstantOffset' => [
'<?php
class C {
private const A = [ 0 => "string" ];
private const B = self::A[0];
public function foo(): string {
return self::B;
}
}'
],
];
}