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

Assert::upper() correct usage #299

Open
bpastukh opened this issue Feb 21, 2024 · 5 comments
Open

Assert::upper() correct usage #299

bpastukh opened this issue Feb 21, 2024 · 5 comments

Comments

@bpastukh
Copy link

Hello
I'm trying to use Assert::upper() method, but psalm assert * @psalm-assert !lowercase-string $value link confuses me - how the string can be upper if we assert it is lowercase?

So following code does not pass psalm check:

        $string = $data['someKey'];
        Assert::upper($string);

The error is:

ERROR: DocblockTypeContradiction - src/Event/AbstractEvent.php:28:17 - Docblock-defined type string for $string is always lowercase-string (see https://psalm.dev/155)
        Assert::upper($string);

So what is correct usage of upper assertion?

@zerkms
Copy link
Contributor

zerkms commented Feb 21, 2024

how the string can be upper if we assert it is lowercase?

Assertion is for !lowercase-string, exclamation mark ! means negation.

The error is:

How exactly is typed $string? Show /** @psalm-trace $string */;

@bpastukh
Copy link
Author

It is not typed, it's $data is a dynamic array

        #[OA\Property(property: 'data', description: 'An array of data dependent on event type', type: 'object')]
        #[Assert\NotBlank]
        #[Assert\Type('array')]
        public array $data = [],

@bpastukh
Copy link
Author

I'm able to reproduce it this way:

        $array = ['someKey' => random_bytes(10)];
        $string = $array['someKey'];
        Assert::upper($string);

So $array is not typed (it is defined on the first line of the example) and i'm still getting

ERROR: TypeDoesNotContainType - src/Event/AbstractEvent.php:25:17 - Type non-empty-string for $string is always !lowercase-string (see https://psalm.dev/056)
        Assert::upper($string);

As a workaround for now i've used Assert::true

        Assert::true(
            ctype_upper($string),
            sprintf('Expected a value to contain uppercase characters only. Got: "%s"', $string),
        );

@zerkms
Copy link
Contributor

zerkms commented Feb 22, 2024

Indeed, I could reproduce it on the psalm.dev:

https://psalm.dev/r/7f0799fc21

@shadowhand
Copy link
Collaborator

What would the correct type assertion be? Or does it even need one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants