Skip to content

Commit

Permalink
ReflectionClass::implementsInterface() and ReflectionClass::isSubclas…
Browse files Browse the repository at this point in the history
…sOf() template improvements
  • Loading branch information
jack-worman committed Dec 18, 2022
1 parent c529709 commit 9f15a91
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
8 changes: 6 additions & 2 deletions stubs/Php80.phpstub
Expand Up @@ -89,14 +89,18 @@ 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
*/
Expand Down
8 changes: 6 additions & 2 deletions stubs/Reflection.phpstub
Expand Up @@ -222,7 +222,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
*/
Expand All @@ -245,7 +247,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
*/
Expand Down
36 changes: 36 additions & 0 deletions tests/ReflectionTest.php
@@ -0,0 +1,36 @@
<?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>'],
];
}
}
10 changes: 9 additions & 1 deletion tests/Traits/ValidCodeAnalysisTestTrait.php
Expand Up @@ -18,7 +18,15 @@
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;

Expand Down

0 comments on commit 9f15a91

Please sign in to comment.