Skip to content

Commit

Permalink
Set class and method reflection in the MethodReturnStatementNode.
Browse files Browse the repository at this point in the history
  • Loading branch information
mad-briller committed Jul 7, 2023
1 parent dabbd59 commit 4ee3018
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 36 deletions.
13 changes: 13 additions & 0 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,12 +593,25 @@ private function processStmtNode(

$gatheredReturnStatements[] = new ReturnStatement($scope, $node);
}, StatementContext::createTopLevel());

$classReflection = $methodScope->getClassReflection();
if ($classReflection === null) {
throw new ShouldNotHappenException();

Check failure on line 599 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, ubuntu-latest)

Parameter #7 $methodReflection of class PHPStan\Node\MethodReturnStatementsNode constructor expects PHPStan\Reflection\MethodReflection, PHPStan\Reflection\ExtendedMethodReflection|PHPStan\Reflection\FunctionReflection given.

Check failure on line 599 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (7.4, windows-latest)

Parameter #7 $methodReflection of class PHPStan\Node\MethodReturnStatementsNode constructor expects PHPStan\Reflection\MethodReflection, PHPStan\Reflection\ExtendedMethodReflection|PHPStan\Reflection\FunctionReflection given.
}

$methodReflection = $methodScope->getFunction();
if ($methodReflection === null) {
throw new ShouldNotHappenException();
}

$nodeCallback(new MethodReturnStatementsNode(
$stmt,
$gatheredReturnStatements,
$gatheredYieldStatements,
$statementResult,
$executionEnds,
$classReflection,
$methodReflection,

Check failure on line 614 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.1)

Parameter #7 $methodReflection of class PHPStan\Node\MethodReturnStatementsNode constructor expects PHPStan\Reflection\MethodReflection, PHPStan\Reflection\ExtendedMethodReflection|PHPStan\Reflection\FunctionReflection given.

Check failure on line 614 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.2)

Parameter #7 $methodReflection of class PHPStan\Node\MethodReturnStatementsNode constructor expects PHPStan\Reflection\MethodReflection, PHPStan\Reflection\ExtendedMethodReflection|PHPStan\Reflection\FunctionReflection given.

Check failure on line 614 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan with result cache (8.3)

Parameter #7 $methodReflection of class PHPStan\Node\MethodReturnStatementsNode constructor expects PHPStan\Reflection\MethodReflection, PHPStan\Reflection\ExtendedMethodReflection|PHPStan\Reflection\FunctionReflection given.

Check failure on line 614 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, ubuntu-latest)

Parameter #7 $methodReflection of class PHPStan\Node\MethodReturnStatementsNode constructor expects PHPStan\Reflection\MethodReflection, PHPStan\Reflection\ExtendedMethodReflection|PHPStan\Reflection\FunctionReflection given.

Check failure on line 614 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.0, windows-latest)

Parameter #7 $methodReflection of class PHPStan\Node\MethodReturnStatementsNode constructor expects PHPStan\Reflection\MethodReflection, PHPStan\Reflection\ExtendedMethodReflection|PHPStan\Reflection\FunctionReflection given.

Check failure on line 614 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, ubuntu-latest)

Parameter #7 $methodReflection of class PHPStan\Node\MethodReturnStatementsNode constructor expects PHPStan\Reflection\MethodReflection, PHPStan\Reflection\ExtendedMethodReflection|PHPStan\Reflection\FunctionReflection given.

Check failure on line 614 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.1, windows-latest)

Parameter #7 $methodReflection of class PHPStan\Node\MethodReturnStatementsNode constructor expects PHPStan\Reflection\MethodReflection, PHPStan\Reflection\ExtendedMethodReflection|PHPStan\Reflection\FunctionReflection given.

Check failure on line 614 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, ubuntu-latest)

Parameter #7 $methodReflection of class PHPStan\Node\MethodReturnStatementsNode constructor expects PHPStan\Reflection\MethodReflection, PHPStan\Reflection\ExtendedMethodReflection|PHPStan\Reflection\FunctionReflection given.

Check failure on line 614 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.2, windows-latest)

Parameter #7 $methodReflection of class PHPStan\Node\MethodReturnStatementsNode constructor expects PHPStan\Reflection\MethodReflection, PHPStan\Reflection\ExtendedMethodReflection|PHPStan\Reflection\FunctionReflection given.

Check failure on line 614 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, ubuntu-latest)

Parameter #7 $methodReflection of class PHPStan\Node\MethodReturnStatementsNode constructor expects PHPStan\Reflection\MethodReflection, PHPStan\Reflection\ExtendedMethodReflection|PHPStan\Reflection\FunctionReflection given.

Check failure on line 614 in src/Analyser/NodeScopeResolver.php

View workflow job for this annotation

GitHub Actions / PHPStan (8.3, windows-latest)

