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
Feature: psalm-assert "one of" while providing the list of possible values via variable #5657
Labels
Comments
I found these snippets: https://psalm.dev/r/e5faadd216<?php
namespace Webmozart
{
class Assert
{
/**
* @psalm-pure
* @throws \InvalidArgumentException
*/
public static function oneOf(mixed $value, array $values): void
{
if (!\in_array($value, $values, true)) {
throw new \InvalidArgumentException('whatever');
}
}
}
}
namespace {
class Foo
{
public const VALUE_FOO = 'foo';
public const VALUE_BAR = 'bar';
public const VALUE_BAZ = 'baz';
/** @psalm-param self::VALUE_* $value */
public static function qoo(string $value): void
{
$value;
}
}
/** @var string $unknownValue */
$unknownValue = $_SERVER['QUERY_STRING'] ?? '';
\Webmozart\Assert::oneOf($unknownValue, [Foo::VALUE_FOO, Foo::VALUE_BAR, Foo::VALUE_BAZ]);
Foo::qoo($unknownValue);
}
|
I would expect something like this to work: https://psalm.dev/r/13845d6d06 (but it doesn't) |
I found these snippets: https://psalm.dev/r/13845d6d06<?php
namespace Webmozart {
class Assert {
/**
* @psalm-pure
* @template T
* @param array<array-key, T> $values
* @psalm-assert T $value
*
* @throws \InvalidArgumentException
*/
public static function oneOf(mixed $value, array $values): void {
if (!\in_array($value, $values, true)) {
throw new \InvalidArgumentException('whatever');
}
}
}
}
namespace {
/**
* @return 'a'|'b'
*/
function f(string $unknownValue): string {
\Webmozart\Assert::oneOf($unknownValue, ['a', 'b']);
/** @psalm-trace $unknownValue */;
return $unknownValue;
}
}
|
orklah
pushed a commit
that referenced
this issue
Jun 27, 2022
fixes #5657 Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com>
Closed with #8077 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey there,
I think, many of us are using
webmozart/assert
.I am experiencing some kind of problem for now, as I cannot tell that a value contains one of the values provided by another value.
Lets assume, I have this piece of code:
https://psalm.dev/r/e5faadd216
I dont know any way to provide
psalm-assert
informations that theunknownValue
is one of these which were provided via the$values
argument ofAssert::oneOf
.Is that something which could be achieved in any way?
The text was updated successfully, but these errors were encountered: