Skip to content

Fix reflection error on unknown symbols #273

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/Rule/ForbidCheckedExceptionInCallableRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public function processFirstClassCallable(
$errors = array_merge($errors, $this->processCall($scope, $callerType, $methodName, $line, $nodeHash));
}

if ($callNode instanceof FuncCall && $callNode->name instanceof Name) {
if ($callNode instanceof FuncCall && $callNode->name instanceof Name && $this->reflectionProvider->hasFunction($callNode->name, $scope)) {
$functionReflection = $this->reflectionProvider->getFunction($callNode->name, $scope);
$errors = array_merge($errors, $this->processThrowType($functionReflection->getThrowType(), $scope, $line, $nodeHash));
}
Expand Down Expand Up @@ -427,7 +427,7 @@ private function whitelistAllowedCallables(CallLike $node, Scope $scope): void

} elseif ($node instanceof FuncCall && $node->name instanceof Name) {
$callerType = null;
$methodReflection = $this->reflectionProvider->getFunction($node->name, $scope);
$methodReflection = $this->getFunctionReflection($node->name, $scope);

} elseif ($node instanceof FuncCall && $this->isFirstClassCallableOrClosureOrArrowFunction($node->name)) { // immediately called callable syntax
$this->allowedCallables[spl_object_hash($node->name)] = true;
Expand Down Expand Up @@ -534,4 +534,11 @@ private function buildError(
return $builder->build();
}

private function getFunctionReflection(Name $functionName, Scope $scope): ?FunctionReflection
{
return $this->reflectionProvider->hasFunction($functionName, $scope)
? $this->reflectionProvider->getFunction($functionName, $scope)
: null;
}

}
4 changes: 4 additions & 0 deletions src/Rule/ForbidCustomFunctionsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ private function validateCallOverExpr(array $methodNames, Expr $expr, Scope $sco
*/
private function validateMethod(array $methodNames, string $className): array
{
if (!$this->reflectionProvider->hasClass($className)) {
return [];
}

$errors = [];

foreach ($this->reflectionProvider->getClass($className)->getAncestors() as $ancestor) {
Expand Down
15 changes: 14 additions & 1 deletion src/Rule/ForbidEnumInFunctionArgumentsRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use PhpParser\Node\Name;
use PHPStan\Analyser\ArgumentsNormalizer;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\FunctionReflection;
use PHPStan\Reflection\ParametersAcceptorSelector;
use PHPStan\Reflection\ReflectionProvider;
use PHPStan\Rules\IdentifierRuleError;
Expand Down Expand Up @@ -86,7 +87,12 @@ public function processNode(Node $node, Scope $scope): array

$wrongArguments = [];

$functionReflection = $this->reflectionProvider->getFunction($node->name, $scope);
$functionReflection = $this->getFunctionReflection($node->name, $scope);

if ($functionReflection === null) {
return [];
}

$parametersAcceptor = ParametersAcceptorSelector::selectFromArgs($scope, $node->getArgs(), $functionReflection->getVariants());
$funcCall = ArgumentsNormalizer::reorderFuncArguments($parametersAcceptor, $node);

Expand Down Expand Up @@ -150,4 +156,11 @@ private function containsEnum(Type $type): bool
return $type->isEnum()->yes();
}

private function getFunctionReflection(Name $functionName, Scope $scope): ?FunctionReflection
{
return $this->reflectionProvider->hasFunction($functionName, $scope)
? $this->reflectionProvider->getFunction($functionName, $scope)
: null;
}

}
17 changes: 17 additions & 0 deletions tests/Rule/data/unknown-symbol.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace UnknownSymbol;

class A {
public function aMethod(): bool {
return true;
}
}

function doFoo(B $b) {
/** @var A $a */
$a = createMagically();
$a->aMethod();
$b->bMethod();
}

58 changes: 58 additions & 0 deletions tests/UnknownSymbolTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php declare(strict_types = 1);

namespace ShipMonk\PHPStan;

use PHPStan\Analyser\Analyser;
use PHPStan\Analyser\AnalyserResultFinalizer;
use PHPStan\Analyser\Error;
use PHPStan\Testing\PHPStanTestCase;
use function array_map;
use function array_merge;

class UnknownSymbolTest extends PHPStanTestCase
{

/**
* @return string[]
*/
public static function getAdditionalConfigFiles(): array
{
return array_merge(
parent::getAdditionalConfigFiles(),
[__DIR__ . '/../rules.neon'],
);
}

public function testNoInternalError(): void
{
$errors = $this->runAnalyser([__DIR__ . '/Rule/data/unknown-symbol.php']);

$internalErrors = [];

foreach ($errors as $error) {
if ($error->getIdentifier() === 'phpstan.internal') {
$internalErrors[] = $error->getMessage();
}
}

self::assertSame([], $internalErrors);
}

/**
* @param list<string> $filePaths
* @return list<Error>
*/
private function runAnalyser(array $filePaths): array
{
$analyser = self::getContainer()->getByType(Analyser::class); // @phpstan-ignore phpstanApi.classConstant
$finalizer = self::getContainer()->getByType(AnalyserResultFinalizer::class); // @phpstan-ignore phpstanApi.classConstant

$normalizedFilePaths = array_map(fn (string $path): string => $this->getFileHelper()->normalizePath($path), $filePaths);

$analyserResult = $analyser->analyse($normalizedFilePaths);
$analyserResult = $finalizer->finalize($analyserResult, true, false)->getAnalyserResult();

return $analyserResult->getErrors();
}

}
Loading