1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-22 05:41:20 +01:00

Merge pull request #6922 from weirdan/cases-references

This commit is contained in:
Bruce Weirdan 2021-11-15 11:49:47 +02:00 committed by GitHub
commit 608e8d11fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 67 additions and 30 deletions

View File

@ -1627,35 +1627,36 @@ class ClassLikes
$storage = $this->classlike_storage_provider->get($class_name);
if (!isset($storage->constants[$constant_name])) {
return null;
if (isset($storage->constants[$constant_name])) {
$constant_storage = $storage->constants[$constant_name];
if ($visibility === ReflectionProperty::IS_PUBLIC
&& $constant_storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PUBLIC
) {
return null;
}
if ($visibility === ReflectionProperty::IS_PROTECTED
&& $constant_storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PUBLIC
&& $constant_storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PROTECTED
) {
return null;
}
if ($constant_storage->unresolved_node) {
$constant_storage->type = new Type\Union([ConstantTypeResolver::resolve(
$this,
$constant_storage->unresolved_node,
$statements_analyzer,
$visited_constant_ids
)]);
}
return $constant_storage->type;
} elseif (isset($storage->enum_cases[$constant_name])) {
return new Type\Union([new Type\Atomic\TEnumCase($storage->name, $constant_name)]);
}
$constant_storage = $storage->constants[$constant_name];
if ($visibility === ReflectionProperty::IS_PUBLIC
&& $constant_storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PUBLIC
) {
return null;
}
if ($visibility === ReflectionProperty::IS_PROTECTED
&& $constant_storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PUBLIC
&& $constant_storage->visibility !== ClassLikeAnalyzer::VISIBILITY_PROTECTED
) {
return null;
}
if ($constant_storage->unresolved_node) {
$constant_storage->type = new Type\Union([ConstantTypeResolver::resolve(
$this,
$constant_storage->unresolved_node,
$statements_analyzer,
$visited_constant_ids
)]);
}
return $constant_storage->type;
return null;
}
private function checkMethodReferences(ClassLikeStorage $classlike_storage, Methods $methods): void

View File

@ -7,7 +7,7 @@ class ConstantTest extends TestCase
use Traits\ValidCodeAnalysisTestTrait;
/**
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[], php_version?: string}>
*/
public function providerValidCodeParse(): iterable
{
@ -1180,6 +1180,22 @@ class ConstantTest extends TestCase
}',
],
'classConstantReferencingEnumCase' => [
'<?php
enum E {
case Z;
}
class C {
public const CC = E::Z;
}
$c = C::CC;
',
'assertions' => [
'$c===' => 'enum(E::Z)'
],
[],
'8.1'
],
];
}

View File

@ -154,6 +154,26 @@ class EnumTest extends TestCase
[],
'8.1'
],
'SKIPPED-wildcardEnum' => [
'<?php
enum A {
case C_1;
case C_2;
case C_3;
/**
* @param self::C_* $i
*/
public static function foo(self $i) : void {}
}
A::foo(A::C_1);
A::foo(A::C_2);
A::foo(A::C_3);',
'assertions' => [],
[],
'8.1',
],
];
}

View File

@ -15,7 +15,7 @@ use const PHP_VERSION;
trait ValidCodeAnalysisTestTrait
{
/**
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[]}>
* @return iterable<string,array{string,assertions?:array<string,string>,error_levels?:string[],php_version?:string}>
*/
abstract public function providerValidCodeParse(): iterable;