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

Support interfaces

This commit is contained in:
RobChett 2023-04-28 17:10:26 +01:00
parent de877f1dcf
commit cad5288484
2 changed files with 59 additions and 1 deletions

View File

@ -335,7 +335,7 @@ abstract class ClassLikeAnalyzer extends SourceAnalyzer
$classUnion = new Union([new TNamedObject($fq_class_name)]);
foreach ($class_storage->parent_classes as $parent_class) {
foreach ($class_storage->parent_classes + $class_storage->direct_class_interfaces as $parent_class) {
$parent_storage = $codebase->classlikes->getStorageFor($parent_class);
if ($parent_storage && $parent_storage->inheritors) {
if (!UnionTypeComparator::isContainedBy($codebase, $classUnion, $parent_storage->inheritors)) {

View File

@ -909,6 +909,34 @@ class ClassTest extends TestCase
$b = new BarClass();
PHP,
],
'InterfaceInheritorIsAllowed' => [
'code' => <<<'PHP'
<?php
/**
* @psalm-inheritors FooClass|BarClass
*/
interface BaseInterface {}
class FooClass implements BaseInterface {}
$a = new FooClass();
class BarClass implements BaseInterface {}
$b = new BarClass();
PHP,
],
'MultiInterfaceInheritorIsAllowed' => [
'code' => <<<'PHP'
<?php
/**
* @psalm-inheritors FooClass|BarClass
*/
interface InterfaceA {}
/**
* @psalm-inheritors FooClass|BarClass
*/
interface InterfaceB {}
class FooClass implements InterfaceA, InterfaceB {}
$a = new FooClass();
PHP,
],
];
}
@ -1368,6 +1396,36 @@ class ClassTest extends TestCase
'error_message' => 'InvalidExtendClass',
'ignored_issues' => [],
],
'classCannotImplementIfNotInInheritors' => [
'code' => <<<'PHP'
<?php
/**
* @psalm-inheritors FooClass|BarClass
*/
interface BaseInterface {}
class BazClass implements BaseInterface {}
$a = new BazClass();
PHP,
'error_message' => 'InvalidExtendClass',
'ignored_issues' => [],
],
'UnfulfilledInterfaceInheritors' => [
'code' => <<<'PHP'
<?php
/**
* @psalm-inheritors FooClass
*/
interface InterfaceA {}
/**
* @psalm-inheritors BarClass
*/
interface InterfaceB {}
class BazClass implements InterFaceA, InterFaceB {}
$a = new BazClass();
PHP,
'error_message' => 'InvalidExtendClass',
'ignored_issues' => [],
],
];
}
}