mirror of
https://github.com/danog/psalm.git
synced 2025-01-22 05:41:20 +01:00
str_* functions assert for non-empty-string
This commit is contained in:
parent
cf86b16199
commit
f3e950bac7
@ -1059,18 +1059,27 @@ function str_split(string $string, int $length = 1) {}
|
||||
|
||||
/**
|
||||
* @psalm-pure
|
||||
* @template T as string
|
||||
* @param T $needle
|
||||
* @psalm-assert-if-true =(T is '' ? string : non-empty-string) $haystack
|
||||
* @return ($needle is '' ? true : ($haystack is '' ? false : bool))
|
||||
*/
|
||||
function str_starts_with(string $haystack, string $needle): bool {}
|
||||
|
||||
/**
|
||||
* @psalm-pure
|
||||
* @template T as string
|
||||
* @param T $needle
|
||||
* @psalm-assert-if-true =(T is '' ? string : non-empty-string) $haystack
|
||||
* @return ($needle is '' ? true : ($haystack is '' ? false : bool))
|
||||
*/
|
||||
function str_ends_with(string $haystack, string $needle): bool {}
|
||||
|
||||
/**
|
||||
* @psalm-pure
|
||||
* @template T as string
|
||||
* @param T $needle
|
||||
* @psalm-assert-if-true =(T is '' ? string : non-empty-string) $haystack
|
||||
* @return ($needle is '' ? true : ($haystack is '' ? false : bool))
|
||||
*/
|
||||
function str_contains(string $haystack, string $needle): bool {}
|
||||
|
@ -180,6 +180,88 @@ class CoreStubsTest extends TestCase
|
||||
'$c3===' => 'bool',
|
||||
],
|
||||
];
|
||||
yield 'PHP8 str_* function assert non-empty-string' => [
|
||||
'code' => '<?php
|
||||
/** @return non-empty-string */
|
||||
function after_str_contains(): string
|
||||
{
|
||||
$string = file_get_contents("");
|
||||
if (str_contains($string, "foo")) {
|
||||
return $string;
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
/** @return non-empty-string */
|
||||
function after_str_starts_with(): string
|
||||
{
|
||||
$string = file_get_contents("");
|
||||
if (str_starts_with($string, "foo")) {
|
||||
return $string;
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
/** @return non-empty-string */
|
||||
function after_str_ends_with(): string
|
||||
{
|
||||
$string = file_get_contents("");
|
||||
if (str_ends_with($string, "foo")) {
|
||||
return $string;
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
$a = after_str_contains();
|
||||
$b = after_str_starts_with();
|
||||
$c = after_str_ends_with();
|
||||
',
|
||||
'assertions' => [
|
||||
'$a===' => 'non-empty-string',
|
||||
'$b===' => 'non-empty-string',
|
||||
'$c===' => 'non-empty-string',
|
||||
],
|
||||
];
|
||||
yield "PHP8 str_* function doesn't subtract string after assertion" => [
|
||||
'code' => '<?php
|
||||
/** @return false|string */
|
||||
function after_str_contains()
|
||||
{
|
||||
$string = file_get_contents("");
|
||||
if (!str_contains($string, "foo")) {
|
||||
return $string;
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
/** @return false|string */
|
||||
function after_str_starts_with()
|
||||
{
|
||||
$string = file_get_contents("");
|
||||
if (!str_starts_with($string, "foo")) {
|
||||
return $string;
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
/** @return false|string */
|
||||
function after_str_ends_with()
|
||||
{
|
||||
$string = file_get_contents("");
|
||||
if (!str_ends_with($string, "foo")) {
|
||||
return $string;
|
||||
}
|
||||
throw new RuntimeException();
|
||||
}
|
||||
$a = after_str_contains();
|
||||
$b = after_str_starts_with();
|
||||
$c = after_str_ends_with();
|
||||
',
|
||||
'assertions' => [
|
||||
'$a===' => 'false|string',
|
||||
'$b===' => 'false|string',
|
||||
'$c===' => 'false|string',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerInvalidCodeParse(): iterable
|
||||
|
Loading…
x
Reference in New Issue
Block a user