Skip to content

Add option to disable recursive refs in Name Resolution #1075

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

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
10 changes: 7 additions & 3 deletions lib/PhpParser/NameContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,17 @@ class NameContext {
/** @var ErrorHandler Error handler */
protected ErrorHandler $errorHandler;

protected bool $noCyclicRefs;

/**
* Create a name context.
*
* @param ErrorHandler $errorHandler Error handling used to report errors
* @param array{noCyclicRefs?:bool} $options
*/
public function __construct(ErrorHandler $errorHandler) {
public function __construct(ErrorHandler $errorHandler, array $options = []) {
$this->errorHandler = $errorHandler;
$this->noCyclicRefs = $options['noCyclicRefs'] ?? false;
}

/**
Expand Down Expand Up @@ -107,12 +111,12 @@ public function getResolvedName(Name $name, int $type): ?Name {
$name->getAttributes()
));
}
return $name;
return $this->noCyclicRefs ? clone $name : $name;
}

// fully qualified names are already resolved
if ($name->isFullyQualified()) {
return $name;
return $this->noCyclicRefs ? clone $name : $name;
}

// Try to resolve aliases
Expand Down
7 changes: 5 additions & 2 deletions lib/PhpParser/NodeVisitor/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,13 @@ class NameResolver extends NodeVisitorAbstract {
* namespacedName attribute, as usual.)
*
* @param ErrorHandler|null $errorHandler Error handler
* @param array{preserveOriginalNames?: bool, replaceNodes?: bool} $options Options
* @param array{preserveOriginalNames?: bool, replaceNodes?: bool, noCyclicRefs?: bool} $options Options
*/
public function __construct(?ErrorHandler $errorHandler = null, array $options = []) {
$this->nameContext = new NameContext($errorHandler ?? new ErrorHandler\Throwing());
$this->nameContext = new NameContext(
$errorHandler ?? new ErrorHandler\Throwing(),
['noCyclicRefs' => $options['noCyclicRefs'] ?? false],
);
$this->preserveOriginalNames = $options['preserveOriginalNames'] ?? false;
$this->replaceNodes = $options['replaceNodes'] ?? true;
}
Expand Down