-
Notifications
You must be signed in to change notification settings - Fork 672
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
@psalm-assert with side effects #7096
Comments
Hey @AndrolGenhald, can you reproduce the issue on https://psalm.dev ? |
Please provide an example of code |
I guess my use case is more about having a function that is more strict than it's But the same issue exists with side-effects: https://psalm.dev/r/9dbaa9a230 |
I found these snippets: https://psalm.dev/r/c0165e28b9<?php
/**
* @psalm-assert-if-true string $val
*/
function is_string_with_length(mixed $val, int $min, int $max): bool
{
if (is_string($val)) {
$len = strlen($val);
return $min <= $len && $len <= $max;
}
return false;
}
function takesString(string $val): string
{
return $val;
}
/** @var string */
$foo = "might be a long string that is too long to store in a database column or something";
if (is_string_with_length($foo, 0, 100)) {
takesString($foo);
}
https://psalm.dev/r/9dbaa9a230<?php
class Foo
{
public ?string $bar = null;
/**
* @psalm-assert string $val
*/
public function baz(mixed $val): void
{
if (!is_string($val)) {
throw new \InvalidArgumentException();
}
$this->bar = $val;
}
}
$foo = new Foo();
$bar = "bar";
$foo->baz($bar); // If this call is removed $foo->bar will be different, so the call isn't redundant
|
Basically we need a way for Psalm to distinguish purely asserting functions (those that exist only to assert things declared in |
@weirdan That's a much better explanation. Actually, I think if we treated assertion functions as having side effects unless the function is also Maybe |
In #8378 we have functions that are a good candidate for using |
yeah, except that in the exemple, sometimes it also asserts that the type is more precise so I don't like What we're dealing with in 8378, is pretty different to me. We're actually abusing the assertion module (that was used to express that the goal of the function was to refine the type of a param) to describe a mere side effect or an implied truth about the function In that regard, most calls to str_starts_with or similar could end up Redundant from the point of view of the assertion module and this is the core of the issue. So, there are two solutions to find:
|
I don't like any of the names I've been able to come up with 🙁 Naming things is hard.
It's far from the only case of that though, I think there might actually be some like that that are already merged, but I might be misremembering. There are several similar cases waiting on this issue.
I'm mostly ambivalent between an additional tag vs a different assert tag like I think part of the problem with naming this is that it can serve several purposes, the two I'm thinking about right now being functions that assert a type for a variable but also have side effects, and functions like |
Sometimes I have a function that makes assertions about a type that also has other side effects, such as a function that takes a mixed argument and asserts it is a string is less than a maximum length.
If I annotate this with
@psalm-assert string $arg
, psalm will complain aboutRedundantConditionGivenDocblockType
whenever I call it on something I already know is a string, but I still want to call it because it has the additional side effect of throwing an exception if the string is too large, as well is if it's not a string.It would be nice if there were a way to tell psalm that such side effects exist so that it can ignore the function for the
RedundantConditionGivenDocblockType
check.The text was updated successfully, but these errors were encountered: