Skip to content

Commit 0e2587f

Browse files
committed
More specific return type for methods used to analyse currently entered function or method
1 parent c83196b commit 0e2587f

7 files changed

+16
-20
lines changed

src/Analyser/MutatingScope.php

+2-3
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ public function __construct(
208208
private ScopeContext $context,
209209
private PhpVersion $phpVersion,
210210
private bool $declareStrictTypes = false,
211-
private FunctionReflection|ExtendedMethodReflection|null $function = null,
211+
private PhpFunctionFromParserNodeReflection|null $function = null,
212212
?string $namespace = null,
213213
private array $expressionTypes = [],
214214
private array $nativeExpressionTypes = [],
@@ -310,9 +310,8 @@ public function getTraitReflection(): ?ClassReflection
310310

311311
/**
312312
* @api
313-
* @return FunctionReflection|ExtendedMethodReflection|null
314313
*/
315-
public function getFunction()
314+
public function getFunction(): ?PhpFunctionFromParserNodeReflection
316315
{
317316
return $this->function;
318317
}

src/Analyser/NodeScopeResolver.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -728,7 +728,7 @@ private function processStmtNode(
728728
$classReflection = $scope->getClassReflection();
729729

730730
$methodReflection = $methodScope->getFunction();
731-
if (!$methodReflection instanceof ExtendedMethodReflection) {
731+
if (!$methodReflection instanceof PhpMethodFromParserNodeReflection) {
732732
throw new ShouldNotHappenException();
733733
}
734734

src/Analyser/Scope.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use PHPStan\Reflection\NamespaceAnswerer;
1717
use PHPStan\Reflection\ParameterReflection;
1818
use PHPStan\Reflection\ParametersAcceptor;
19+
use PHPStan\Reflection\Php\PhpFunctionFromParserNodeReflection;
1920
use PHPStan\TrinaryLogic;
2021
use PHPStan\Type\Type;
2122
use PHPStan\Type\TypeWithClassName;
@@ -49,10 +50,7 @@ public function isInTrait(): bool;
4950

5051
public function getTraitReflection(): ?ClassReflection;
5152

52-
/**
53-
* @return FunctionReflection|ExtendedMethodReflection|null
54-
*/
55-
public function getFunction();
53+
public function getFunction(): ?PhpFunctionFromParserNodeReflection;
5654

5755
public function getFunctionName(): ?string;
5856

src/Node/FunctionReturnStatementsNode.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use PhpParser\NodeAbstract;
1010
use PHPStan\Analyser\ImpurePoint;
1111
use PHPStan\Analyser\StatementResult;
12-
use PHPStan\Reflection\FunctionReflection;
12+
use PHPStan\Reflection\Php\PhpFunctionFromParserNodeReflection;
1313
use function count;
1414

1515
/**
@@ -32,7 +32,7 @@ public function __construct(
3232
private StatementResult $statementResult,
3333
private array $executionEnds,
3434
private array $impurePoints,
35-
private FunctionReflection $functionReflection,
35+
private PhpFunctionFromParserNodeReflection $functionReflection,
3636
)
3737
{
3838
parent::__construct($function->getAttributes());
@@ -91,7 +91,7 @@ public function getSubNodeNames(): array
9191
return [];
9292
}
9393

94-
public function getFunctionReflection(): FunctionReflection
94+
public function getFunctionReflection(): PhpFunctionFromParserNodeReflection
9595
{
9696
return $this->functionReflection;
9797
}

src/Node/MethodReturnStatementsNode.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use PHPStan\Analyser\ImpurePoint;
1111
use PHPStan\Analyser\StatementResult;
1212
use PHPStan\Reflection\ClassReflection;
13-
use PHPStan\Reflection\ExtendedMethodReflection;
13+
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
1414
use function count;
1515

1616
/**
@@ -36,7 +36,7 @@ public function __construct(
3636
private array $executionEnds,
3737
private array $impurePoints,
3838
private ClassReflection $classReflection,
39-
private ExtendedMethodReflection $methodReflection,
39+
private PhpMethodFromParserNodeReflection $methodReflection,
4040
)
4141
{
4242
parent::__construct($method->getAttributes());
@@ -88,7 +88,7 @@ public function getClassReflection(): ClassReflection
8888
return $this->classReflection;
8989
}
9090

91-
public function getMethodReflection(): ExtendedMethodReflection
91+
public function getMethodReflection(): PhpMethodFromParserNodeReflection
9292
{
9393
return $this->methodReflection;
9494
}

src/Rules/Api/ApiInstanceofRule.php

+3
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ private function processCoveredClass(Node\Expr\Instanceof_ $node, Scope $scope,
8484
if ($classReflection->getName() === Type::class || $classReflection->isSubclassOf(Type::class)) {
8585
return [];
8686
}
87+
if ($classReflection->isInterface()) {
88+
return [];
89+
}
8790

8891
$instanceofType = $scope->getType($node);
8992
if ($instanceofType->isTrue()->or($instanceofType->isFalse())->yes()) {

src/Rules/Functions/ReturnTypeRule.php

+2-6
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
use PhpParser\Node;
66
use PhpParser\Node\Stmt\Return_;
77
use PHPStan\Analyser\Scope;
8+
use PHPStan\Reflection\MethodReflection;
89
use PHPStan\Reflection\ParametersAcceptorSelector;
9-
use PHPStan\Reflection\Php\PhpFunctionFromParserNodeReflection;
10-
use PHPStan\Reflection\Php\PhpMethodFromParserNodeReflection;
1110
use PHPStan\Rules\FunctionReturnTypeCheck;
1211
use PHPStan\Rules\Rule;
1312
use function sprintf;
@@ -40,10 +39,7 @@ public function processNode(Node $node, Scope $scope): array
4039
}
4140

4241
$function = $scope->getFunction();
43-
if (
44-
!($function instanceof PhpFunctionFromParserNodeReflection)
45-
|| $function instanceof PhpMethodFromParserNodeReflection
46-
) {
42+
if ($function instanceof MethodReflection) {
4743
return [];
4844
}
4945

0 commit comments

Comments
 (0)