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

[New Feature] Not using Docblock parameter types #11070

Open
samuelmaudo opened this issue Aug 12, 2024 · 1 comment
Open

[New Feature] Not using Docblock parameter types #11070

samuelmaudo opened this issue Aug 12, 2024 · 1 comment

Comments

@samuelmaudo
Copy link

Hello,

I am creating a library, and documenting the types of the parameters to make it easier to use it.

However, I am not relying on the documented types, but checking the received arguments to make sure they match.

Here is an example:

/**
 * @param positive-int $value
 */
function myFunction(int $value): void
{
    if ($value < 1) {
        throw new Exception();
    }
}

The problem is that Psalm reports such validations as errors:

So I would like to tell Psalm not to use the documented parameter types.

Would it be possible to create a new setting called useDocblockParameterTypes (or something similar) to set it to false and achieve this behavior?

I have seen two similar settings (useDocblockTypes and useDocblockPropertyTypes), but they have a different effect.

Thanks in advance.

PS: I am willing to help with the implementation of this new feature.

Copy link

I found these snippets:

https://psalm.dev/r/d53bc36ff6
<?php

/**
 * @param positive-int $value
 */
function myFunction(int $value): void
{
    if ($value < 1) {
        throw new Exception();
    }
}
Psalm output (using commit 16b24bd):

ERROR: DocblockTypeContradiction - 8:9 - Docblock-defined type int<1, max> for $value is never <= 1
https://psalm.dev/r/06d37e244b
<?php

/**
 * @param non-empty-string $value
 */
function myFunction(string $value): void
{
    if ($value === '') {
        throw new Exception();
    }
}
Psalm output (using commit 16b24bd):

ERROR: TypeDoesNotContainType - 8:9 - '' cannot be identical to non-empty-string

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

No branches or pull requests

1 participant