66use PHPStan \Node \Printer \Printer ;
77use PHPStan \Rules \Rule ;
88use PHPStan \Rules \RuleLevelHelper ;
9+ use PHPStan \Rules \TypeCoercionRuleHelper ;
910use PHPStan \Testing \RuleTestCase ;
1011use PHPUnit \Framework \Attributes \RequiresPhp ;
1112
1516class InvalidPartOfEncapsedStringRuleTest extends RuleTestCase
1617{
1718
19+ private ?TypeCoercionRuleHelper $ typeCoercionRuleHelper = null ;
20+
1821 protected function getRule (): Rule
1922 {
2023 return new InvalidPartOfEncapsedStringRule (
@@ -29,6 +32,7 @@ protected function getRule(): Rule
2932 checkBenevolentUnionTypes: false ,
3033 discoveringSymbolsTip: true ,
3134 ),
35+ $ this ->typeCoercionRuleHelper ?? new TypeCoercionRuleHelper (true , true ),
3236 );
3337 }
3438
@@ -54,6 +58,37 @@ public function testRule(): void
5458 ]);
5559 }
5660
61+ public function testRuleWithStrictCoercions (): void
62+ {
63+ $ this ->typeCoercionRuleHelper = new TypeCoercionRuleHelper (true , false );
64+ $ this ->analyse ([__DIR__ . '/data/invalid-encapsed-part.php ' ], [
65+ [
66+ 'Part $std (stdClass) of encapsed string cannot be cast to string. ' ,
67+ 26 ,
68+ ],
69+ [
70+ 'Part $bool (bool) of encapsed string cannot be cast to string. ' ,
71+ 27 ,
72+ ],
73+ [
74+ 'Part $array (array) of encapsed string cannot be cast to string. ' ,
75+ 30 ,
76+ ],
77+ [
78+ 'Part $std (stdClass|string) of encapsed string cannot be cast to string. ' ,
79+ 56 ,
80+ ],
81+ [
82+ 'Part $bool (bool|string) of encapsed string cannot be cast to string. ' ,
83+ 57 ,
84+ ],
85+ [
86+ 'Part $array (array|string) of encapsed string cannot be cast to string. ' ,
87+ 60 ,
88+ ],
89+ ]);
90+ }
91+
5792 #[RequiresPhp('>= 8.0.0 ' )]
5893 public function testRuleWithNullsafeVariant (): void
5994 {
0 commit comments