Skip to content

Commit

Permalink
[PHPStan 1.0] resolve FunctionReflection getFileName() from main inte…
Browse files Browse the repository at this point in the history
…rface (#1117)

Co-authored-by: GitHub Action <action@github.com>
  • Loading branch information
TomasVotruba and actions-user committed Oct 31, 2021
1 parent 4ddd986 commit da4d598
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 24 deletions.
13 changes: 4 additions & 9 deletions rules/CodingStyle/Reflection/VendorLocationDetector.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Php\PhpFunctionReflection;
use PHPStan\Reflection\ReflectionWithFilename;
use Symplify\SmartFileSystem\Normalizer\PathNormalizer;

Expand All @@ -33,20 +32,16 @@ public function detectFunctionLikeReflection(

private function resolveReflectionFileName(
MethodReflection | ReflectionWithFilename | FunctionReflection $reflection
): string | null {
): ?string {
if ($reflection instanceof ReflectionWithFilename) {
return $reflection->getFileName();
}

if ($reflection instanceof PhpFunctionReflection) {
if ($reflection instanceof FunctionReflection) {
return $reflection->getFileName();
}

if ($reflection instanceof MethodReflection) {
$declaringClassReflection = $reflection->getDeclaringClass();
return $declaringClassReflection->getFileName();
}

return null;
$declaringClassReflection = $reflection->getDeclaringClass();
return $declaringClassReflection->getFileName();
}
}
3 changes: 1 addition & 2 deletions src/NodeAnalyzer/VariadicAnalyzer.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use PhpParser\Node\Stmt\Function_;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Php\PhpFunctionReflection;
use Rector\Core\PhpParser\AstResolver;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
use Rector\Core\Reflection\ReflectionResolver;
Expand All @@ -38,7 +37,7 @@ public function hasVariadicParameters(FuncCall | StaticCall | MethodCall $call):
return true;
}

if ($functionLikeReflection instanceof PhpFunctionReflection) {
if ($functionLikeReflection instanceof FunctionReflection) {
$function = $this->astResolver->resolveFunctionFromFunctionReflection($functionLikeReflection);
if (! $function instanceof Function_) {
return false;
Expand Down
22 changes: 9 additions & 13 deletions src/PhpParser/AstResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
use PhpParser\NodeFinder;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\ClassReflection;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\MethodReflection;
use PHPStan\Reflection\Php\PhpFunctionReflection;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Type\TypeWithClassName;
use Rector\Core\PhpParser\Node\BetterNodeFinder;
Expand Down Expand Up @@ -139,21 +139,21 @@ public function resolveClassMethodOrFunctionFromCall(
return $this->resolveClassMethodFromCall($call);
}

public function resolveFunctionFromFunctionReflection(PhpFunctionReflection $phpFunctionReflection): ?Function_
public function resolveFunctionFromFunctionReflection(FunctionReflection $functionReflection): ?Function_
{
if (isset($this->functionsByName[$phpFunctionReflection->getName()])) {
return $this->functionsByName[$phpFunctionReflection->getName()];
if (isset($this->functionsByName[$functionReflection->getName()])) {
return $this->functionsByName[$functionReflection->getName()];
}

$fileName = $phpFunctionReflection->getFileName();
$fileName = $functionReflection->getFileName();
if ($fileName === null) {
return null;
}

$fileContent = $this->smartFileSystem->readFile($fileName);
if (! is_string($fileContent)) {
// to avoid parsing missing function again
$this->functionsByName[$phpFunctionReflection->getName()] = null;
$this->functionsByName[$functionReflection->getName()] = null;
return null;
}

Expand All @@ -165,18 +165,18 @@ public function resolveFunctionFromFunctionReflection(PhpFunctionReflection $php
/** @var Function_[] $functions */
$functions = $this->nodeFinder->findInstanceOf($nodes, Function_::class);
foreach ($functions as $function) {
if (! $this->nodeNameResolver->isName($function, $phpFunctionReflection->getName())) {
if (! $this->nodeNameResolver->isName($function, $functionReflection->getName())) {
continue;
}

// to avoid parsing missing function again
$this->functionsByName[$phpFunctionReflection->getName()] = $function;
$this->functionsByName[$functionReflection->getName()] = $function;

return $function;
}

// to avoid parsing missing function again
$this->functionsByName[$phpFunctionReflection->getName()] = null;
$this->functionsByName[$functionReflection->getName()] = null;

return null;
}
Expand Down Expand Up @@ -372,10 +372,6 @@ private function resolveFunctionFromFuncCall(FuncCall $funcCall, Scope $scope):
}

$reflectionFunction = $this->reflectionProvider->getFunction($funcCall->name, $scope);
if (! $reflectionFunction instanceof PhpFunctionReflection) {
return null;
}

return $this->resolveFunctionFromFunctionReflection($reflectionFunction);
}
}

0 comments on commit da4d598

Please sign in to comment.