6
6
use PHPStan \Node \Printer \Printer ;
7
7
use PHPStan \Rules \Rule ;
8
8
use PHPStan \Rules \RuleLevelHelper ;
9
+ use PHPStan \Rules \TypeCoercionRuleHelper ;
9
10
use PHPStan \Testing \RuleTestCase ;
10
11
use PHPUnit \Framework \Attributes \RequiresPhp ;
11
12
15
16
class InvalidPartOfEncapsedStringRuleTest extends RuleTestCase
16
17
{
17
18
19
+ private ?TypeCoercionRuleHelper $ typeCoercionRuleHelper = null ;
20
+
18
21
protected function getRule (): Rule
19
22
{
20
23
return new InvalidPartOfEncapsedStringRule (
21
24
new ExprPrinter (new Printer ()),
22
25
new RuleLevelHelper (self ::createReflectionProvider (), true , false , true , false , false , false , true ),
26
+ $ this ->typeCoercionRuleHelper ?? new TypeCoercionRuleHelper (true , true ),
23
27
);
24
28
}
25
29
@@ -45,6 +49,37 @@ public function testRule(): void
45
49
]);
46
50
}
47
51
52
+ public function testRuleWithStrictCoercions (): void
53
+ {
54
+ $ this ->typeCoercionRuleHelper = new TypeCoercionRuleHelper (true , false );
55
+ $ this ->analyse ([__DIR__ . '/data/invalid-encapsed-part.php ' ], [
56
+ [
57
+ 'Part $std (stdClass) of encapsed string cannot be cast to string. ' ,
58
+ 26 ,
59
+ ],
60
+ [
61
+ 'Part $bool (bool) of encapsed string cannot be cast to string. ' ,
62
+ 27 ,
63
+ ],
64
+ [
65
+ 'Part $array (array) of encapsed string cannot be cast to string. ' ,
66
+ 30 ,
67
+ ],
68
+ [
69
+ 'Part $std (stdClass|string) of encapsed string cannot be cast to string. ' ,
70
+ 56 ,
71
+ ],
72
+ [
73
+ 'Part $bool (bool|string) of encapsed string cannot be cast to string. ' ,
74
+ 57 ,
75
+ ],
76
+ [
77
+ 'Part $array (array|string) of encapsed string cannot be cast to string. ' ,
78
+ 60 ,
79
+ ],
80
+ ]);
81
+ }
82
+
48
83
#[RequiresPhp('>= 8.0 ' )]
49
84
public function testRuleWithNullsafeVariant (): void
50
85
{
0 commit comments