Skip to content
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

Detection of circular references #1240

Merged
merged 2 commits into from Sep 27, 2022
Merged

Detection of circular references #1240

merged 2 commits into from Sep 27, 2022

Conversation

kukulich
Copy link
Collaborator

Fixes #1165

Comment on lines 1063 to 1085
if ($this->cachedParentClassNames === null) {
$parentClassNames = [];

$parentClass = $this->getParentClass();
while ($parentClass !== null) {
$parentClassName = $parentClass->getName();

if (
$this->name === $parentClassName
|| in_array($parentClassName, $parentClassNames, true)
) {
throw CircularReference::fromClassName($parentClassName);
}

$parentClassNames[] = $parentClassName;
$parentClass = $parentClass->getParentClass();
}

$this->cachedParentClassNames = $parentClassNames;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we implement this check without having a mutable $this->cachedParentClassNames? Thinking of a by-ref parameter in a private method 🤔

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is is such a problem? ReflectionClass already have some more caches...

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not, just trying to push the design for less stateful changes: can roll with the current solution too.

In practice, at some point, my aim is to have everything readonly :)

@herndlm
Copy link
Contributor

herndlm commented Sep 27, 2022

thank you for working on this! You beat me to opening a PR :) Looks like this will make it into 6.0, nice

@kukulich kukulich marked this pull request as ready for review September 27, 2022 15:36
Copy link
Member

@Ocramius Ocramius left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, nice! 👍

@Ocramius Ocramius self-assigned this Sep 27, 2022
@Ocramius Ocramius merged commit 0c2838c into Roave:6.0.x Sep 27, 2022
@Ocramius
Copy link
Member

Thanks @kukulich @herndlm!

@kukulich kukulich deleted the parents branch September 27, 2022 16:31
@staabm
Copy link
Contributor

staabm commented Sep 27, 2022

Nice. Maybe this is similar to the endless loop we see in phpstan/phpstan#8054 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Better detection of circular references
4 participants