Skip to content

Commit d3f3b18

Browse files
schlndhondrejmirtes
authored andcommitted
add more tests for InvalidPartOfEncapsedStringRule
1 parent 091c5a7 commit d3f3b18

3 files changed

Lines changed: 114 additions & 4 deletions

File tree

‎tests/PHPStan/Rules/Cast/InvalidPartOfEncapsedStringRuleTest.php‎

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,19 @@ public function testRule(): void
3737
$this->analyse([__DIR__ . '/data/invalid-encapsed-part.php'], [
3838
[
3939
'Part $std (stdClass) of encapsed string cannot be cast to string.',
40-
8,
40+
26,
41+
],
42+
[
43+
'Part $array (array) of encapsed string cannot be cast to string.',
44+
30,
45+
],
46+
[
47+
'Part $std (stdClass|string) of encapsed string cannot be cast to string.',
48+
56,
49+
],
50+
[
51+
'Part $array (array|string) of encapsed string cannot be cast to string.',
52+
60,
4153
],
4254
]);
4355
}
@@ -53,4 +65,23 @@ public function testRuleWithNullsafeVariant(): void
5365
]);
5466
}
5567

68+
#[RequiresPhp('>= 8.1')]
69+
public function testRuleWithEnum(): void
70+
{
71+
$this->analyse([__DIR__ . '/data/invalid-encapsed-part-enum.php'], [
72+
[
73+
'Part $unitEnum (InvalidEncapsedPartEnum\\FooUnitEnum) of encapsed string cannot be cast to string.',
74+
21,
75+
],
76+
[
77+
'Part $intEnum (InvalidEncapsedPartEnum\\IntEnum) of encapsed string cannot be cast to string.',
78+
22,
79+
],
80+
[
81+
'Part $stringEnum (InvalidEncapsedPartEnum\\StringEnum) of encapsed string cannot be cast to string.',
82+
23,
83+
],
84+
]);
85+
}
86+
5687
}
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)