|
5 | 5 | use PHPStan\Rules\Rule;
|
6 | 6 | use PHPStan\Testing\RuleTestCase;
|
7 | 7 | use stdClass;
|
| 8 | +use function array_filter; |
| 9 | +use function array_map; |
| 10 | +use function array_values; |
| 11 | +use function count; |
8 | 12 | use const PHP_VERSION_ID;
|
9 | 13 |
|
10 | 14 | /**
|
@@ -252,6 +256,11 @@ public function testImpossibleCheckTypeFunctionCall(): void
|
252 | 256 | 889,
|
253 | 257 | 'Remove remaining cases below this one and this error will disappear too.',
|
254 | 258 | ],
|
| 259 | + [ |
| 260 | + 'Call to function in_array() with arguments 1, array<string> and true will always evaluate to false.', |
| 261 | + 927, |
| 262 | + 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.', |
| 263 | + ], |
255 | 264 | ],
|
256 | 265 | );
|
257 | 266 | }
|
@@ -357,6 +366,11 @@ public function testImpossibleCheckTypeFunctionCallWithoutAlwaysTrue(): void
|
357 | 366 | 'Call to function is_numeric() with \'blabla\' will always evaluate to false.',
|
358 | 367 | 694,
|
359 | 368 | ],
|
| 369 | + [ |
| 370 | + 'Call to function in_array() with arguments 1, array<string> and true will always evaluate to false.', |
| 371 | + 927, |
| 372 | + 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.', |
| 373 | + ], |
360 | 374 | ],
|
361 | 375 | );
|
362 | 376 | }
|
@@ -387,6 +401,16 @@ public function testReportTypesFromPhpDocs(): void
|
387 | 401 | 19,
|
388 | 402 | 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.',
|
389 | 403 | ],
|
| 404 | + [ |
| 405 | + 'Call to function in_array() with arguments int, array<string> and true will always evaluate to false.', |
| 406 | + 27, |
| 407 | + 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.', |
| 408 | + ], |
| 409 | + [ |
| 410 | + 'Call to function in_array() with arguments 1, array<string> and true will always evaluate to false.', |
| 411 | + 30, |
| 412 | + 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.', |
| 413 | + ], |
390 | 414 | ]);
|
391 | 415 | }
|
392 | 416 |
|
@@ -513,6 +537,11 @@ public function testBugInArrayDateFormat(): void
|
513 | 537 | 'Call to function in_array() with arguments int, array{} and true will always evaluate to false.',
|
514 | 538 | 47,
|
515 | 539 | ],
|
| 540 | + [ |
| 541 | + 'Call to function in_array() with arguments int, array<int, string> and true will always evaluate to false.', |
| 542 | + 61, |
| 543 | + 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.', |
| 544 | + ], |
516 | 545 | ]);
|
517 | 546 | }
|
518 | 547 |
|
@@ -813,15 +842,11 @@ public function testObjectShapes(): void
|
813 | 842 | ]);
|
814 | 843 | }
|
815 | 844 |
|
816 |
| - public function testLooseComparisonAgainstEnums(): void |
| 845 | + /** @return list<array{0: string, 1: int, 2?: string}> */ |
| 846 | + private static function getLooseComparisonAgainsEnumsIssues(): array |
817 | 847 | {
|
818 |
| - if (PHP_VERSION_ID < 80100) { |
819 |
| - $this->markTestSkipped('Test requires PHP 8.1.'); |
820 |
| - } |
821 |
| - |
822 |
| - $this->checkAlwaysTrueCheckTypeFunctionCall = true; |
823 |
| - $this->treatPhpDocTypesAsCertain = true; |
824 |
| - $this->analyse([__DIR__ . '/data/loose-comparison-against-enums.php'], [ |
| 848 | + $tipText = 'Because the type is coming from a PHPDoc, you can turn off this check by setting <fg=cyan>treatPhpDocTypesAsCertain: false</> in your <fg=cyan>%configurationFile%</>.'; |
| 849 | + return [ |
825 | 850 | [
|
826 | 851 | 'Call to function in_array() with LooseComparisonAgainstEnums\\FooUnitEnum and array{\'A\'} will always evaluate to false.',
|
827 | 852 | 21,
|
@@ -910,7 +935,105 @@ public function testLooseComparisonAgainstEnums(): void
|
910 | 935 | 'Call to function in_array() with null and array{LooseComparisonAgainstEnums\FooBackedEnum} will always evaluate to false.',
|
911 | 936 | 96,
|
912 | 937 | ],
|
913 |
| - ]); |
| 938 | + [ |
| 939 | + 'Call to function in_array() with LooseComparisonAgainstEnums\FooUnitEnum and array<string> will always evaluate to false.', |
| 940 | + 125, |
| 941 | + $tipText, |
| 942 | + ], |
| 943 | + [ |
| 944 | + 'Call to function in_array() with arguments LooseComparisonAgainstEnums\FooUnitEnum, array<string> and false will always evaluate to false.', |
| 945 | + 128, |
| 946 | + $tipText, |
| 947 | + ], |
| 948 | + [ |
| 949 | + 'Call to function in_array() with arguments LooseComparisonAgainstEnums\FooUnitEnum, array<string> and true will always evaluate to false.', |
| 950 | + 131, |
| 951 | + $tipText, |
| 952 | + ], |
| 953 | + [ |
| 954 | + 'Call to function in_array() with string and array<LooseComparisonAgainstEnums\FooUnitEnum> will always evaluate to false.', |
| 955 | + 143, |
| 956 | + $tipText, |
| 957 | + ], |
| 958 | + [ |
| 959 | + 'Call to function in_array() with arguments string, array<LooseComparisonAgainstEnums\FooUnitEnum> and false will always evaluate to false.', |
| 960 | + 146, |
| 961 | + $tipText, |
| 962 | + ], |
| 963 | + [ |
| 964 | + 'Call to function in_array() with arguments string, array<LooseComparisonAgainstEnums\FooUnitEnum> and true will always evaluate to false.', |
| 965 | + 149, |
| 966 | + $tipText, |
| 967 | + ], |
| 968 | + [ |
| 969 | + 'Call to function in_array() with LooseComparisonAgainstEnums\FooUnitEnum::B and non-empty-array<LooseComparisonAgainstEnums\FooUnitEnum::A> will always evaluate to false.', |
| 970 | + 159, |
| 971 | + $tipText, |
| 972 | + ], |
| 973 | + [ |
| 974 | + 'Call to function in_array() with LooseComparisonAgainstEnums\FooUnitEnum::A and non-empty-array<LooseComparisonAgainstEnums\FooUnitEnum::A> will always evaluate to true.', |
| 975 | + 162, |
| 976 | + $tipText, |
| 977 | + ], |
| 978 | + [ |
| 979 | + 'Call to function in_array() with arguments LooseComparisonAgainstEnums\FooUnitEnum::A, non-empty-array<LooseComparisonAgainstEnums\FooUnitEnum::A> and false will always evaluate to true.', |
| 980 | + 165, |
| 981 | + 'BUG', |
| 982 | + //$tipText, |
| 983 | + ], |
| 984 | + [ |
| 985 | + 'Call to function in_array() with arguments LooseComparisonAgainstEnums\FooUnitEnum::A, non-empty-array<LooseComparisonAgainstEnums\FooUnitEnum::A> and true will always evaluate to true.', |
| 986 | + 168, |
| 987 | + 'BUG', |
| 988 | + //$tipText, |
| 989 | + ], |
| 990 | + [ |
| 991 | + 'Call to function in_array() with arguments LooseComparisonAgainstEnums\FooUnitEnum::B, non-empty-array<LooseComparisonAgainstEnums\FooUnitEnum::A> and false will always evaluate to false.', |
| 992 | + 171, |
| 993 | + 'BUG', |
| 994 | + //$tipText, |
| 995 | + ], |
| 996 | + [ |
| 997 | + 'Call to function in_array() with arguments LooseComparisonAgainstEnums\FooUnitEnum::B, non-empty-array<LooseComparisonAgainstEnums\FooUnitEnum::A> and true will always evaluate to false.', |
| 998 | + 174, |
| 999 | + 'BUG', |
| 1000 | + //$tipText, |
| 1001 | + ], |
| 1002 | + ]; |
| 1003 | + } |
| 1004 | + |
| 1005 | + public function testLooseComparisonAgainstEnums(): void |
| 1006 | + { |
| 1007 | + if (PHP_VERSION_ID < 80100) { |
| 1008 | + $this->markTestSkipped('Test requires PHP 8.1.'); |
| 1009 | + } |
| 1010 | + |
| 1011 | + $this->checkAlwaysTrueCheckTypeFunctionCall = true; |
| 1012 | + $this->treatPhpDocTypesAsCertain = true; |
| 1013 | + $issues = array_map( |
| 1014 | + static function (array $i): array { |
| 1015 | + if (($i[2] ?? null) === 'BUG') { |
| 1016 | + unset($i[2]); |
| 1017 | + } |
| 1018 | + |
| 1019 | + return $i; |
| 1020 | + }, |
| 1021 | + self::getLooseComparisonAgainsEnumsIssues(), |
| 1022 | + ); |
| 1023 | + $this->analyse([__DIR__ . '/data/loose-comparison-against-enums.php'], $issues); |
| 1024 | + } |
| 1025 | + |
| 1026 | + public function testLooseComparisonAgainstEnumsNoPhpdoc(): void |
| 1027 | + { |
| 1028 | + if (PHP_VERSION_ID < 80100) { |
| 1029 | + $this->markTestSkipped('Test requires PHP 8.1.'); |
| 1030 | + } |
| 1031 | + |
| 1032 | + $this->checkAlwaysTrueCheckTypeFunctionCall = true; |
| 1033 | + $this->treatPhpDocTypesAsCertain = false; |
| 1034 | + $issues = self::getLooseComparisonAgainsEnumsIssues(); |
| 1035 | + $issues = array_values(array_filter($issues, static fn (array $i) => count($i) === 2)); |
| 1036 | + $this->analyse([__DIR__ . '/data/loose-comparison-against-enums.php'], $issues); |
914 | 1037 | }
|
915 | 1038 |
|
916 | 1039 | }
|
0 commit comments