-
Notifications
You must be signed in to change notification settings - Fork 472
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes phpstan/phpstan#8536 Closes phpstan/phpstan#8092 Closes phpstan/phpstan#6294 Closes phpstan/phpstan#2580
- Loading branch information
1 parent
21b5365
commit 70cd9e8
Showing
7 changed files
with
141 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php | ||
|
||
namespace Bug2580; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
/** | ||
* @template T of object | ||
* @param class-string<T> $typeName | ||
* @param mixed $value | ||
*/ | ||
function cast($value, string $typeName): void { | ||
if (is_object($value) && get_class($value) === $typeName) { | ||
assertType('T of object (function Bug2580\cast(), argument)', $value); | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
namespace Bug6294; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class HelloWorld1 | ||
{ | ||
/** | ||
* @phpstan-param object $object | ||
* @phpstan-param class-string<HelloWorld1> $classString | ||
* @phpstan-return HelloWorld1|null | ||
*/ | ||
public function sayHello(object $object, $classString): ?object | ||
{ | ||
if ($classString === get_class($object)) { | ||
assertType(HelloWorld1::class, $object); | ||
|
||
return $object; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @phpstan-param HelloWorld1 $object | ||
* @phpstan-return HelloWorld1|null | ||
*/ | ||
public function sayHello2(object $object, object $object2): ?object | ||
{ | ||
if (get_class($object2) === get_class($object)) { | ||
assertType(HelloWorld1::class, $object); | ||
|
||
return $object; | ||
} | ||
|
||
return null; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
|
||
namespace Bug8092; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
interface Generic | ||
{} | ||
|
||
class Specific implements Generic | ||
{} | ||
|
||
/** @template-covariant T of Generic */ | ||
interface TypeWithGeneric | ||
{ | ||
/** @return T */ | ||
public function get(): Generic; | ||
} | ||
|
||
/** @implements TypeWithGeneric<Specific> */ | ||
class TypeWithSpecific implements TypeWithGeneric | ||
{ | ||
public function get(): Specific | ||
{ | ||
return new Specific(); | ||
} | ||
} | ||
|
||
class HelloWorld | ||
{ | ||
/** @param TypeWithGeneric<Generic> $type */ | ||
public function test(TypeWithGeneric $type): void | ||
{ | ||
match (get_class($type)) { | ||
TypeWithSpecific::class => assertType(TypeWithSpecific::class, $type), | ||
default => false, | ||
}; | ||
} | ||
} |
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,23 @@ | ||
<?php | ||
|
||
namespace Bug8536; | ||
|
||
final class A { | ||
public function __construct(public readonly string $id) {} | ||
} | ||
final class B { | ||
public function __construct(public readonly string $name) {} | ||
} | ||
|
||
class Foo | ||
{ | ||
|
||
public function getValue(A|B $obj): string | ||
{ | ||
return match(get_class($obj)) { | ||
A::class => $obj->id, | ||
B::class => $obj->name, | ||
}; | ||
} | ||
|
||
} |
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