1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-11 16:59:45 +01:00

Apply psalm-inheritors to interfaces too

This commit is contained in:
robchett 2023-08-31 19:12:58 +01:00 committed by Bruce Weirdan
parent e23971ca29
commit d0c4d170b0
No known key found for this signature in database
GPG Key ID: CFC3AAB181751B0D
2 changed files with 45 additions and 13 deletions

View File

@ -11,9 +11,13 @@ use Psalm\FileManipulation;
use Psalm\Internal\Analyzer\Statements\Expression\ClassConstAnalyzer; use Psalm\Internal\Analyzer\Statements\Expression\ClassConstAnalyzer;
use Psalm\Internal\FileManipulation\FileManipulationBuffer; use Psalm\Internal\FileManipulation\FileManipulationBuffer;
use Psalm\Internal\Provider\NodeDataProvider; use Psalm\Internal\Provider\NodeDataProvider;
use Psalm\Internal\Type\Comparator\UnionTypeComparator;
use Psalm\Issue\InheritorViolation;
use Psalm\Issue\ParseError; use Psalm\Issue\ParseError;
use Psalm\Issue\UndefinedInterface; use Psalm\Issue\UndefinedInterface;
use Psalm\IssueBuffer; use Psalm\IssueBuffer;
use Psalm\Type\Atomic\TNamedObject;
use Psalm\Type\Union;
use UnexpectedValueException; use UnexpectedValueException;
use function strtolower; use function strtolower;
@ -109,6 +113,23 @@ class InterfaceAnalyzer extends ClassLikeAnalyzer
} }
} }
$class_union = new Union([new TNamedObject($fq_interface_name)]);
foreach ($class_storage->direct_interface_parents as $parent_interface) {
$parent_storage = $codebase->classlikes->getStorageFor($parent_interface);
if ($parent_storage && $parent_storage->inheritors) {
if (!UnionTypeComparator::isContainedBy($codebase, $class_union, $parent_storage->inheritors)) {
IssueBuffer::maybeAdd(
new InheritorViolation(
'Interface ' . $fq_interface_name . '
is not an allowed inheritor of parent interface ' . $parent_interface,
new CodeLocation($this, $this->class),
),
$this->getSuppressedIssues(),
);
}
}
}
$fq_interface_name = $this->getFQCLN(); $fq_interface_name = $this->getFQCLN();
if (!$fq_interface_name) { if (!$fq_interface_name) {

View File

@ -857,7 +857,6 @@ class ClassTest extends TestCase
*/ */
class BaseClass {} class BaseClass {}
class FooClass extends BaseClass {} class FooClass extends BaseClass {}
$a = new FooClass();
PHP, PHP,
], ],
'unionInheritorIsAllowed' => [ 'unionInheritorIsAllowed' => [
@ -868,9 +867,7 @@ class ClassTest extends TestCase
*/ */
class BaseClass {} class BaseClass {}
class FooClass extends BaseClass {} class FooClass extends BaseClass {}
$a = new FooClass();
class BarClass extends FooClass {} class BarClass extends FooClass {}
$b = new BarClass();
PHP, PHP,
], ],
'multiInheritorIsAllowed' => [ 'multiInheritorIsAllowed' => [
@ -881,9 +878,7 @@ class ClassTest extends TestCase
*/ */
class BaseClass {} class BaseClass {}
class FooClass extends BaseClass {} class FooClass extends BaseClass {}
$a = new FooClass();
class BarClass extends FooClass {} class BarClass extends FooClass {}
$b = new BarClass();
PHP, PHP,
], ],
'skippedInheritorIsAllowed' => [ 'skippedInheritorIsAllowed' => [
@ -894,9 +889,7 @@ class ClassTest extends TestCase
*/ */
class BaseClass {} class BaseClass {}
class FooClass extends BaseClass {} class FooClass extends BaseClass {}
$a = new FooClass();
class BarClass extends FooClass {} class BarClass extends FooClass {}
$b = new BarClass();
PHP, PHP,
], ],
'CompositeInheritorIsAllowed' => [ 'CompositeInheritorIsAllowed' => [
@ -908,7 +901,6 @@ class ClassTest extends TestCase
class BaseClass {} class BaseClass {}
interface FooInterface {} interface FooInterface {}
class BarClass extends BaseClass implements FooInterface {} class BarClass extends BaseClass implements FooInterface {}
$b = new BarClass();
PHP, PHP,
], ],
'InterfaceInheritorIsAllowed' => [ 'InterfaceInheritorIsAllowed' => [
@ -919,12 +911,10 @@ class ClassTest extends TestCase
*/ */
interface BaseInterface {} interface BaseInterface {}
class FooClass implements BaseInterface {} class FooClass implements BaseInterface {}
$a = new FooClass();
class BarClass implements BaseInterface {} class BarClass implements BaseInterface {}
$b = new BarClass();
PHP, PHP,
], ],
'MultiInterfaceInheritorIsAllowed' => [ 'MultiInterfaceInheritorIsAllowed' => [
'code' => <<<'PHP' 'code' => <<<'PHP'
<?php <?php
/** /**
@ -936,7 +926,16 @@ class ClassTest extends TestCase
*/ */
interface InterfaceB {} interface InterfaceB {}
class FooClass implements InterfaceA, InterfaceB {} class FooClass implements InterfaceA, InterfaceB {}
$a = new FooClass(); PHP,
],
'InterfaceOfInterfaceInheritorIsAllowed' => [
'code' => <<<'PHP'
<?php
/**
* @psalm-inheritors InterfaceB
*/
interface InterfaceA {}
interface InterfaceB extends InterfaceA {}
PHP, PHP,
], ],
]; ];
@ -1409,6 +1408,18 @@ class ClassTest extends TestCase
'error_message' => 'InheritorViolation', 'error_message' => 'InheritorViolation',
'ignored_issues' => [], 'ignored_issues' => [],
], ],
'interfaceCannotImplementIfNotInInheritors' => [
'code' => <<<'PHP'
<?php
/**
* @psalm-inheritors FooClass|BarClass
*/
interface BaseInterface {}
interface BazInterface extends BaseInterface {}
PHP,
'error_message' => 'InheritorViolation',
'ignored_issues' => [],
],
'UnfulfilledInterfaceInheritors' => [ 'UnfulfilledInterfaceInheritors' => [
'code' => <<<'PHP' 'code' => <<<'PHP'
<?php <?php