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 (
2124 new ExprPrinter (new Printer ()),
2225 new RuleLevelHelper (self ::createReflectionProvider (), true , false , true , false , false , false , true ),
26+ $ this ->typeCoercionRuleHelper ?? new TypeCoercionRuleHelper (true , true ),
2327 );
2428 }
2529
@@ -45,6 +49,37 @@ public function testRule(): void
4549 ]);
4650 }
4751
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+
4883 #[RequiresPhp('>= 8.0 ' )]
4984 public function testRuleWithNullsafeVariant (): void
5085 {
0 commit comments