-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix implicit
@phpstan-assert
PHPDoc inheritance with generics
- Loading branch information
Showing
6 changed files
with
274 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
<?php | ||
|
||
namespace AssertInheritance; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
/** | ||
* @template T | ||
*/ | ||
interface WrapperInterface | ||
{ | ||
/** | ||
* @phpstan-assert T $param | ||
*/ | ||
public function assert(mixed $param): void; | ||
|
||
/** | ||
* @phpstan-assert-if-true T $param | ||
*/ | ||
public function supports(mixed $param): bool; | ||
|
||
/** | ||
* @phpstan-assert-if-false T $param | ||
*/ | ||
public function notSupports(mixed $param): bool; | ||
} | ||
|
||
/** | ||
* @implements WrapperInterface<int> | ||
*/ | ||
class IntWrapper implements WrapperInterface | ||
{ | ||
public function assert(mixed $param): void | ||
{ | ||
} | ||
|
||
public function supports(mixed $param): bool | ||
{ | ||
return is_int($param); | ||
} | ||
|
||
public function notSupports(mixed $param): bool | ||
{ | ||
return !is_int($param); | ||
} | ||
} | ||
|
||
/** | ||
* @template T of object | ||
* @implements WrapperInterface<T> | ||
*/ | ||
abstract class ObjectWrapper implements WrapperInterface | ||
{ | ||
} | ||
|
||
/** | ||
* @extends ObjectWrapper<\DateTimeInterface> | ||
*/ | ||
class DateTimeInterfaceWrapper extends ObjectWrapper | ||
{ | ||
public function assert(mixed $param): void | ||
{ | ||
} | ||
|
||
public function supports(mixed $param): bool | ||
{ | ||
return $param instanceof \DateTimeInterface; | ||
} | ||
|
||
public function notSupports(mixed $param): bool | ||
{ | ||
return !$param instanceof \DateTimeInterface; | ||
} | ||
} | ||
|
||
function (IntWrapper $test, $val) { | ||
if ($test->supports($val)) { | ||
assertType('int', $val); | ||
} else { | ||
assertType('mixed~int', $val); | ||
} | ||
|
||
if ($test->notSupports($val)) { | ||
assertType('mixed~int', $val); | ||
} else { | ||
assertType('int', $val); | ||
} | ||
|
||
assertType('mixed', $val); | ||
$test->assert($val); | ||
assertType('int', $val); | ||
}; | ||
|
||
function (DateTimeInterfaceWrapper $test, $val) { | ||
if ($test->supports($val)) { | ||
assertType('DateTimeInterface', $val); | ||
} else { | ||
assertType('mixed~DateTimeInterface', $val); | ||
} | ||
|
||
if ($test->notSupports($val)) { | ||
assertType('mixed~DateTimeInterface', $val); | ||
} else { | ||
assertType('DateTimeInterface', $val); | ||
} | ||
|
||
assertType('mixed', $val); | ||
$test->assert($val); | ||
assertType('DateTimeInterface', $val); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace Bug10037; | ||
|
||
interface Identifier | ||
{} | ||
|
||
interface Document | ||
{} | ||
|
||
/** @template T of Identifier */ | ||
interface Fetcher | ||
{ | ||
/** @phpstan-assert-if-true T $identifier */ | ||
public function supports(Identifier $identifier): bool; | ||
|
||
/** @param T $identifier */ | ||
public function fetch(Identifier $identifier): Document; | ||
} | ||
|
||
/** @implements Fetcher<PostIdentifier> */ | ||
final readonly class PostFetcher implements Fetcher | ||
{ | ||
public function supports(Identifier $identifier): bool | ||
{ | ||
return $identifier instanceof PostIdentifier; | ||
} | ||
|
||
public function fetch(Identifier $identifier): Document | ||
{ | ||
// SA knows $identifier is instance of PostIdentifier here | ||
return $identifier->foo(); | ||
} | ||
} | ||
|
||
class PostIdentifier implements Identifier | ||
{ | ||
public function foo(): Document | ||
{ | ||
return new class implements Document{}; | ||
} | ||
} | ||
|
||
function (Identifier $i): void { | ||
$fetcher = new PostFetcher(); | ||
\PHPStan\Testing\assertType('Bug10037\Identifier', $i); | ||
if ($fetcher->supports($i)) { | ||
\PHPStan\Testing\assertType('Bug10037\PostIdentifier', $i); | ||
$fetcher->fetch($i); | ||
} else { | ||
$fetcher->fetch($i); | ||
} | ||
}; | ||
|
||
class Post | ||
{ | ||
} | ||
|
||
/** @template T */ | ||
abstract class Voter | ||
{ | ||
|
||
/** @phpstan-assert-if-true T $subject */ | ||
abstract function supports(string $attribute, mixed $subject): bool; | ||
|
||
/** @param T $subject */ | ||
abstract function voteOnAttribute(string $attribute, mixed $subject): bool; | ||
|
||
} | ||
|
||
/** @extends Voter<Post> */ | ||
class PostVoter extends Voter | ||
{ | ||
|
||
/** @phpstan-assert-if-true Post $subject */ | ||
function supports(string $attribute, mixed $subject): bool | ||
{ | ||
|
||
} | ||
|
||
function voteOnAttribute(string $attribute, mixed $subject): bool | ||
{ | ||
\PHPStan\Testing\assertType('Bug10037\Post', $subject); | ||
} | ||
} | ||
|
||
function ($subject): void { | ||
$voter = new PostVoter(); | ||
\PHPStan\Testing\assertType('mixed', $subject); | ||
if ($voter->supports('aaa', $subject)) { | ||
\PHPStan\Testing\assertType('Bug10037\Post', $subject); | ||
$voter->voteOnAttribute('aaa', $subject); | ||
} else { | ||
$voter->voteOnAttribute('aaa', $subject); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Bug9123; | ||
|
||
interface Event {} | ||
|
||
class MyEvent implements Event {} | ||
|
||
/** @template T of Event */ | ||
interface EventListener | ||
{ | ||
/** @phpstan-assert-if-true T $event */ | ||
public function canBeListen(Event $event): bool; | ||
|
||
public function listen(Event $event): void; | ||
} | ||
|
||
/** @implements EventListener<MyEvent> */ | ||
final class Implementation implements EventListener | ||
{ | ||
public function canBeListen(Event $event): bool | ||
{ | ||
return $event instanceof MyEvent; | ||
} | ||
|
||
public function listen(Event $event): void | ||
{ | ||
if (! $this->canBeListen($event)) { | ||
return; | ||
} | ||
|
||
\PHPStan\Testing\assertType('Bug9123\MyEvent', $event); | ||
} | ||
} | ||
|
||
/** @implements EventListener<MyEvent> */ | ||
final class Implementation2 implements EventListener | ||
{ | ||
/** @phpstan-assert-if-true MyEvent $event */ | ||
public function canBeListen(Event $event): bool | ||
{ | ||
return $event instanceof MyEvent; | ||
} | ||
|
||
public function listen(Event $event): void | ||
{ | ||
if (! $this->canBeListen($event)) { | ||
return; | ||
} | ||
|
||
\PHPStan\Testing\assertType('Bug9123\MyEvent', $event); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters