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
PropertyTypeCoercion for @psalm-var intersect type #7879
Comments
Hey @michnovka, can you reproduce the issue on https://psalm.dev ? |
No I cannot, but changing to $view->vars['attr'] = ['class' => 'form-control']; Fixes the issue, which makes me believe this has to be a bug in psalm. The FormView is a Symfony class, I cannot edit its PHPDoc |
I've noticed this before too, but I've never had the time to go debug it. Can you try adding a
Not necessarily. I don't remember how far I got last time I looked into this, but if Psalm thinks |
|
Any idea what we can do to address this? Tons of errors whenever I access
|
I reproduced it @AndrolGenhald |
I found these snippets: https://psalm.dev/r/93d9653415<?php
/**
* @template T
*/
class FormView
{
/**
* @psalm-suppress MixedArrayAssignment
* @psalm-suppress InvalidArrayOffset
*
* @psalm-var array{value: ?T, attr: array<array-key, mixed>}&array<string, mixed>
*/
public array $vars = [
'value' => null,
'attr' => [],
];
/**
* @psalm-var ?self
*/
public $parent;
/**
* @psalm-var array<string, FormView>
*/
public $children = [];
}
$view = new FormView();
$view->vars['attr']['a'] = true;
|
The issue seems to be the intersection type as changing * @psalm-var array{value: ?T, attr: array<array-key, mixed>}&array<string, mixed> for * @psalm-var array{value: ?T, attr: array<array-key, mixed>} resolves the issue |
That's definitely a Psalm bug then, Psalm's always behaved a bit weird when intersecting or unioning certain array types. |
If interested, psalm uses https://github.com/psalm/psalm-plugin-symfony/blob/master/src/Stubs/common/Component/Form/FormView.stubphp as the default Symfony FormView class has no psalm PHPDoc |
Is this even a valid definition? * @psalm-var array{value: ?T, attr: array<array-key, mixed>}&array<string, mixed> Is this how you specify an array type with multiple keys, but define just some explicitly in |
Yes, it's valid because array shapes default to being unsealed, meaning an array with extra keys set still satisfies the type (ie Unfortunately the way arrays and array-shapes are intersected right now has some edge cases, and it's not really possible to correctly express an array with extra keys constrained to a type that doesn't match the defined keys, but Psalm currently just ignores this inconsistency. I would like to eventually do something like TypeScript's index signatures like I mentioned here, but I haven't had much time to work on Psalm lately. |
I found these snippets: https://psalm.dev/r/e0971d3777<?php
/** @var array{foo: 1}&array<int, string> */
$arr = [];
/** @psalm-trace $arr */;
|
Ok, there seems to be some "type flattening" going on: https://psalm.dev/r/2489730359 when working on multi-dimensional arrays See the MixedArrayAssignment error which in my opinion is a bug |
I found these snippets: https://psalm.dev/r/2489730359<?php
/**
* @psalm-trace $x
* @psalm-var array{attr: array<array-key, mixed>}&array<string, mixed>
*/
$x = [
'attr' => [],
];
/** @psalm-trace $x */
$x['attr']['foo'] = 'bar';
|
In my code I have:
And it results in this error:
The variable is defined like this in parent class:
The text was updated successfully, but these errors were encountered: