diff --git a/stubs/CoreGenericFunctions.phpstub b/stubs/CoreGenericFunctions.phpstub index a03447b1f4b..5e2640e1a1f 100644 --- a/stubs/CoreGenericFunctions.phpstub +++ b/stubs/CoreGenericFunctions.phpstub @@ -627,12 +627,23 @@ function substr_replace($string, $replace, $offset, $length = null) {} /** * @psalm-pure - * * @param string $haystack - * + * @param string $needle + * @param int $offset + * @psalm-assert-if-true =non-empty-string $haystack + * @psalm-return positive-int|0|false + */ +function strpos(string $haystack, string $needle, int $offset = 0) {} + +/** + * @psalm-pure + * @param string $haystack + * @param string $needle + * @param int $offset + * @psalm-assert-if-true =non-empty-string $haystack * @psalm-return positive-int|0|false */ -function strpos($haystack, $needle, int $offset = 0) : int|false {} +function stripos(string $haystack, string $needle, int $offset = 0) {} /** * @psalm-pure diff --git a/tests/CoreStubsTest.php b/tests/CoreStubsTest.php index 2eda2d93129..647675b7274 100644 --- a/tests/CoreStubsTest.php +++ b/tests/CoreStubsTest.php @@ -211,14 +211,39 @@ function after_str_ends_with(): string } throw new RuntimeException(); } + + /** @return non-empty-string */ + function after_strpos(): string + { + $string = uniqid(); + if (strpos($string, "foo") !== false) { + return $string; + } + throw new RuntimeException(); + } + + /** @return non-empty-string */ + function after_stripos(): string + { + $string = uniqid(); + if (stripos($string, "foo") !== false) { + return $string; + } + throw new RuntimeException(); + } + $a = after_str_contains(); $b = after_str_starts_with(); $c = after_str_ends_with(); + $d = after_strpos(); + $e = after_stripos(); ', 'assertions' => [ '$a===' => 'non-empty-string', '$b===' => 'non-empty-string', '$c===' => 'non-empty-string', + '$d===' => 'non-empty-string', + '$e===' => 'non-empty-string', ], ]; yield "PHP8 str_* function doesn't subtract string after assertion" => [ diff --git a/tests/FunctionCallTest.php b/tests/FunctionCallTest.php index efc037c758f..abc1703814c 100644 --- a/tests/FunctionCallTest.php +++ b/tests/FunctionCallTest.php @@ -598,12 +598,6 @@ function foo(string $a, string $b) : int { return $aTime - $bTime; }', ], - 'strposIntSecondParam' => [ - 'code' => ' [ 'code' => ' 'InvalidParamDefault', ], + 'disallowStrposIntSecondParam' => [ + 'code' => ' 'InvalidScalarArgument', + ], ]; }