Skip to content

Use argument types as parameter types for inline closures (#7798) #1628

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

Merged
merged 1 commit into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions conf/config.neon
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,16 @@ services:
tags:
- phpstan.parser.richParserNodeVisitor

-
class: PHPStan\Parser\ClosureArgVisitor
tags:
- phpstan.parser.richParserNodeVisitor

-
class: PHPStan\Parser\ArrowFunctionArgVisitor
tags:
- phpstan.parser.richParserNodeVisitor

-
class: PHPStan\Parser\NewAssignedToPropertyVisitor
tags:
Expand Down
59 changes: 51 additions & 8 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@
use PHPStan\Reflection\InitializerExprTypeResolver;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Native\NativeMethodReflection;
use PHPStan\Reflection\Native\NativeParameterReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\Php\PhpMethodReflection;
Expand Down Expand Up @@ -2960,21 +2961,42 @@ private function processClosureNode(

$byRefUses = [];

if ($passedToType !== null && !$passedToType->isCallable()->no()) {
$callableParameters = null;
$closureCallArgs = $expr->getAttribute('closureCallArgs');

if ($closureCallArgs !== null) {
$acceptors = $scope->getType($expr)->getCallableParametersAcceptors($scope);
if (count($acceptors) === 1) {
$callableParameters = $acceptors[0]->getParameters();

foreach ($callableParameters as $index => $callableParameter) {
if (!isset($closureCallArgs[$index])) {
continue;
}

$type = $scope->getType($closureCallArgs[$index]->value);
$callableParameters[$index] = new NativeParameterReflection(
$callableParameter->getName(),
$callableParameter->isOptional(),
$type,
$callableParameter->passedByReference(),
$callableParameter->isVariadic(),
$callableParameter->getDefaultValue(),
);
}
}
} elseif ($passedToType !== null && !$passedToType->isCallable()->no()) {
if ($passedToType instanceof UnionType) {
$passedToType = TypeCombinator::union(...array_filter(
$passedToType->getTypes(),
static fn (Type $type) => $type->isCallable()->yes(),
));
}

$callableParameters = null;
$acceptors = $passedToType->getCallableParametersAcceptors($scope);
if (count($acceptors) === 1) {
$callableParameters = $acceptors[0]->getParameters();
}
} else {
$callableParameters = null;
}

$useScope = $scope;
Expand Down Expand Up @@ -3101,21 +3123,42 @@ private function processArrowFunctionNode(
$nodeCallback($expr->returnType, $scope);
}

if ($passedToType !== null && !$passedToType->isCallable()->no()) {
$callableParameters = null;
$arrowFunctionCallArgs = $expr->getAttribute('arrowFunctionCallArgs');

if ($arrowFunctionCallArgs !== null) {
$acceptors = $scope->getType($expr)->getCallableParametersAcceptors($scope);
if (count($acceptors) === 1) {
$callableParameters = $acceptors[0]->getParameters();

foreach ($callableParameters as $index => $callableParameter) {
if (!isset($arrowFunctionCallArgs[$index])) {
continue;
}

$type = $scope->getType($arrowFunctionCallArgs[$index]->value);
$callableParameters[$index] = new NativeParameterReflection(
$callableParameter->getName(),
$callableParameter->isOptional(),
$type,
$callableParameter->passedByReference(),
$callableParameter->isVariadic(),
$callableParameter->getDefaultValue(),
);
}
}
} elseif ($passedToType !== null && !$passedToType->isCallable()->no()) {
if ($passedToType instanceof UnionType) {
$passedToType = TypeCombinator::union(...array_filter(
$passedToType->getTypes(),
static fn (Type $type) => $type->isCallable()->yes(),
));
}

$callableParameters = null;
$acceptors = $passedToType->getCallableParametersAcceptors($scope);
if (count($acceptors) === 1) {
$callableParameters = $acceptors[0]->getParameters();
}
} else {
$callableParameters = null;
}

$arrowFunctionScope = $scope->enterArrowFunction($expr, $callableParameters);
Expand Down
24 changes: 24 additions & 0 deletions src/Parser/ArrowFunctionArgVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace PHPStan\Parser;

use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use function count;

class ArrowFunctionArgVisitor extends NodeVisitorAbstract
{

public function enterNode(Node $node): ?Node
{
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Expr\ArrowFunction) {
$args = $node->getArgs();

if (count($args) > 0) {
$node->name->setAttribute('arrowFunctionCallArgs', $args);
}
}
return null;
}

}
24 changes: 24 additions & 0 deletions src/Parser/ClosureArgVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php declare(strict_types = 1);

namespace PHPStan\Parser;

use PhpParser\Node;
use PhpParser\NodeVisitorAbstract;
use function count;

class ClosureArgVisitor extends NodeVisitorAbstract
{

public function enterNode(Node $node): ?Node
{
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Expr\Closure) {
$args = $node->getArgs();

if (count($args) > 0) {
$node->name->setAttribute('closureCallArgs', $args);
}
}
return null;
}

}
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,12 @@ public function dataFileAsserts(): iterable
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Arrays/data/bug-7469.php');
yield from $this->gatherAssertTypes(__DIR__ . '/../Rules/Variables/data/bug-3391.php');
yield from $this->gatherAssertTypes(__DIR__ . '/data/bug-6901.php');

if (PHP_VERSION_ID >= 70400) {
yield from $this->gatherAssertTypes(__DIR__ . '/data/arrow-function-argument-type.php');
}

yield from $this->gatherAssertTypes(__DIR__ . '/data/closure-argument-type.php');
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/PHPStan/Analyser/data/arrow-function-argument-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace ArrowFunctionArgumentType;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* @param array{a: int} $array
*/
public function doFoo(int $integer, array $array, ?string $nullableString)
{
(fn($context) => assertType('int', $context))($integer);

(fn($context) => assertType('array{a: int}', $context))($array);

(fn($context) => assertType('string|null', $context))($nullableString);

(fn($a, $b, $c) => assertType('array{int, array{a: int}, string|null}', [$a, $b, $c]))($integer, $array, $nullableString);

(fn($a, $b, $c = null) => assertType('array{int, array{a: int}, mixed}', [$a, $b, $c]))($integer, $array);
}

}
40 changes: 40 additions & 0 deletions tests/PHPStan/Analyser/data/closure-argument-type.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace ClosureArgumentType;

use function PHPStan\Testing\assertType;

class Foo
{

/**
* @param array{a: int} $array
*/
public function doFoo(int $integer, array $array, ?string $nullableString)
{
(function($context) {
assertType('int', $context);
})($integer);

(function($context) {
assertType('array{a: int}', $context);
})($array);

(function($context) {
assertType('string|null', $context);
})($nullableString);

(function($context1, $context2, $context3) {
assertType('int', $context1);
assertType('array{a: int}', $context2);
assertType('string|null', $context3);
})($integer, $array, $nullableString);

(function($context1, $context2, $context3 = null) {
assertType('int', $context1);
assertType('array{a: int}', $context2);
assertType('mixed', $context3);
})($integer, $array);
}

}