-
-
Notifications
You must be signed in to change notification settings - Fork 903
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
Generic's sub-type is lost when passed through a callable #7281
Comments
Your callables are only defined to return
|
If by "special handling", you mean a custom DynamicReturnTypeExtension, then I'm confused, because I've created one too for our <?php
declare(strict_types=1);
namespace Gammadia\Collections\PhpStan\Functional\Extensions;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Type\Php\ArrayMapFunctionReturnTypeExtension;
use PHPStan\Type\Type;
use function Gammadia\Collections\Functional\reverse;
final class FunctionalMapDynamicReturnTypeExtension extends ArrayMapFunctionReturnTypeExtension
{
public function isFunctionSupported(FunctionReflection $functionReflection): bool
{
return 'gammadia\collections\functional\map' === strtolower($functionReflection->getName());
}
/**
* Our implementation as reversed arguments (array first, callable second) compared to array_map
*/
public function getTypeFromFunctionCall(FunctionReflection $functionReflection, FuncCall $functionCall, Scope $scope): Type
{
$reversedFunctionCall = new FuncCall($functionCall->name, reverse($functionCall->getArgs()));
return parent::getTypeFromFunctionCall($functionReflection, $reversedFunctionCall, $scope);
}
} |
Unfortunately I'm not talking about an extension, but internal handling: https://github.com/phpstan/phpstan-src/blob/75c5574a402e858458bf0f83a5942a22e6cfb737/src/Analyser/MutatingScope.php#L1235 |
@rvanvelzen would it be feasible to make that "internal handling" pluggable ? Like being able to define a list of functions (and/or methods?) acting like array_map ? |
@gnutix fairly unlikely, but this case will be solved when generalization of template types goes away (see phpstan/phpstan-src#1206 for some info on that) |
@rvanvelzen Out of curiosity, is this "generalization of template types going away" thing still a topic ? (just seen the PR hasn't been touched in a year) |
@gnutix After the latest push in 1.11.x, PHPStan now reports different result with your code snippet: @@ @@
-PHP 7.4 – 8.1 (4 errors)
+PHP 7.4 – 8.1 (2 errors)
==========
34: Expected type array<int, Timeline<Percentage>>, actual: array<int, Timeline>
-40: Expected type array<int, Timeline<Percentage>>, actual: array<int, mixed>
47: Expected type array<int, Timeline<Percentage>>, actual: array<int, Timeline>
-51: Expected type array<int, Timeline<Percentage>>, actual: array<int, mixed>
PHP 7.1 – 7.3 (8 errors)
========== Full reportPHP 7.4 – 8.1 (2 errors)
PHP 7.1 – 7.3 (8 errors)
|
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
Bug report
Hello there. While adding some types and types' tests for a bunch of Functional-related functions in our codebase, I noticed a loss of precision in the return type of operations like map, reduce and collect, when a Generic with a sub-type is passed through a callable.
I've collected a few playgrounds over the last few weeks, sorry if some of them overlap.
(I don't currently have the time to work on an implementation, but maybe someone will.)
Code snippet that reproduces the problem
Expected output
The expected types should be the ones returned by PHPStan.
Did PHPStan help you today? Did it make you happy in any way?
(I have a bunch of small issues to create, sorry if I don't come up with something new for each of them 🙏 )
The text was updated successfully, but these errors were encountered: