44
55use PHPStan \Node \Printer \ExprPrinter ;
66use PHPStan \Node \Printer \Printer ;
7+ use PHPStan \Php \PhpVersion ;
78use PHPStan \Rules \Rule ;
89use PHPStan \Rules \RuleLevelHelper ;
910use PHPStan \Testing \RuleTestCase ;
1011use PHPUnit \Framework \Attributes \RequiresPhp ;
12+ use function array_merge ;
13+ use function usort ;
14+ use const PHP_VERSION_ID ;
1115
1216/**
1317 * @extends RuleTestCase<InvalidBinaryOperationRule>
@@ -33,6 +37,7 @@ protected function getRule(): Rule
3337 checkBenevolentUnionTypes: false ,
3438 discoveringSymbolsTip: true ,
3539 ),
40+ new PhpVersion (PHP_VERSION_ID ),
3641 );
3742 }
3843
@@ -616,7 +621,7 @@ public function testBug10440(): void
616621
617622 public function testBenevolentUnion (): void
618623 {
619- $ this -> analyse ([ __DIR__ . ' /data/binary-op-benevolent-union.php ' ], [
624+ $ errors = [
620625 [
621626 'Binary operation "+" between (array<string, string>|bool|int|object|resource) and BinaryOpBenevolentUnion\Foo results in an error. ' ,
622627 12 ,
@@ -801,7 +806,15 @@ public function testBenevolentUnion(): void
801806 'Binary operation "|=" between BinaryOpBenevolentUnion \\Foo and (array<string, string>|bool|int|object|resource) results in an error. ' ,
802807 365 ,
803808 ],
804- ]);
809+ ];
810+ if (PHP_VERSION_ID >= 80100 ) {
811+ $ errors [] = [
812+ 'Deprecated in PHP 8.1: Implicit conversion from 1.23 to int loses precision. ' ,
813+ 138 ,
814+ ];
815+ usort ($ errors , static fn (array $ a , array $ b ): int => $ a [1 ] <=> $ b [1 ]);
816+ }
817+ $ this ->analyse ([__DIR__ . '/data/binary-op-benevolent-union.php ' ], $ errors );
805818 }
806819
807820 public function testBug12123 (): void
@@ -858,4 +871,172 @@ public function testBug1452(): void
858871 $ this ->analyse ([__DIR__ . '/data/bug-1452.php ' ], []);
859872 }
860873
874+ public function testBug8288 (): void
875+ {
876+ $ expectedErrors = [];
877+ if (PHP_VERSION_ID >= 80100 ) {
878+ $ expectedErrors = [
879+ [
880+ 'Deprecated in PHP 8.1: Implicit conversion from float to int loses precision. ' ,
881+ 17 ,
882+ ],
883+ [
884+ 'Deprecated in PHP 8.1: Implicit conversion from numeric-string to int loses precision. ' ,
885+ 18 ,
886+ ],
887+ [
888+ 'Deprecated in PHP 8.1: Implicit conversion from float to int loses precision. ' ,
889+ 19 ,
890+ ],
891+ [
892+ 'Deprecated in PHP 8.1: Implicit conversion from float to int loses precision. ' ,
893+ 20 ,
894+ ],
895+ [
896+ 'Deprecated in PHP 8.1: Implicit conversion from float to int loses precision. ' ,
897+ 21 ,
898+ ],
899+ [
900+ 'Deprecated in PHP 8.1: Implicit conversion from numeric-string to int loses precision. ' ,
901+ 22 ,
902+ ],
903+ [
904+ 'Deprecated in PHP 8.1: Implicit conversion from numeric-string to int loses precision. ' ,
905+ 23 ,
906+ ],
907+ [
908+ 'Deprecated in PHP 8.1: Implicit conversion from numeric-string to int loses precision. ' ,
909+ 24 ,
910+ ],
911+ ];
912+ }
913+ $ expectedErrors = array_merge ($ expectedErrors , [
914+ [
915+ 'Binary operation "%" between int and string results in an error. ' ,
916+ 26 ,
917+ ],
918+ [
919+ 'Binary operation "%" between int and Stringable results in an error. ' ,
920+ 27 ,
921+ ],
922+ [
923+ 'Binary operation "%" between int and array results in an error. ' ,
924+ 28 ,
925+ ],
926+ [
927+ 'Binary operation "%" between float and string results in an error. ' ,
928+ 29 ,
929+ ],
930+ [
931+ 'Binary operation "%" between float and Stringable results in an error. ' ,
932+ 30 ,
933+ ],
934+ [
935+ 'Binary operation "%" between float and array results in an error. ' ,
936+ 31 ,
937+ ],
938+ [
939+ 'Binary operation "%" between string and int results in an error. ' ,
940+ 32 ,
941+ ],
942+ [
943+ 'Binary operation "%" between string and float results in an error. ' ,
944+ 33 ,
945+ ],
946+ [
947+ 'Binary operation "%" between string and string results in an error. ' ,
948+ 34 ,
949+ ],
950+ [
951+ 'Binary operation "%" between string and numeric-string results in an error. ' ,
952+ 35 ,
953+ ],
954+ [
955+ 'Binary operation "%" between string and Stringable results in an error. ' ,
956+ 36 ,
957+ ],
958+ [
959+ 'Binary operation "%" between string and array results in an error. ' ,
960+ 37 ,
961+ ],
962+ [
963+ 'Binary operation "%" between numeric-string and string results in an error. ' ,
964+ 38 ,
965+ ],
966+ [
967+ 'Binary operation "%" between numeric-string and Stringable results in an error. ' ,
968+ 39 ,
969+ ],
970+ [
971+ 'Binary operation "%" between numeric-string and array results in an error. ' ,
972+ 40 ,
973+ ],
974+ [
975+ 'Binary operation "%" between Stringable and int results in an error. ' ,
976+ 41 ,
977+ ],
978+ [
979+ 'Binary operation "%" between Stringable and float results in an error. ' ,
980+ 42 ,
981+ ],
982+ [
983+ 'Binary operation "%" between Stringable and string results in an error. ' ,
984+ 43 ,
985+ ],
986+ [
987+ 'Binary operation "%" between Stringable and numeric-string results in an error. ' ,
988+ 44 ,
989+ ],
990+ [
991+ 'Binary operation "%" between Stringable and Stringable results in an error. ' ,
992+ 45 ,
993+ ],
994+ [
995+ 'Binary operation "%" between Stringable and array results in an error. ' ,
996+ 46 ,
997+ ],
998+ [
999+ 'Binary operation "%" between array and int results in an error. ' ,
1000+ 47 ,
1001+ ],
1002+ [
1003+ 'Binary operation "%" between array and float results in an error. ' ,
1004+ 48 ,
1005+ ],
1006+ [
1007+ 'Binary operation "%" between array and string results in an error. ' ,
1008+ 49 ,
1009+ ],
1010+ [
1011+ 'Binary operation "%" between array and numeric-string results in an error. ' ,
1012+ 50 ,
1013+ ],
1014+ [
1015+ 'Binary operation "%" between array and Stringable results in an error. ' ,
1016+ 51 ,
1017+ ],
1018+ [
1019+ 'Binary operation "%" between array and array results in an error. ' ,
1020+ 52 ,
1021+ ],
1022+ ]);
1023+ if (PHP_VERSION_ID >= 80100 ) {
1024+ $ expectedErrors = array_merge ($ expectedErrors , [
1025+ [
1026+ 'Deprecated in PHP 8.1: Implicit conversion from float|int<0, 15> to int loses precision. ' ,
1027+ 58 ,
1028+ ],
1029+ [
1030+ 'Deprecated in PHP 8.1: Implicit conversion from 6.0625 to int loses precision. ' ,
1031+ 59 ,
1032+ ],
1033+ [
1034+ 'Deprecated in PHP 8.1: Implicit conversion from 6.0625 to int loses precision. ' ,
1035+ 60 ,
1036+ ],
1037+ ]);
1038+ }
1039+ $ this ->analyse ([__DIR__ . '/data/bug8288.php ' ], $ expectedErrors );
1040+ }
1041+
8611042}
0 commit comments