-
Notifications
You must be signed in to change notification settings - Fork 669
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
annotating vars ? #11206
Comments
I found these snippets: https://psalm.dev/r/c62b0a7eeb<?php
/** @psalm-var array<int> */
$x = 7;
|
That's how Psalm is designed - when you write Also there is no rule about what you can assign to a local variable with Psalm. It's not like Typescript where the variable has a type and you can only assign things that are assignable to that. With psalm the variable may have one type for the code before the assignment, and after the assignment is gets a new type based on that assignment: this is perfectly legal. |
I found these snippets: https://psalm.dev/r/1842465ac5<?php
$x = 3;
echo $x;
$x = ['hello'];
echo count($x);
|
In general where you can it s better to use |
I found these snippets: https://psalm.dev/r/c62b0a7eeb<?php
/** @psalm-var array<int> */
$x = 7;
|
So what's the way to disambiguate the following cases ? $a = [2, 2] (tuple always exactly 2 values) and array{int} (any number) using type annotation ? Don't you think a @var which checks compatibility would make sense ? Something like @var-force and @var ? Or /** @var 'abc' | 'def' */ $s = 7; is obviously wrong. |
The problem goes away if you disableVarParsing. But it is not available in online testing tool. |
But disabling is not a fix. If you have $a = 'red'; //* type 'red' | 'blue' no idea how to do that ? In tests there was kind of var is of type assertion maybe such is the way to go. I don't know psalm good enough yet. |
Disabling var parsing is absolutely recommended. #10577 will introduce a better alternative to |
There are two cases: One is @Satisfies, and another might be @Specialize. Example: If you have $db->query() returning array<array> or such you might want to specialize it to array. Does a mixed satisfy RowType ? Don't think so. |
https://psalm.dev/r/c62b0a7eeb
Why doesn't this fail due to bad type assignment ? I mean assigning 7 to an array isn't this a problem? You also can assign a fn($x) => $x
The text was updated successfully, but these errors were encountered: