Skip to content

Commit

Permalink
Remove Context::withNameContext()
Browse files Browse the repository at this point in the history
  • Loading branch information
vudaltsov committed Oct 27, 2024
1 parent d5be1e5 commit 50ae86f
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 24 deletions.
18 changes: 7 additions & 11 deletions src/Internal/Context/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,18 @@ private function __construct(
/**
* @param ?non-empty-string $file
*/
public static function start(string $code, ?string $file = null): self
public static function start(string $code, ?string $file = null, ?NameContext $nameContext = null): self
{
$nameContext = new NameContext(new Throwing());
$nameContext->startNamespace();
if ($nameContext === null) {
$nameContext = new NameContext(new Throwing());
$nameContext->startNamespace();
} else {
$nameContext = clone $nameContext;
}

return new self($code, $file, $nameContext);
}

public function withNameContext(NameContext $nameContext): self
{
$context = clone $this;
$context->nameContext = clone $nameContext;

return $context;
}

/**
* @param non-empty-string $name
* @param list<non-empty-string> $templateNames
Expand Down
25 changes: 12 additions & 13 deletions src/Internal/Context/ContextVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
*/
final class ContextVisitor extends NodeVisitorAbstract implements ContextProvider
{
private readonly Context $codeContext;

/**
* @var list<Context>
*/
Expand All @@ -38,18 +36,15 @@ final class ContextVisitor extends NodeVisitorAbstract implements ContextProvide
* @param ?non-empty-string $file
*/
public function __construct(
string $code,
?string $file,
private readonly string $code,
private readonly ?string $file,
private readonly NameContext $nameContext,
private readonly AnnotatedDeclarationsDiscoverer $annotatedDeclarationsDiscoverer = NullAnnotatedDeclarationsDiscoverer::Instance,
) {
$this->codeContext = Context::start($code, $file);
}
) {}

public function get(): Context
{
return array_value_last($this->contextStack)
?? $this->codeContext->withNameContext($this->nameContext);
return array_value_last($this->contextStack) ?? Context::start($this->code, $this->file, $this->nameContext);
}

public function beforeTraverse(array $nodes): ?array
Expand Down Expand Up @@ -78,9 +73,11 @@ public function enterNode(Node $node): ?int
$position = $node->getStartFilePos();
\assert($position >= 0);

$this->contextStack[] = $this->get()->enterAnonymousFunction(
$context = $this->get();

$this->contextStack[] = $context->enterAnonymousFunction(
line: $line,
column: $this->codeContext->column($position),
column: $context->column($position),
templateNames: $this->annotatedDeclarationsDiscoverer->discoverAnnotatedDeclarations($node)->templateNames,
);

Expand All @@ -96,9 +93,11 @@ public function enterNode(Node $node): ?int
$position = $node->getStartFilePos();
\assert($position >= 0);

$this->contextStack[] = $this->get()->enterAnonymousClass(
$context = $this->get();

$this->contextStack[] = $context->enterAnonymousClass(
line: $line,
column: $this->codeContext->column($position),
column: $context->column($position),
parentName: $node->extends?->toString(),
aliasNames: $typeNames->aliasNames,
templateNames: $typeNames->templateNames,
Expand Down

0 comments on commit 50ae86f

Please sign in to comment.