-
Notifications
You must be signed in to change notification settings - Fork 660
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
Psalm 5 breaking changes #4700
Comments
Perhaps extend this to redundant intval, floatval,... calls too? |
Closing, going to make this a minor change instead |
|
|
= make array shapes explicit — |
Ideally this would first come with a config switch and v5 would just flip the default, unless you think that's too much work. |
I'm not sure introducing a config long term on something so fundamental would be beneficial for the project. It will make every future bug report hard to reason about depending on the value of the config. |
The following methods were previously deprecated, linked PR marks them for removal in Psalm 5
|
Things to be removed can be located by grepping the |
The format array{foo: string}&array is currently not supported by phpstan. So this Bc break would require to have both a phpstan-param and psalm-param annotation for all the array phpdoc. Having a config option would be nice for the migration (and also until phpstan support the same format). |
I don't think this is a good idea. Same as you wouldn't want to make arrays behave differently in the PHP language, you don't want to change interpretation of such a basic building block when there's already a lot of code out there that uses it. I think the new behaviour needs to be connected with a different type, like |
Also, I don't love the Maybe something more similar to TypeScript's notation would be better, maybe: |
Also, in the same way that a method which accept a class Foo accept all the class extending Foo, I find more natural that array{foo: string} accepts array with extra values. I would prefer strict-array |
I don't like hijacking other issues but I'm gonna continue here because I'm not sure that @muglug wants to change his mind (I hope so!). Maybe a better syntax would be:
Also:
|
My fear about the proposed array shape meaning change is not only backward compatibility with all the existing analysed code out there (it would be pain for people to upgrade to the new meaning), but also compatibility and synchronicity with PHPStan. We'd have to release new versions simultaneously and that only solves part of the problem. A configuration option to toggle between the meanings isn't great either - you need to be able interpret old code vs. new code at the same time (1st party code + vendor). |
I must admit Ondrej makes a lot of good points. Especially on the syntax topic, the intersection doesn't appear fitted for the job. Once this is laid out, the BC issue and compatibility obstacle with PHPStan feels unnecessary I was quite fond of The hybrid syntax suggested by Ondrej could be interesting too, (but do we really want to support possible integers keys in an object-like structure? I feel this would open up to yet more PHP weirdness with numeric array-keys that turns to integer keys) |
I do. This inferrence is there because most of the times it's right and it's a really useful assumption to make. (because again, for me most case already describe strict arrays and Psalm acts accordingly) Tomorrow, with an array that is lax by default, this inferrence will never be made again. This will introduce new errors in builds that were prevented by this inference. I agree the BC break is annoying but I think it'd be better for the long term |
This is where we disagree. I believe the opposite. Most case correctly describe lax array to me.
Just update
I don't see any long term for BC-break. When the BC-break occur, dev will have to update all their code base and then it won't have any impact in the futur. Either people will have to change all the strict array to The decision should be based on
Just asked to my developer team and they find also more natural that when we add a term, it should be stricter. |
I agree on these concepts about strictness. At the very least if Psalm made the BC break it should ship with a rule that replaces all instances of I also feel that See https://psalm.dev/r/b40bdfa369 vs https://psalm.dev/r/c8c8bc59ff
I'd suggest that the bug here is using |
Any possibilities to see this in Psalm 5? |
|
On the |
@bdsl well for example, the inference is not optimal on $_b here: https://psalm.dev/r/7c42d25a9f It can be useful to be able to infer this for some applications and there's no dedicated syntax for it. I kinda changed my mind since the discussion above and I no longer think using intersection between two arrays makes sense (even more so now that we have intersection in the language). I still believe that array shapes described right now should be the strict form, but I won't change it unilaterally. As a user and occasional contributor, I had the luxury of having strong opinions that I can't hold on my own now that Matt is gone :D I'll let PHPStan take the lead on whatever syntax is the best going forward and we'll follow |
I found these snippets: https://psalm.dev/r/7c42d25a9f<?php
/** @param array{a?: string, b?: string} $a */
function a(array $a): void{
$_b = count($a);
/** @psalm-trace $_b */;
$_c = ['a' => 'a'];
if(rand(0,1)){
$_c['b'] = 'b';
}
$_d = count($_c);
/** @psalm-trace $_d */;
}
|
Right, I see what you mean - if you consider 'has a count of 1' to be part of the behaviour specification of |
@bdsl that's why we need to introduce a new syntax. It may be |
The best move I see is
|
|
|
|
About the Reason:PHP itself might support shapes but with dash in the name, it will not not happen. But I think something like this is possible: function test(): shape{a: string, b?: ?int} It would also be close to identical to shapes in Hacklang, look for |
The downside to that is that it's not a reserved word for PHP, and it's a common enough word that there are probably projects that have a I'm still planning to move forward with |
Agreed but given that Shape is a class, capital letter and most likely namespaced; would that be a big BC? Whatever the decision is, I would be happy anyway. |
I suppose if we only treat it as a shape if it's followed by a I'm not necessarily opposed to |
The release is imminent, so I'm closing this issue. |
RedundantCondition
)The text was updated successfully, but these errors were encountered: