-
Notifications
You must be signed in to change notification settings - Fork 665
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
array_map((a, b) => c), a, b)
fails to infer the type of b
in the closure
#2772
Comments
I found these snippets: https://psalm.dev/r/cdabfb49ce<?php
/** @psalm-return array<array<string, string>> */
function functionsToBeTested() : array
{
$as = ['key'];
$bs = ['not important'];
return array_map(
/** @psalm-suppress MissingClosureParamType closure type is omitted to show missing inference */
function (string $a, $b) : array {
return [$a => $b];
},
$as,
$bs
);
}
https://psalm.dev/r/c5f1c38c50<?php
/** @psalm-return array<array<string, string>> */
function functionsToBeTested() : array
{
$as = ['key'];
$bs = ['not important'];
return array_map(
function (string $a, string $b) : array {
return [$a => $b];
},
$as,
$bs
);
}
|
Actual example from the code in the downstream project: https://psalm.dev/r/f5715acb09 |
I found these snippets: https://psalm.dev/r/f5715acb09<?php
final class BetterReflectionFunctionAbstract
{
}
final class BetterReflectionClass
{
public function getMethod(string $method) : BetterReflectionFunctionAbstract
{
throw new \Exception('ignore');
}
}
final class ClassReflector
{
public function reflect(string $className) : BetterReflectionClass
{
throw new \Exception('ignore');
}
}
final class FunctionReflector
{
public function reflect(string $functionName) : BetterReflectionFunctionAbstract
{
throw new \Exception('ignore');
}
}
/**
* @psalm-return array<string, array{
* 0: BetterReflectionFunctionAbstract,
* 1: BetterReflectionFunctionAbstract,
* 2: list<string>
* }>
*/
function functionsToBeTested() : array
{
$fromClassReflector = new ClassReflector();
$toClassReflector = new ClassReflector();
$fromReflector = new FunctionReflector();
$toReflector = new FunctionReflector();
$functions = [
'orderChanged' => [
'[BC] CHANGED: Default parameter value for for parameter $a of orderChanged() changed from 1 to 3',
'[BC] CHANGED: Default parameter value for for parameter $c of orderChanged() changed from 3 to 1',
],
];
return array_merge(
array_combine(
array_keys($functions),
array_map(
function (string $function, array $errorMessages) use ($fromReflector, $toReflector) : array {
return [
$fromReflector->reflect($function),
$toReflector->reflect($function),
$errorMessages,
];
},
array_keys($functions),
array_values($functions)
)
),
[
'C::changed1' => [
$fromClassReflector->reflect('C')->getMethod('changed1'),
$toClassReflector->reflect('C')->getMethod('changed1'),
[
'[BC] CHANGED: Default parameter value for for parameter $a of C::changed1() changed from 1 to 2',
],
]
]
);
}
|
For whatever reason the |
I found these snippets: https://psalm.dev/r/3ca580553f<?php
/** @psalm-return array<array<string, string>> */
function functionsToBeTested() : array
{
$as = ['key'];
$bs = ['not important'];
return array_map(
function (string $a, $b) : array {
return [$a => $b];
},
$as,
$bs
);
}
|
Added workarounds for vimeo/psalm#2772 and moved from `Safe\array_combine` to just `\array_combine` due to thecodingmachine/safe#185
Uhh, something else happened too: if you see the comment above (about the real-world use-case), @psalm-github-bot found:
Now it no longer reports anything |
Yeah, because now it's inferring the types properly, right? Or is the code incorrect? |
No, the code is correct, I'm just wondering what changed on this side meanwhile (the snippet wasn't changed) |
That's due to this commit: 9115ffd2 Psalm now has a better idea the array type than it did before, so it uses that instead of the |
\o/ thanks! |
…-phpunit` This change ensures that we don't use `@internal` symbols in `src/` (too dangerous, for long-term usage), and makes the type checking much stricter, thanks to `psalm/plugin-phpunit`. Since `vimeo/psalm` has been upgraded, and vimeo/psalm#2772 no longer applies, we can also remove a lot of manually written code that was safe to clean up.
Added workarounds for vimeo/psalm#2772 and moved from `Safe\array_combine` to just `\array_combine` due to thecodingmachine/safe#185
…-phpunit` This change ensures that we don't use `@internal` symbols in `src/` (too dangerous, for long-term usage), and makes the type checking much stricter, thanks to `psalm/plugin-phpunit`. Since `vimeo/psalm` has been upgraded, and vimeo/psalm#2772 no longer applies, we can also remove a lot of manually written code that was safe to clean up.
Typically,
array_map()
is used with two parameters, one is the closure mapping the values, and one is the foldable passed to that closure.When
array_map(function ($a, $b) { return $c; }, $as, $bs)
is used, then the type of$b
fails to be inferred as the type of$bs
items: https://psalm.dev/r/cdabfb49ceWhen declaring the type of
$b
explicitly, the errors obviously go away: https://psalm.dev/r/c5f1c38c50The code is obviously over-simplified: the reason why this issue exists, is because
$b
is often a complex nested structure.The text was updated successfully, but these errors were encountered: