Skip to content

Commit

Permalink
Fix undetected generator function with static reflection
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Jun 7, 2020
1 parent 8fdeba5 commit 3ae5e83
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
8 changes: 7 additions & 1 deletion conf/config.level3.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ rules:
- PHPStan\Rules\Arrays\UnpackIterableInArrayRule
- PHPStan\Rules\Functions\ArrowFunctionReturnTypeRule
- PHPStan\Rules\Functions\ClosureReturnTypeRule
- PHPStan\Rules\Functions\ReturnTypeRule
- PHPStan\Rules\Generators\YieldTypeRule
- PHPStan\Rules\Methods\ReturnTypeRule
- PHPStan\Rules\Properties\DefaultValueTypesAssignedToPropertiesRule
Expand Down Expand Up @@ -50,6 +49,13 @@ services:
tags:
- phpstan.rules.rule

-
class: PHPStan\Rules\Functions\ReturnTypeRule
arguments:
functionReflector: @betterReflectionFunctionReflector
tags:
- phpstan.rules.rule

-
class: PHPStan\Rules\Generators\YieldFromTypeRule
arguments:
Expand Down
16 changes: 15 additions & 1 deletion src/Rules/Functions/ReturnTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
use PHPStan\Reflection\Php\PhpFunctionFromParserNodeReflection;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Rules\FunctionReturnTypeCheck;
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
use Roave\BetterReflection\Reflector\FunctionReflector;

/**
* @implements \PHPStan\Rules\Rule<\PhpParser\Node\Stmt\Return_>
Expand All @@ -18,9 +20,15 @@ class ReturnTypeRule implements \PHPStan\Rules\Rule

private \PHPStan\Rules\FunctionReturnTypeCheck $returnTypeCheck;

public function __construct(FunctionReturnTypeCheck $returnTypeCheck)
private FunctionReflector $functionReflector;

public function __construct(
FunctionReturnTypeCheck $returnTypeCheck,
FunctionReflector $functionReflector
)
{
$this->returnTypeCheck = $returnTypeCheck;
$this->functionReflector = $functionReflector;
}

public function getNodeType(): string
Expand Down Expand Up @@ -49,6 +57,12 @@ public function processNode(Node $node, Scope $scope): array
$reflection = null;
if (function_exists($function->getName())) {
$reflection = new \ReflectionFunction($function->getName());
} else {
try {
$reflection = $this->functionReflector->reflect($function->getName());
} catch (IdentifierNotFound $e) {
// pass
}
}

return $this->returnTypeCheck->checkReturnType(
Expand Down
3 changes: 2 additions & 1 deletion tests/PHPStan/Rules/Functions/ReturnTypeRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class ReturnTypeRuleTest extends \PHPStan\Testing\RuleTestCase

protected function getRule(): \PHPStan\Rules\Rule
{
return new ReturnTypeRule(new FunctionReturnTypeCheck(new RuleLevelHelper($this->createReflectionProvider(), true, false, true, false)));
[, $functionReflector] = self::getReflectors();
return new ReturnTypeRule(new FunctionReturnTypeCheck(new RuleLevelHelper($this->createReflectionProvider(), true, false, true, false)), $functionReflector);
}

public function testReturnTypeRule(): void
Expand Down

0 comments on commit 3ae5e83

Please sign in to comment.