Skip to content

Commit

Permalink
Regression tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jan 15, 2022
1 parent 7461185 commit 0917670
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
10 changes: 10 additions & 0 deletions tests/PHPStan/Rules/Classes/InstantiationRuleTest.php
Expand Up @@ -384,4 +384,14 @@ public function testEnumInstantiation(): void
]);
}

public function testBug6370(): void
{
$this->analyse([__DIR__ . '/data/bug-6370.php'], [
[
'Parameter #1 $something of class Bug6370\A constructor expects string, int given.',
45,
],
]);
}

}
53 changes: 53 additions & 0 deletions tests/PHPStan/Rules/Classes/data/bug-6370.php
@@ -0,0 +1,53 @@
<?php

namespace Bug6370;

interface I
{
}
class A implements I
{
public function __construct(string $something)
{
// one class needs a constructor to reproduce the issue
unset($something);
}
}
class B implements I
{
}

class Foo
{

function test(string $className): I
{
if (!in_array($className, [B::class, A::class], true)) {
throw new \Exception();
}
switch($className) {
case A::class:
return new $className('something');
case B::class:
return new $className();
default:
throw new \Exception();
}
}

function test2(string $className): I
{
if (!in_array($className, [B::class, A::class], true)) {
throw new \Exception();
}
switch($className) {
case A::class:
return new $className(1);
case B::class:
return new $className();
default:
throw new \Exception();
}
}

}
11 changes: 11 additions & 0 deletions tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php
Expand Up @@ -930,4 +930,15 @@ public function testFirstClassCallables(): void
$this->analyse([__DIR__ . '/data/first-class-callables.php'], []);
}

public function testBug4413(): void
{
require_once __DIR__ . '/data/bug-4413.php';
$this->analyse([__DIR__ . '/data/bug-4413.php'], [
[
'Parameter #1 $date of function Bug4413\takesDate expects class-string<DateTime>, string given.',
18,
],
]);
}

}
21 changes: 21 additions & 0 deletions tests/PHPStan/Rules/Functions/data/bug-4413.php
@@ -0,0 +1,21 @@
<?php

namespace Bug4413;

use DateTime;

/**
* @param class-string<DateTime> $date
*/
function takesDate(string $date): void {}

function input(string $in): void {
switch ($in) {
case DateTime::class :
takesDate($in);
break;
case \stdClass::class :
takesDate($in);
break;
}
}

0 comments on commit 0917670

Please sign in to comment.