Skip to content

Conversation

@gsteel
Copy link

@gsteel gsteel commented Dec 23, 2025

isMap should accept mixed or array<array-key, T>, not array<string, T>. If the input was array<string, T>, we wouldn't need to use Assert::isMap().

`isMap` should accept `mixed` or `array<array-key, T>`, not `array<string, T>`. If the input was `array<string, T>`, we wouldn't need to use `Assert::isMap()`.
@zerkms
Copy link
Contributor

zerkms commented Dec 23, 2025

Isn't mixed|array<array-key, T> === mixed?

@gsteel
Copy link
Author

gsteel commented Dec 23, 2025

The array<array-key, T> preserves the value type when it is asserted via @psalm-assert, so array<array-key, MyThing> becomes array<string, MyThing>

@zerkms
Copy link
Contributor

zerkms commented Dec 23, 2025

I guess it's implementation dependent (the static analysis tool), but yep, agree.

* @psalm-assert array<string, T> $array
*
* @param array<string, T> $array
* @param mixed|array<array-key, T> $array
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should just be mixed. As in, remove the @param declaration because the actual type is already mixed $array.

* @param array<string, T> $array
* @param mixed|array<array-key, T> $array
*
* @return array<string, T>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is incorrect, it should be @return array<array-key, T>.

*
* @template T
*
* @psalm-assert array<string, T> $array
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, this should be array<array-key, T>.

@gsteel
Copy link
Author

gsteel commented Dec 27, 2025

The static analysis tests disagree with all of these points as do I:

/**
* @psalm-pure
*
* @param mixed $value
*
* @return array<string, mixed>
*/
function isMap(mixed $value): array
{
Assert::isMap($value);
return $value;
}
/**
* Verifying that the type of the elements in the array is preserved by the assertion
*
* @psalm-pure
*
* @param array<int|string, stdClass> $value
*
* @return array<string, stdClass>
*/
function isMapWithKnownType(array $value): array
{
Assert::isMap($value);
return $value;
}

@shadowhand
Copy link
Collaborator

@gsteel the static analysis tests verify expectations of example code, they are not unit tests.

The very first line of isMap is Assert::isArray, thereby ensuring that the input is an array, implying the type is not known beforehand. Thus, the only viable type is mixed $array. In other words:

The assertion isMap takes a mixed input, proves it is a map (aka associative array), then returns it.

@gsteel
Copy link
Author

gsteel commented Dec 27, 2025

The good thing about the old parameter type mixed|array<array-key, T> and the return type of array<string, T>, is that the template preserves the value type of the input, therefore, if you have already asserted the value type, static analysis will retain the value type whilst also asserting the key type to be a string.

The SA tests here backup that perspective.

Returning or asserting array<array-key, T> means that the isMap method is effectively useless, because a map, by definition is at the very least array<string, mixed>.

This patch effectively reverts incorrect changes to this method.

If you feel like a map is defined by array<array-key, mixed> then please feel free to close this patch.

@shadowhand
Copy link
Collaborator

Maps can also be array<int, mixed> so long as the keys are not sequential numbers. Thus, array<string, mixed> would be fundamentally incorrect.

@shadowhand
Copy link
Collaborator

However, I see the message in the assertion is also incorrect in this regard.

And I do understand your point about preserving the template value, I'm just not quite sure what to do about it...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants