Skip to content

Commit cf5b91e

Browse files
committed
Type acceptance tests
1 parent 1065a0f commit cf5b91e

4 files changed

Lines changed: 86 additions & 0 deletions

tests/PHPStan/Analyser/ReportUnsafeArrayStringKeyCastingDetectTypeAcceptanceTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,16 @@ public function testRule(): void
2929
'Parameter #1 $a of method ReportUnsafeArrayStringKeyCastingAccepts\Foo::doBaz() expects array<non-decimal-int-string, stdClass>, non-empty-array<string, stdClass> given.',
3030
39,
3131
],
32+
[
33+
'Parameter #1 $a of method ReportUnsafeArrayStringKeyCastingAccepts\UnsealedArrayShape::doBaz() expects array{stdClass, ...<non-decimal-int-string, stdClass>}, array{stdClass, ...<string, stdClass>} given.',
34+
79,
35+
'Unsealed array key type non-decimal-int-string does not accept unsealed array key type string.',
36+
],
37+
[
38+
'Parameter #1 $a of method ReportUnsafeArrayStringKeyCastingAccepts\UnsealedArrayShape::doBaz() expects array{stdClass, ...<non-decimal-int-string, stdClass>}, array{stdClass, ...<string, stdClass>} given.',
39+
85,
40+
'Unsealed array key type non-decimal-int-string does not accept unsealed array key type string.',
41+
],
3242
]);
3343
}
3444

tests/PHPStan/Analyser/ReportUnsafeArrayStringKeyCastingPreventTypeAcceptanceTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,26 @@ public function testRule(): void
3737
'Parameter #1 $a of method ReportUnsafeArrayStringKeyCastingAccepts\Foo::doBaz() expects array<non-decimal-int-string, stdClass>, non-empty-array<int|non-decimal-int-string, stdClass> given.',
3838
39,
3939
],
40+
[
41+
'Parameter #1 $a of method ReportUnsafeArrayStringKeyCastingAccepts\UnsealedArrayShape::doFoo() expects array{stdClass, ...<non-decimal-int-string, stdClass>}, array{stdClass, ...<int<min, -1>|int<1, max>|non-decimal-int-string, stdClass>} given.',
42+
77,
43+
'Unsealed array key type non-decimal-int-string does not accept unsealed array key type int<min, -1>|int<1, max>|non-decimal-int-string.',
44+
],
45+
[
46+
'Parameter #1 $a of method ReportUnsafeArrayStringKeyCastingAccepts\UnsealedArrayShape::doBaz() expects array{stdClass, ...<non-decimal-int-string, stdClass>}, array{stdClass, ...<int<min, -1>|int<1, max>|non-decimal-int-string, stdClass>} given.',
47+
79,
48+
'Unsealed array key type non-decimal-int-string does not accept unsealed array key type int<min, -1>|int<1, max>|non-decimal-int-string.',
49+
],
50+
[
51+
'Parameter #1 $a of method ReportUnsafeArrayStringKeyCastingAccepts\UnsealedArrayShape::doFoo() expects array{stdClass, ...<non-decimal-int-string, stdClass>}, array{stdClass, ...<int<min, -1>|int<1, max>|non-decimal-int-string, stdClass>} given.',
52+
83,
53+
'Unsealed array key type non-decimal-int-string does not accept unsealed array key type int<min, -1>|int<1, max>|non-decimal-int-string.',
54+
],
55+
[
56+
'Parameter #1 $a of method ReportUnsafeArrayStringKeyCastingAccepts\UnsealedArrayShape::doBaz() expects array{stdClass, ...<non-decimal-int-string, stdClass>}, array{stdClass, ...<int<min, -1>|int<1, max>|non-decimal-int-string, stdClass>} given.',
57+
85,
58+
'Unsealed array key type non-decimal-int-string does not accept unsealed array key type int<min, -1>|int<1, max>|non-decimal-int-string.',
59+
],
4060
]);
4161
}
4262

tests/PHPStan/Analyser/ReportUnsafeArrayStringKeyCastingUnsafeTypeAcceptanceTest.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ public function testRule(): void
2828
'Parameter #1 $a of method ReportUnsafeArrayStringKeyCastingAccepts\Foo::doBaz() expects array<non-decimal-int-string, stdClass>, non-empty-array<string, stdClass> given.',
2929
39,
3030
],
31+
[
32+
'Parameter #1 $a of method ReportUnsafeArrayStringKeyCastingAccepts\UnsealedArrayShape::doBaz() expects array{stdClass, ...<non-decimal-int-string, stdClass>}, array{stdClass, ...<string, stdClass>} given.',
33+
79,
34+
'Unsealed array key type non-decimal-int-string does not accept unsealed array key type string.',
35+
],
36+
[
37+
'Parameter #1 $a of method ReportUnsafeArrayStringKeyCastingAccepts\UnsealedArrayShape::doBaz() expects array{stdClass, ...<non-decimal-int-string, stdClass>}, array{stdClass, ...<string, stdClass>} given.',
38+
85,
39+
'Unsealed array key type non-decimal-int-string does not accept unsealed array key type string.',
40+
],
3141
]);
3242
}
3343

tests/PHPStan/Analyser/data/report-unsafe-array-string-key-casting-accepts.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,49 @@ public function doTest(string $s): void
4040
}
4141

4242
}
43+
44+
class UnsealedArrayShape
45+
{
46+
47+
/**
48+
* @param array{stdClass, ...<string, stdClass>} $a
49+
* @return void
50+
*/
51+
public function doFoo(array $a): void
52+
{
53+
54+
}
55+
56+
/**
57+
* @param array{stdClass, ...<int|string, stdClass>} $a
58+
* @return void
59+
*/
60+
public function doBar(array $a): void
61+
{
62+
63+
}
64+
65+
/**
66+
* @param array{stdClass, ...<non-decimal-int-string, stdClass>} $a
67+
* @return void
68+
*/
69+
public function doBaz(array $a): void
70+
{
71+
72+
}
73+
74+
public function doTest(string $s): void
75+
{
76+
$a = [new stdClass(), $s => new stdClass()];
77+
$this->doFoo($a);
78+
$this->doBar($a);
79+
$this->doBaz($a);
80+
81+
$b = [new stdClass()];
82+
$b[$s] = new stdClass();
83+
$this->doFoo($b);
84+
$this->doBar($b);
85+
$this->doBaz($b);
86+
}
87+
88+
}

0 commit comments

Comments
 (0)