Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve PHP8 str_* function signatures #9534

Merged
merged 5 commits into from
Mar 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 27 additions & 0 deletions stubs/CoreGenericFunctions.phpstub
Original file line number Diff line number Diff line change
Expand Up @@ -1057,6 +1057,33 @@ function str_shuffle(string $string): string {}
*/
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually this should be written as
@psalm-assert-if-true (T is '' ? string : =non-empty-string) $haystack
but psalm parser explodes.
But it's a really edge case when this difference matters. I don't think there would be any issues against any real-world code.

* @return ($needle is '' ? true : ($haystack is '' ? false : bool))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is an edge case when $haystack is literal '' and $needle is a non-literal string (which includes an empty string). Thus it could result in true, but this conditional return always evaluates such case to false.
I doubt it can be seen in any real-world code though.

*/
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 {}

/**
* @psalm-pure
* @return string|false
Expand Down
129 changes: 129 additions & 0 deletions tests/CoreStubsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,117 @@ function foo(string $foo): string
'$f===' => 'false|non-empty-string',
],
];
yield 'str_starts_with/str_ends_with/str_contains redundant condition detection' => [
'code' => '<?php
$a1 = str_starts_with(uniqid(), "");
/** @psalm-suppress InvalidLiteralArgument */
$b1 = str_starts_with("", "random string");
$c1 = str_starts_with(uniqid(), "random string");

$a2 = str_ends_with(uniqid(), "");
/** @psalm-suppress InvalidLiteralArgument */
$b2 = str_ends_with("", "random string");
$c2 = str_ends_with(uniqid(), "random string");

$a3 = str_contains(uniqid(), "");
/** @psalm-suppress InvalidLiteralArgument */
$b3 = str_contains("", "random string");
$c3 = str_contains(uniqid(), "random string");
',
'assertions' => [
'$a1===' => 'true',
'$b1===' => 'false',
'$c1===' => 'bool',
'$a2===' => 'true',
'$b2===' => 'false',
'$c2===' => 'bool',
'$a3===' => 'true',
'$b3===' => 'false',
'$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
Expand All @@ -167,5 +278,23 @@ public function providerInvalidCodeParse(): iterable
',
'error_message' => 'InvalidArgument',
];
yield 'str_contains literal haystack' => [
'code' => '<?php
str_contains("literal", "");
',
'error_message' => 'InvalidLiteralArgument',
];
yield 'str_starts_with literal haystack' => [
'code' => '<?php
str_starts_with("literal", "");
',
'error_message' => 'InvalidLiteralArgument',
];
yield 'str_ends_with literal haystack' => [
'code' => '<?php
str_ends_with("literal", "");
',
'error_message' => 'InvalidLiteralArgument',
];
}
}