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

Remove InvalidScalarArgument (BC break) #7181

Closed
wants to merge 1 commit into from

Conversation

muglug
Copy link
Collaborator

@muglug muglug commented Dec 17, 2021

This could be controversial! I'm happy to close if you think it might be too disruptive for Psalm 5.

InvalidScalarArgument originally came about because, in non-strict mode, PHP is more lenient with those scalar type conversions.

If you take the example given the example code, and add a strict_types declare you get InvalidArgument (example).

But that's not universal — docblock types are treated differently than runtime typehints. This can make some bugs seem less harmful even though they still lead to runtime errors (albeit with a level of indirection).

InvalidScalarArgument has also never played comfortably with PossiblyInvalidArgument — the former often suppresses the latter in an unintuitive fashion.

This PR removes it in favour of InvalidArgument/PossiblyInvalidArgument — whichever is more appropriate.

@muglug muglug added the release:removed The PR will be included in 'Removed' section of the release notes label Dec 17, 2021
@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/83cfce18a5
<?php declare(strict_types=1);

function foo(int $i) : void {}
function bar(string $s) : void {
    if (is_numeric($s)) {
        foo($s);
    }
}
Psalm output (using commit e9ad61e):

ERROR: InvalidArgument - 6:13 - Argument 1 of foo expects int, numeric-string provided
https://psalm.dev/r/001ac14f96
<?php declare(strict_types=1);

function foo(int $i) : void {}
function bar(string $s) : void {
    if (is_numeric($s)) {
        foo($s);
    }
}

/** @param int $i */
function foo_soft($i) : void {
	foo($i);
}
function bar_soft(string $s) : void {
    if (is_numeric($s)) {
        foo_soft($s);
    }
}
Psalm output (using commit e9ad61e):

ERROR: InvalidArgument - 6:13 - Argument 1 of foo expects int, numeric-string provided

ERROR: InvalidScalarArgument - 16:18 - Argument 1 of foo_soft expects int, numeric-string provided

@muglug muglug added the release:feature The PR will be included in 'Features' section of the release notes label Dec 17, 2021
@AndrolGenhald
Copy link
Collaborator

What do you think about leaving it as an issue, but being more strict about where it's emitted? Change it so string doesn't scalar-coerce to int but numeric-string still can? I think most of the other scalar coercions are more straightforward, it seems like string to int or float is the one that causes the most actual problems (although "0" to false is a bit weird too).

Is it actually still useful to have the issue if it's more strict like that, or would it be less useful to the point that it's not worth maintaining?

@muglug muglug force-pushed the muglug-remove-invalidscalarargument branch from 8c82e0b to 53bc6ae Compare December 18, 2021 20:42
@orklah
Copy link
Collaborator

orklah commented Dec 18, 2021

In practice, whatever can help users with huge baseline to solve the bigger issues first is a win in my book. This seems to go in the other direction for me.

For users that have small baselines, this has no impact or very little.

So, we would need to have a big reason for wanting to drop that internally for it to be worth it. You say it plays weirdly with PossiblyInvalidArgument so it could be one. Maybe we need to keep that in mind to not stick to the current behaviour if this is an issue for future refactors

@muglug
Copy link
Collaborator Author

muglug commented Dec 18, 2021

Ok, in that case I'd argue for something slightly different: keep InvalidScalarArgument, but ensure it only fires for non-docblock types (where PHP is sure to be lenient). I'll draw up a different PR to see how that'd look.

@muglug
Copy link
Collaborator Author

muglug commented Dec 23, 2021

Closing this in favour of #7188 — maybe it could be considered again for Psalm 6, but I have no great attachment to it.

@muglug muglug closed this Dec 23, 2021
@muglug muglug deleted the muglug-remove-invalidscalarargument branch December 23, 2021 22:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release:feature The PR will be included in 'Features' section of the release notes release:removed The PR will be included in 'Removed' section of the release notes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants