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

Merge pull request #8933 from jack-worman/ReflectionClass_implementsInterface_assert

Closes https://github.com/vimeo/psalm/issues/8920
This commit is contained in:
Bruce Weirdan 2022-12-18 18:12:35 -04:00 committed by GitHub
commit 7afeee54d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 77 additions and 15 deletions

View File

@ -89,15 +89,17 @@ class ReflectionClass implements Reflector {
public function getDocComment(): string|false {}
/**
* @param ReflectionClass|class-string $class
*
* @template J as object
* @param self<J>|class-string<J> $class
* @psalm-assert-if-true self<T&J> $this
* @psalm-pure
*/
public function isSubclassOf(self|string $class): bool {}
/**
* @param self|class-string $interface
*
* @template J as object
* @param self<J>|interface-string<J> $interface
* @psalm-assert-if-true self<T&J> $this
* @psalm-pure
*/
public function implementsInterface(self|string $interface): bool {}

View File

@ -147,15 +147,13 @@ class ReflectionClass implements Reflector {
public function getReflectionConstants(?int $filter = null): array {}
/**
* @return array<class-string, self>
*
* @return array<interface-string, self>
* @psalm-pure
*/
public function getInterfaces(): array {}
/**
* @return list<class-string>
*
* @return list<interface-string>
* @psalm-pure
*/
public function getInterfaceNames(): array {}
@ -192,7 +190,12 @@ class ReflectionClass implements Reflector {
*/
public function getModifiers(): bool {}
/** @psalm-pure */
/**
* @template J as object
* @param J $object
* @psalm-assert-if-true T&J $object
* @psalm-pure
*/
public function isInstance(object $object): bool {}
/**
@ -222,8 +225,9 @@ class ReflectionClass implements Reflector {
public function getParentClass() {}
/**
* @param ReflectionClass|class-string $class
*
* @template J as object
* @param self<J>|class-string<J> $class
* @psalm-assert-if-true self<T&J> $this
* @psalm-pure
*/
public function isSubclassOf($class): bool {}
@ -245,8 +249,9 @@ class ReflectionClass implements Reflector {
public function isIterable(): bool {}
/**
* @param self|class-string $interface
*
* @template J as object
* @param self<J>|interface-string<J> $interface
* @psalm-assert-if-true self<T&J> $this
* @psalm-pure
*/
public function implementsInterface($interface): bool {}
@ -275,7 +280,7 @@ class ReflectionClass implements Reflector {
public function getShortName(): string {}
/**
* @return ?array<string>
* @return ?list<trait-string>
* @psalm-ignore-nullable-return
* @psalm-pure
*/

47
tests/ReflectionTest.php Normal file
View File

@ -0,0 +1,47 @@
<?php
declare(strict_types=1);
namespace Psalm\Tests;
use Psalm\Tests\Traits\ValidCodeAnalysisTestTrait;
class ReflectionTest extends TestCase
{
use ValidCodeAnalysisTestTrait;
public function providerValidCodeParse(): iterable
{
yield 'ReflectionClass::isSubclassOf' => [
'code' => <<<'PHP'
<?php
$a = new ReflectionClass(stdClass::class);
if (!$a->isSubclassOf(Iterator::class)) {
throw new Exception();
}
PHP,
'assertions' => ['$a===' => 'ReflectionClass<stdClass&Iterator>'],
];
yield 'ReflectionClass::implementsInterface' => [
'code' => <<<'PHP'
<?php
$a = new ReflectionClass(stdClass::class);
if (!$a->implementsInterface(Iterator::class)) {
throw new Exception();
}
PHP,
'assertions' => ['$a===' => 'ReflectionClass<stdClass&Iterator>'],
];
yield 'ReflectionClass::isInstance' => [
'code' => <<<'PHP'
<?php
$a = new stdClass();
$b = new ReflectionClass(Iterator::class);
if (!$b->isInstance($a)) {
throw new Exception();
}
PHP,
'assertions' => ['$a===' => 'Iterator&stdClass'],
];
}
}

View File

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