Skip to content

Commit

Permalink
Evict private symbols from cache after class analysis
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Apr 14, 2022
1 parent fc1d8c5 commit 790678c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/Analyser/NodeScopeResolver.php
Expand Up @@ -639,6 +639,7 @@ private function processStmtNode(
$nodeCallback(new ClassPropertiesNode($stmt, $classStatementsGatherer->getProperties(), $classStatementsGatherer->getPropertyUsages(), $classStatementsGatherer->getMethodCalls()), $classScope);
$nodeCallback(new ClassMethodsNode($stmt, $classStatementsGatherer->getMethods(), $classStatementsGatherer->getMethodCalls()), $classScope);
$nodeCallback(new ClassConstantsNode($stmt, $classStatementsGatherer->getConstants(), $classStatementsGatherer->getConstantFetches()), $classScope);
$classReflection->evictPrivateSymbols();
} elseif ($stmt instanceof Node\Stmt\Property) {
$hasYield = false;
$throwPoints = [];
Expand Down
26 changes: 26 additions & 0 deletions src/Reflection/ClassReflection.php
Expand Up @@ -438,6 +438,32 @@ private function getPhpExtension(): PhpClassReflectionExtension
return $extension;
}

public function evictPrivateSymbols(): void
{
foreach ($this->constants as $name => $constant) {
if (!$constant->isPrivate()) {
continue;
}

unset($this->constants[$name]);
}
foreach ($this->properties as $name => $property) {
if (!$property->isPrivate()) {
continue;
}

unset($this->properties[$name]);
}
foreach ($this->methods as $name => $method) {
if (!$method->isPrivate()) {
continue;
}

unset($this->methods[$name]);
}
$this->getPhpExtension()->evictPrivateSymbols($this->getCacheKey());
}

public function getProperty(string $propertyName, ClassMemberAccessAnswerer $scope): PropertyReflection
{
if ($this->isEnum()) {
Expand Down
48 changes: 48 additions & 0 deletions src/Reflection/Php/PhpClassReflectionExtension.php
Expand Up @@ -108,6 +108,54 @@ public function __construct(
{
}

public function evictPrivateSymbols(string $classCacheKey): void
{
foreach ($this->propertiesIncludingAnnotations as $key => $properties) {
if ($key !== $classCacheKey) {
continue;
}
foreach ($properties as $name => $property) {
if (!$property->isPrivate()) {
continue;
}
unset($this->propertiesIncludingAnnotations[$key][$name]);
}
}
foreach ($this->nativeProperties as $key => $properties) {
if ($key !== $classCacheKey) {
continue;
}
foreach ($properties as $name => $property) {
if (!$property->isPrivate()) {
continue;
}
unset($this->nativeProperties[$key][$name]);
}
}
foreach ($this->methodsIncludingAnnotations as $key => $methods) {
if ($key !== $classCacheKey) {
continue;
}
foreach ($methods as $name => $method) {
if (!$method->isPrivate()) {
continue;
}
unset($this->methodsIncludingAnnotations[$key][$name]);
}
}
foreach ($this->nativeMethods as $key => $methods) {
if ($key !== $classCacheKey) {
continue;
}
foreach ($methods as $name => $method) {
if (!$method->isPrivate()) {
continue;
}
unset($this->nativeMethods[$key][$name]);
}
}
}

public function hasProperty(ClassReflection $classReflection, string $propertyName): bool
{
return $classReflection->getNativeReflection()->hasProperty($propertyName);
Expand Down

0 comments on commit 790678c

Please sign in to comment.