-
Notifications
You must be signed in to change notification settings - Fork 483
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Resolving type of closure - get
$passedToType
from `inFunctionCalls…
…Stack`
- Loading branch information
1 parent
0159d34
commit ca41b7d
Showing
12 changed files
with
174 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Analyser; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Closure; | ||
use PhpParser\Node\FunctionLike; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use PHPStan\Type\VerbosityLevel; | ||
use function sprintf; | ||
|
||
/** | ||
* @implements Rule<FunctionLike> | ||
*/ | ||
class TestClosureTypeRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return FunctionLike::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node instanceof Closure && !$node instanceof Node\Expr\ArrowFunction) { | ||
return []; | ||
} | ||
|
||
$type = $scope->getType($node); | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf('Closure type: %s', $type->describe(VerbosityLevel::precise()))) | ||
->identifier('tests.closureType') | ||
->build(), | ||
]; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Analyser; | ||
|
||
use PHPStan\Rules\Rule as TRule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<TestClosureTypeRule> | ||
*/ | ||
class TestClosureTypeRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): TRule | ||
{ | ||
return new TestClosureTypeRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/closure-passed-to-type.php'], [ | ||
[ | ||
'Closure type: Closure(mixed): (1|2|3)', | ||
25, | ||
], | ||
[ | ||
'Closure type: Closure(mixed): (1|2|3)', | ||
35, | ||
], | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace ClosurePassedToType; | ||
|
||
use function PHPStan\Testing\assertType; | ||
|
||
class Foo | ||
{ | ||
|
||
/** | ||
* @template T | ||
* @template U | ||
* @param array<T> $items | ||
* @param callable(T): U $cb | ||
* @return array<U> | ||
*/ | ||
public function doFoo(array $items, callable $cb) | ||
{ | ||
|
||
} | ||
|
||
public function doBar() | ||
{ | ||
$a = [1, 2, 3]; | ||
$b = $this->doFoo($a, function ($item) { | ||
assertType('1|2|3', $item); | ||
return $item; | ||
}); | ||
assertType('array<1|2|3>', $b); | ||
} | ||
|
||
public function doBaz() | ||
{ | ||
$a = [1, 2, 3]; | ||
$b = $this->doFoo($a, fn ($item) => $item); | ||
assertType('array<1|2|3>', $b); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters