Skip to content

Commit 70cd9e8

Browse files
committed
1 parent 21b5365 commit 70cd9e8

File tree

7 files changed

+141
-0
lines changed

7 files changed

+141
-0
lines changed

tests/PHPStan/Analyser/NodeScopeResolverTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,8 @@ public function dataFileAsserts(): iterable
223223
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3915.php');
224224

225225
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2378.php');
226+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6294.php');
227+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-2580.php');
226228

227229
yield from $this->gatherAssertTypes(__DIR__ . '/data/match-expr.php');
228230

@@ -1255,6 +1257,7 @@ public function dataFileAsserts(): iterable
12551257
yield from $this->gatherAssertTypes(__DIR__ . '/data/invalid-type-aliases.php');
12561258
yield from $this->gatherAssertTypes(__DIR__ . '/data/asymmetric-properties.php');
12571259
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9062.php');
1260+
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8092.php');
12581261
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Variables/data/bug-9403.php');
12591262
yield from $this->gatherAssertTypes(__DIR__ . '/data/object-shape.php');
12601263
yield from $this->gatherAssertTypes(__DIR__ . '/data/memcache-get.php');
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Bug2580;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
/**
8+
* @template T of object
9+
* @param class-string<T> $typeName
10+
* @param mixed $value
11+
*/
12+
function cast($value, string $typeName): void {
13+
if (is_object($value) && get_class($value) === $typeName) {
14+
assertType('T of object (function Bug2580\cast(), argument)', $value);
15+
}
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Bug6294;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
class HelloWorld1
8+
{
9+
/**
10+
* @phpstan-param object $object
11+
* @phpstan-param class-string<HelloWorld1> $classString
12+
* @phpstan-return HelloWorld1|null
13+
*/
14+
public function sayHello(object $object, $classString): ?object
15+
{
16+
if ($classString === get_class($object)) {
17+
assertType(HelloWorld1::class, $object);
18+
19+
return $object;
20+
}
21+
22+
return null;
23+
}
24+
25+
/**
26+
* @phpstan-param HelloWorld1 $object
27+
* @phpstan-return HelloWorld1|null
28+
*/
29+
public function sayHello2(object $object, object $object2): ?object
30+
{
31+
if (get_class($object2) === get_class($object)) {
32+
assertType(HelloWorld1::class, $object);
33+
34+
return $object;
35+
}
36+
37+
return null;
38+
}
39+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace Bug8092;
4+
5+
use function PHPStan\Testing\assertType;
6+
7+
interface Generic
8+
{}
9+
10+
class Specific implements Generic
11+
{}
12+
13+
/** @template-covariant T of Generic */
14+
interface TypeWithGeneric
15+
{
16+
/** @return T */
17+
public function get(): Generic;
18+
}
19+
20+
/** @implements TypeWithGeneric<Specific> */
21+
class TypeWithSpecific implements TypeWithGeneric
22+
{
23+
public function get(): Specific
24+
{
25+
return new Specific();
26+
}
27+
}
28+
29+
class HelloWorld
30+
{
31+
/** @param TypeWithGeneric<Generic> $type */
32+
public function test(TypeWithGeneric $type): void
33+
{
34+
match (get_class($type)) {
35+
TypeWithSpecific::class => assertType(TypeWithSpecific::class, $type),
36+
default => false,
37+
};
38+
}
39+
}

tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,4 +507,13 @@ public function testBug8614(): void
507507
$this->analyse([__DIR__ . '/data/bug-8614.php'], []);
508508
}
509509

510+
public function testBug8536(): void
511+
{
512+
if (PHP_VERSION_ID < 80100) {
513+
$this->markTestSkipped('Test requires PHP 8.1.');
514+
}
515+
516+
$this->analyse([__DIR__ . '/data/bug-8536.php'], []);
517+
}
518+
510519
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Bug8536;
4+
5+
final class A {
6+
public function __construct(public readonly string $id) {}
7+
}
8+
final class B {
9+
public function __construct(public readonly string $name) {}
10+
}
11+
12+
class Foo
13+
{
14+
15+
public function getValue(A|B $obj): string
16+
{
17+
return match(get_class($obj)) {
18+
A::class => $obj->id,
19+
B::class => $obj->name,
20+
};
21+
}
22+
23+
}

tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -901,4 +901,16 @@ public function testConflictingAnnotationProperty(): void
901901
$this->analyse([__DIR__ . '/data/conflicting-annotation-property.php'], []);
902902
}
903903

904+
public function testBug8536(): void
905+
{
906+
if (PHP_VERSION_ID < 80100) {
907+
$this->markTestSkipped('Test requires PHP 8.1.');
908+
}
909+
910+
$this->checkThisOnly = false;
911+
$this->checkUnionTypes = true;
912+
$this->checkDynamicProperties = true;
913+
$this->analyse([__DIR__ . '/../Comparison/data/bug-8536.php'], []);
914+
}
915+
904916
}

0 commit comments

Comments
 (0)