Skip to content

Commit 51f9e83

Browse files
committed
add more tests for InvalidPartOfEncapsedStringRule
1 parent 53439c2 commit 51f9e83

File tree

3 files changed

+114
-4
lines changed

3 files changed

+114
-4
lines changed

tests/PHPStan/Rules/Cast/InvalidPartOfEncapsedStringRuleTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,19 @@ public function testRule(): void
2828
$this->analyse([__DIR__ . '/data/invalid-encapsed-part.php'], [
2929
[
3030
'Part $std (stdClass) of encapsed string cannot be cast to string.',
31-
8,
31+
26,
32+
],
33+
[
34+
'Part $array (array) of encapsed string cannot be cast to string.',
35+
30,
36+
],
37+
[
38+
'Part $std (stdClass|string) of encapsed string cannot be cast to string.',
39+
56,
40+
],
41+
[
42+
'Part $array (array|string) of encapsed string cannot be cast to string.',
43+
60,
3244
],
3345
]);
3446
}
@@ -44,4 +56,23 @@ public function testRuleWithNullsafeVariant(): void
4456
]);
4557
}
4658

59+
#[RequiresPhp('>= 8.1')]
60+
public function testRuleWithEnum(): void
61+
{
62+
$this->analyse([__DIR__ . '/data/invalid-encapsed-part-enum.php'], [
63+
[
64+
'Part $unitEnum (InvalidEncapsedPartEnum\\FooUnitEnum) of encapsed string cannot be cast to string.',
65+
21,
66+
],
67+
[
68+
'Part $intEnum (InvalidEncapsedPartEnum\\IntEnum) of encapsed string cannot be cast to string.',
69+
22,
70+
],
71+
[
72+
'Part $stringEnum (InvalidEncapsedPartEnum\\StringEnum) of encapsed string cannot be cast to string.',
73+
23,
74+
],
75+
]);
76+
}
77+
4778
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php // lint >= 8.1
2+
3+
namespace InvalidEncapsedPartEnum;
4+
5+
enum FooUnitEnum
6+
{
7+
case A;
8+
}
9+
10+
enum IntEnum: int
11+
{
12+
case A = 1;
13+
}
14+
15+
enum StringEnum: string
16+
{
17+
case A = 'a';
18+
}
19+
20+
function doFoo(FooUnitEnum $unitEnum, IntEnum $intEnum, StringEnum $stringEnum) {
21+
"{$unitEnum}";
22+
"{$intEnum}";
23+
"{$stringEnum}";
24+
}
Lines changed: 58 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,64 @@
11
<?php
22

3-
function (
3+
namespace InvalidPartOfEncapsedString;
4+
5+
class ClassWithToString
6+
{
7+
public function __toString(): string
8+
{
9+
return 'str';
10+
}
11+
}
12+
13+
function foo(
414
string $str,
5-
\stdClass $std
15+
\stdClass $std,
16+
bool $bool,
17+
int $int,
18+
float $float,
19+
array $array,
20+
ClassWithToString $objectWithToString
621
) {
22+
$null = null;
23+
$resource = fopen('php://input');
24+
assert($resource !== false);
725
"$str bar";
826
"$std bar";
9-
};
27+
"$bool bar";
28+
"$int bar";
29+
"$float bar";
30+
"$array bar";
31+
"$objectWithToString bar";
32+
"$null bar";
33+
"$resource bar";
34+
}
35+
36+
/**
37+
* @param string|\stdClass $std
38+
* @param string|bool $bool
39+
* @param string|int $int
40+
* @param string|float $float
41+
* @param string|array $array
42+
* @param string|ClassWithToString $objectWithToString
43+
* @param string|null $null
44+
* @param string|resource $resource
45+
*/
46+
function checkUnions(
47+
$std,
48+
$bool,
49+
$int,
50+
$float,
51+
$array,
52+
$objectWithToString,
53+
$null,
54+
$resource
55+
) {
56+
"$std bar";
57+
"$bool bar";
58+
"$int bar";
59+
"$float bar";
60+
"$array bar";
61+
"$objectWithToString bar";
62+
"$null bar";
63+
"$resource bar";
64+
}

0 commit comments

Comments
 (0)