Parameter #7 $methodReflection of class PHPStan\Node\MethodReturnStatementsNode constructor expects PHPStan\Reflection\MethodReflection, PHPStan\Reflection\ExtendedMethodReflection|PHPStan\Reflection\FunctionReflection given.
), $methodScope);
}
} elseif ($stmt instanceof Echo_) {
Expand Down
15 changes: 15 additions & 0 deletions src/Node/MethodReturnStatementsNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\NodeAbstract;
use PHPStan\Analyser\StatementResult;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\MethodReflection;

use function count;

/** @api */
Expand All @@ -26,6 +29,8 @@ public function __construct(
private array $yieldStatements,
private StatementResult $statementResult,
private array $executionEnds,
private ClassReflection $classReflection,
private MethodReflection $methodReflection,
)
{
parent::__construct($method->getAttributes());
Expand Down Expand Up @@ -67,6 +72,16 @@ public function getYieldStatements(): array
return $this->yieldStatements;
}

public function getClassReflection(): ClassReflection
{
return $this->classReflection;
}

public function getMethodReflection(): MethodReflection
{
return $this->methodReflection;
}

public function isGenerator(): bool
{
return count($this->yieldStatements) > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\MethodReturnStatementsNode;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use function sprintf;

/**
Expand All @@ -29,10 +27,7 @@ public function getNodeType(): string
public function processNode(Node $node, Scope $scope): array
{
$statementResult = $node->getStatementResult();
$methodReflection = $scope->getFunction();
if (!$methodReflection instanceof MethodReflection) {
throw new ShouldNotHappenException();
}
$methodReflection = $node->getMethodReflection();

$errors = [];
foreach ($this->check->check($methodReflection->getThrowType(), $statementResult->getThrowPoints()) as [$className, $throwPointNode]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\MethodReturnStatementsNode;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\TrinaryLogic;
use PHPStan\Type\TypeUtils;
use PHPStan\Type\VerbosityLevel;
Expand All @@ -35,10 +33,7 @@ public function getNodeType(): string
public function processNode(Node $node, Scope $scope): array
{
$statementResult = $node->getStatementResult();
$methodReflection = $scope->getFunction();
if (!$methodReflection instanceof MethodReflection) {
throw new ShouldNotHappenException();
}
$methodReflection = $node->getMethodReflection();

if ($methodReflection->getThrowType() === null || !$methodReflection->getThrowType()->isVoid()->yes()) {
return [];
Expand Down
15 changes: 3 additions & 12 deletions src/Rules/Exceptions/TooWideMethodThrowTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\MethodReturnStatementsNode;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\FileTypeMapper;
use function sprintf;

Expand All @@ -29,21 +27,14 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$statementResult = $node->getStatementResult();
$methodReflection = $scope->getFunction();
if (!$methodReflection instanceof MethodReflection) {
throw new ShouldNotHappenException();
}
if (!$scope->isInClass()) {
throw new ShouldNotHappenException();
}

$docComment = $node->getDocComment();
if ($docComment === null) {
return [];
}

$classReflection = $scope->getClassReflection();
$statementResult = $node->getStatementResult();
$methodReflection = $node->getMethodReflection();
$classReflection = $node->getClassReflection();
$resolvedPhpDoc = $this->fileTypeMapper->getResolvedPhpDoc(
$scope->getFile(),
$classReflection->getName(),
Expand Down
7 changes: 1 addition & 6 deletions src/Rules/Playground/MethodNeverRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\MethodReturnStatementsNode;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use function count;
use function sprintf;

Expand All @@ -34,10 +32,7 @@ public function processNode(Node $node, Scope $scope): array
return [];
}

$method = $scope->getFunction();
if (!$method instanceof MethodReflection) {
throw new ShouldNotHappenException();
}
$method = $node->getMethodReflection();

$returnType = ParametersAcceptorSelector::selectSingle($method->getVariants())->getReturnType();
$helperResult = $this->helper->shouldReturnNever($node, $returnType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
use PhpParser\Node;
use PHPStan\Analyser\Scope;
use PHPStan\Node\MethodReturnStatementsNode;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use PHPStan\ShouldNotHappenException;
use PHPStan\Type\Constant\ConstantBooleanType;
use PHPStan\Type\TypeCombinator;
use PHPStan\Type\UnionType;
Expand All @@ -34,10 +32,7 @@ public function getNodeType(): string

public function processNode(Node $node, Scope $scope): array
{
$method = $scope->getFunction();
if (!$method instanceof MethodReflection) {
throw new ShouldNotHappenException();
}
$method = $node->getMethodReflection();
$isFirstDeclaration = $method->getPrototype()->getDeclaringClass() === $method->getDeclaringClass();
if (!$method->isPrivate()) {
if (!$this->checkProtectedAndPublicMethods) {
Expand Down

0 comments on commit 4ee3018

Please sign in to comment.