Skip to content

Commit

Permalink
More specific return type for methods used to analyse currently enter…
Browse files Browse the repository at this point in the history
…ed function or method
  • Loading branch information
ondrejmirtes committed Sep 26, 2024
1 parent c83196b commit 0e2587f
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 20 deletions.
5 changes: 2 additions & 3 deletions src/Analyser/MutatingScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ public function __construct(
private ScopeContext $context,
private PhpVersion $phpVersion,
private bool $declareStrictTypes = false,
private FunctionReflection|ExtendedMethodReflection|null $function = null,
private PhpFunctionFromParserNodeReflection|null $function = null,
?string $namespace = null,
private array $expressionTypes = [],
private array $nativeExpressionTypes = [],
Expand Down Expand Up @@ -310,9 +310,8 @@ public function getTraitReflection(): ?ClassReflection

/**
* @api
* @return FunctionReflection|ExtendedMethodReflection|null
*/
public function getFunction()
public function getFunction(): ?PhpFunctionFromParserNodeReflection
{
return $this->function;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -728,7 +728,7 @@ private function processStmtNode(
$classReflection = $scope->getClassReflection();

$methodReflection = $methodScope->getFunction();
if (!$methodReflection instanceof ExtendedMethodReflection) {
if (!$methodReflection instanceof PhpMethodFromParserNodeReflection) {
throw new ShouldNotHappenException();
}

Expand Down
6 changes: 2 additions & 4 deletions src/Analyser/Scope.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Reflection\NamespaceAnswerer;
use PHPStan\Reflection\ParameterReflection;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\Reflection\Php\PhpFunctionFromParserNodeReflection;
use PHPStan\TrinaryLogic;
use PHPStan\Type\Type;
use PHPStan\Type\TypeWithClassName;
Expand Down Expand Up @@ -49,10 +50,7 @@ public function isInTrait(): bool;

public function getTraitReflection(): ?ClassReflection;

/**
* @return FunctionReflection|ExtendedMethodReflection|null
*/
public function getFunction();
public function getFunction(): ?PhpFunctionFromParserNodeReflection;

public function getFunctionName(): ?string;

Expand Down
6 changes: 3 additions & 3 deletions src/Node/FunctionReturnStatementsNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use PhpParser\NodeAbstract;
use PHPStan\Analyser\ImpurePoint;
use PHPStan\Analyser\StatementResult;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\Php\PhpFunctionFromParserNodeReflection;
use function count;

/**
Expand All @@ -32,7 +32,7 @@ public function __construct(
private StatementResult $statementResult,
private array $executionEnds,
private array $impurePoints,
private FunctionReflection $functionReflection,
private PhpFunctionFromParserNodeReflection $functionReflection,
)
{
parent::__construct($function->getAttributes());
Expand Down Expand Up @@ -91,7 +91,7 @@ public function getSubNodeNames(): array
return [];
}

public function getFunctionReflection(): FunctionReflection
public function getFunctionReflection(): PhpFunctionFromParserNodeReflection
{
return $this->functionReflection;
}
Expand Down
6 changes: 3 additions & 3 deletions src/Node/MethodReturnStatementsNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use PHPStan\Analyser\ImpurePoint;
use PHPStan\Analyser\StatementResult;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\ExtendedMethodReflection;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use function count;

/**
Expand All @@ -36,7 +36,7 @@ public function __construct(
private array $executionEnds,
private array $impurePoints,
private ClassReflection $classReflection,
private ExtendedMethodReflection $methodReflection,
private PhpMethodFromParserNodeReflection $methodReflection,
)
{
parent::__construct($method->getAttributes());
Expand Down Expand Up @@ -88,7 +88,7 @@ public function getClassReflection(): ClassReflection
return $this->classReflection;
}

public function getMethodReflection(): ExtendedMethodReflection
public function getMethodReflection(): PhpMethodFromParserNodeReflection
{
return $this->methodReflection;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Rules/Api/ApiInstanceofRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ private function processCoveredClass(Node\Expr\Instanceof_ $node, Scope $scope,
if ($classReflection->getName() === Type::class || $classReflection->isSubclassOf(Type::class)) {
return [];
}
if ($classReflection->isInterface()) {
return [];
}

$instanceofType = $scope->getType($node);
if ($instanceofType->isTrue()->or($instanceofType->isFalse())->yes()) {
Expand Down
8 changes: 2 additions & 6 deletions src/Rules/Functions/ReturnTypeRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
use PhpParser\Node;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\Php\PhpFunctionFromParserNodeReflection;
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
use PHPStan\Rules\FunctionReturnTypeCheck;
use PHPStan\Rules\Rule;
use function sprintf;
Expand Down Expand Up @@ -40,10 +39,7 @@ public function processNode(Node $node, Scope $scope): array
}

$function = $scope->getFunction();
if (
!($function instanceof PhpFunctionFromParserNodeReflection)
|| $function instanceof PhpMethodFromParserNodeReflection
) {
if ($function instanceof MethodReflection) {
return [];
}

Expand Down

0 comments on commit 0e2587f

Please sign in to comment.