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
Modifier to disallow non-documented keys from array shape #5299
Comments
I found these snippets: https://psalm.dev/r/cdf24c2204<?php declare(strict_types = 1);
/** @param array{status?: int} $data */
function create(array $data): void {
return;
}
create(['status' => 1]);
create([]);
create(['sstatus' => 1]); // I want this to error
|
This was slightly poor design on my part. We could add a config that would make |
That would work for me |
I've been thinking the best way to do this would be to do something like TypeScript's index signatures (and maybe eventually mapped types) for keyed arrays. Unsealed arrays (I'm say "sealed" and "unsealed" because that's what Psalm currently calls it. Sealed means no extra keys, unsealed means extra keys are @ondrejmirtes I know we want to make sure Psalm and PHPStan are in agreement on this since it's such an important feature. I saw you had some proposals here, but I'm not much of a fan of If we want to go with index types, there are couple of things to consider. I think it's fine to remove the name (eg Overlapping types: |
This is now the default behaviour: https://psalm.dev/r/19e15d40ad Old-style, unsealed shapes can be expressed using |
I found these snippets: https://psalm.dev/r/19e15d40ad<?php declare(strict_types = 1);
/** @param array{status?: int} $_data */
function create(array $_data): void {
return;
}
create(['status' => 1]);
create([]);
create(['sstatus' => 1]); // I want this to error
|
@weirdan I thought this isn't going to change in Psalm 5. The array shape (the only one that existed in Psalm 4) shouldn't have got any stricter in Psalm 5. Because:
So please can you tell us what's the behaviour supposed to be now? |
REF #8701 |
This is all on me, but I pushed for this to be clarified in Psalm 5. Both Phan and PHPStan have unsound behaviour. In both cases you were following Psalm’s lead, but unsound behaviour is bad and wrong. The correct behaviour here, IMO, is for array shapes to be sealed by default. I did not want to release another major version with unsound behaviour, and I didn’t want to have everyone start adding |
I wasn’t aware of any changes because three weeks ago we agreed with orklah that array shapes by default shouldn’t change because of the impact on the ecosystem #8395 (comment) and only sealed/strict variants should have the stricter behaviour. That’s what PHPStan will continue to do. |
It's not "stricter" behaviour that we're discussing, though — it's sound vs unsound. Unlike other unsound stuff in typecheckers (like array keys being converted to ints in some cases) this is unsound behaviour introduced by the typecheckers themselves. Psalm has led the way when it comes to preventing unsound behaviour (e.g. making My guilt is that it was an unsoundness introduced by Psalm. This is a chance to remedy that, at the expense of some friction for users. |
Psalm also still supports (actually also incorrectly-thought-out) array annotation For users who encounter this issue who want their docblocks to work as-is in both tools, adding |
Nice, thank you everyone :) |
I'll eventually adopt this too, but I'm not gonna hurry, because I can already foresee the amount of support this change will generate for me for several months in a row :) |
An optional bleeding edge or phpstan-strict-rules rule would be great. I'm
always after more strictness!
…On Thu, 24 Nov 2022, 15:55 Ondřej Mirtes, ***@***.***> wrote:
I'll eventually adopt this too, but I'm not gonna hurry, because I can
already foresee the amount of support this change will generate for me for
several months in a row :)
—
Reply to this email directly, view it on GitHub
<#5299 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAEYV4F3NZDMEUZ3A6TQ7ATWJ6FXTANCNFSM4YMHLZVQ>
.
You are receiving this because you commented.Message ID:
***@***.***>
|
I would like to be able to do the following:
https://psalm.dev/r/cdf24c2204
The current behaviour is obviously wanted in most cases, but I would like a modifier that disallows non-documented keys from array shapes, something like
array-shape-strict{foo: string}
orarray!{foo: string}
The text was updated successfully, but these errors were encountered: