Skip to content

Commit 0917670

Browse files
committed
Regression tests
Closes phpstan/phpstan#6370 Closes phpstan/phpstan#4413
1 parent 7461185 commit 0917670

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

tests/PHPStan/Rules/Classes/InstantiationRuleTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,4 +384,14 @@ public function testEnumInstantiation(): void
384384
]);
385385
}
386386

387+
public function testBug6370(): void
388+
{
389+
$this->analyse([__DIR__ . '/data/bug-6370.php'], [
390+
[
391+
'Parameter #1 $something of class Bug6370\A constructor expects string, int given.',
392+
45,
393+
],
394+
]);
395+
}
396+
387397
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
namespace Bug6370;
4+
5+
interface I
6+
{
7+
}
8+
class A implements I
9+
{
10+
public function __construct(string $something)
11+
{
12+
// one class needs a constructor to reproduce the issue
13+
unset($something);
14+
}
15+
}
16+
class B implements I
17+
{
18+
}
19+
20+
class Foo
21+
{
22+
23+
function test(string $className): I
24+
{
25+
if (!in_array($className, [B::class, A::class], true)) {
26+
throw new \Exception();
27+
}
28+
switch($className) {
29+
case A::class:
30+
return new $className('something');
31+
case B::class:
32+
return new $className();
33+
default:
34+
throw new \Exception();
35+
}
36+
}
37+
38+
function test2(string $className): I
39+
{
40+
if (!in_array($className, [B::class, A::class], true)) {
41+
throw new \Exception();
42+
}
43+
switch($className) {
44+
case A::class:
45+
return new $className(1);
46+
case B::class:
47+
return new $className();
48+
default:
49+
throw new \Exception();
50+
}
51+
}
52+
53+
}

tests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -930,4 +930,15 @@ public function testFirstClassCallables(): void
930930
$this->analyse([__DIR__ . '/data/first-class-callables.php'], []);
931931
}
932932

933+
public function testBug4413(): void
934+
{
935+
require_once __DIR__ . '/data/bug-4413.php';
936+
$this->analyse([__DIR__ . '/data/bug-4413.php'], [
937+
[
938+
'Parameter #1 $date of function Bug4413\takesDate expects class-string<DateTime>, string given.',
939+
18,
940+
],
941+
]);
942+
}
943+
933944
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Bug4413;
4+
5+
use DateTime;
6+
7+
/**
8+
* @param class-string<DateTime> $date
9+
*/
10+
function takesDate(string $date): void {}
11+
12+
function input(string $in): void {
13+
switch ($in) {
14+
case DateTime::class :
15+
takesDate($in);
16+
break;
17+
case \stdClass::class :
18+
takesDate($in);
19+
break;
20+
}
21+
}

0 commit comments

Comments
 (0)