Skip to content

Commit

Permalink
Optimization for currently analysed class reflection when there's onl…
Browse files Browse the repository at this point in the history
…y one instance of the same class in a project
  • Loading branch information
ondrejmirtes committed Jan 26, 2021
1 parent 05b1c84 commit d3435dd
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 26 deletions.
74 changes: 49 additions & 25 deletions src/Analyser/NodeScopeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -595,31 +595,7 @@ private function processStmtNode(
} elseif ($stmt instanceof Node\Stmt\ClassLike) {
$hasYield = false;
if (isset($stmt->namespacedName)) {
$nodeToReflection = new NodeToReflection();
$betterReflectionClass = $nodeToReflection->__invoke(
$this->classReflector,
$stmt,
new LocatedSource(FileReader::read($scope->getFile()), $scope->getFile()),
$scope->getNamespace() !== null ? new Node\Stmt\Namespace_(new Name($scope->getNamespace())) : null,
null
);
if (!$betterReflectionClass instanceof \Roave\BetterReflection\Reflection\ReflectionClass) {
throw new \PHPStan\ShouldNotHappenException();
}
$classReflection = new ClassReflection(
$this->reflectionProvider,
$this->fileTypeMapper,
$this->phpVersion,
$this->classReflectionExtensionRegistryProvider->getRegistry()->getPropertiesClassReflectionExtensions(),
$this->classReflectionExtensionRegistryProvider->getRegistry()->getMethodsClassReflectionExtensions(),
$betterReflectionClass->getName(),
new ReflectionClass($betterReflectionClass),
null,
null,
null,
sprintf('%s:%d', $scope->getFile(), $stmt->getStartLine())
);
$this->reflectionProvider->hasClass($classReflection->getName());
$classReflection = $this->getCurrentClassReflection($stmt, $scope);
$classScope = $scope->enterClass($classReflection);
$nodeCallback(new InClassNode($stmt, $classReflection), $classScope);
} elseif ($stmt instanceof Class_) {
Expand Down Expand Up @@ -1243,6 +1219,54 @@ private function processStmtNode(
return new StatementResult($scope, $hasYield, false, []);
}

private function getCurrentClassReflection(Node\Stmt\ClassLike $stmt, Scope $scope): ClassReflection
{
$className = $stmt->namespacedName->toString();
if (!$this->reflectionProvider->hasClass($className)) {
return $this->createAstClassReflection($stmt, $scope);
}

$defaultClassReflection = $this->reflectionProvider->getClass($stmt->namespacedName->toString());
if ($defaultClassReflection->getFileName() !== $scope->getFile()) {
return $this->createAstClassReflection($stmt, $scope);
}

$startLine = $defaultClassReflection->getNativeReflection()->getStartLine();
if ($startLine !== $stmt->getStartLine()) {
return $this->createAstClassReflection($stmt, $scope);
}

return $defaultClassReflection;
}

private function createAstClassReflection(Node\Stmt\ClassLike $stmt, Scope $scope): ClassReflection
{
$nodeToReflection = new NodeToReflection();
$betterReflectionClass = $nodeToReflection->__invoke(
$this->classReflector,
$stmt,
new LocatedSource(FileReader::read($scope->getFile()), $scope->getFile()),
$scope->getNamespace() !== null ? new Node\Stmt\Namespace_(new Name($scope->getNamespace())) : null
);
if (!$betterReflectionClass instanceof \Roave\BetterReflection\Reflection\ReflectionClass) {
throw new \PHPStan\ShouldNotHappenException();
}

return new ClassReflection(
$this->reflectionProvider,
$this->fileTypeMapper,
$this->phpVersion,
$this->classReflectionExtensionRegistryProvider->getRegistry()->getPropertiesClassReflectionExtensions(),
$this->classReflectionExtensionRegistryProvider->getRegistry()->getMethodsClassReflectionExtensions(),
$betterReflectionClass->getName(),
new ReflectionClass($betterReflectionClass),
null,
null,
null,
sprintf('%s:%d', $scope->getFile(), $stmt->getStartLine())
);
}

/**
* @param Node\Stmt\Catch_ $catchNode
* @param MutatingScope $catchScope
Expand Down
2 changes: 1 addition & 1 deletion tests/PHPStan/Analyser/NodeScopeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8630,7 +8630,7 @@ public function dataTraitsPhpDocs(): array
'$this->conflictingProperty',
],
[
'TraitPhpDocs\AmbiguousPropertyType',
self::$useStaticReflectionProvider ? 'TraitPhpDocs\AmbiguousPropertyType' : 'TraitPhpDocsTwo\AmbiguousPropertyType',
'$this->bogusProperty',
],
[
Expand Down

0 comments on commit d3435dd

Please sign in to comment.