Skip to content

Commit

Permalink
Regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 20, 2023
1 parent 21b5365 commit 70cd9e8
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-3915.php');

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

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

Expand Down Expand Up @@ -1255,6 +1257,7 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/data/invalid-type-aliases.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/asymmetric-properties.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-9062.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-8092.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Variables/data/bug-9403.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/object-shape.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/memcache-get.php');
Expand Down
16 changes: 16 additions & 0 deletions tests/PHPStan/Analyser/data/bug-2580.php
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);
}
}
39 changes: 39 additions & 0 deletions tests/PHPStan/Analyser/data/bug-6294.php
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;
}
}
39 changes: 39 additions & 0 deletions tests/PHPStan/Analyser/data/bug-8092.php
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,
};
}
}
9 changes: 9 additions & 0 deletions tests/PHPStan/Rules/Comparison/MatchExpressionRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -507,4 +507,13 @@ public function testBug8614(): void
$this->analyse([__DIR__ . '/data/bug-8614.php'], []);
}

public function testBug8536(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->analyse([__DIR__ . '/data/bug-8536.php'], []);
}

}
23 changes: 23 additions & 0 deletions tests/PHPStan/Rules/Comparison/data/bug-8536.php
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,
};
}

}
12 changes: 12 additions & 0 deletions tests/PHPStan/Rules/Properties/AccessPropertiesRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -901,4 +901,16 @@ public function testConflictingAnnotationProperty(): void
$this->analyse([__DIR__ . '/data/conflicting-annotation-property.php'], []);
}

public function testBug8536(): void
{
if (PHP_VERSION_ID < 80100) {
$this->markTestSkipped('Test requires PHP 8.1.');
}

$this->checkThisOnly = false;
$this->checkUnionTypes = true;
$this->checkDynamicProperties = true;
$this->analyse([__DIR__ . '/../Comparison/data/bug-8536.php'], []);
}

}

0 comments on commit 70cd9e8

Please sign in to comment.