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