Skip to content
Merged
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
17 changes: 1 addition & 16 deletions src/Analyser/AnonymousClassNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,7 @@ final class AnonymousClassNode
use NodeQueryTrait;
use PropertyUnserializeTrait;
use RecursiveParentsTrait;

/**
* Scope label reported by {@see enclosingScopeName()} for an anonymous
* class declared outside any class-like or named function.
*/
public const FILE_SCOPE = 'file scope';
use ScopeTrait;

/** @var list<string> */
public readonly array $layers;
Expand Down Expand Up @@ -88,14 +83,4 @@ public function __construct(
) {
$this->layers = $layers ?: array_filter([$this->layer]);
}

/**
* Label of the innermost named scope declaring this anonymous class —
* the enclosing class-like, else the enclosing named function — or
* {@see self::FILE_SCOPE} for one declared in top-level procedural code.
*/
public function enclosingScopeName(): string
{
return $this->enclosingClassName ?? $this->enclosingFunctionName ?? self::FILE_SCOPE;
}
}
17 changes: 1 addition & 16 deletions src/Analyser/AnonymousFunctionNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,7 @@ final class AnonymousFunctionNode
{
use NodeQueryTrait;
use PropertyUnserializeTrait;

/**
* Scope label reported by {@see enclosingScopeName()} for an anonymous
* function declared outside any class-like or named function.
*/
public const FILE_SCOPE = 'file scope';
use ScopeTrait;

/** @var list<string> */
public readonly array $layers;
Expand Down Expand Up @@ -70,14 +65,4 @@ public function getType(): string
{
return $this->isArrowFunction ? 'Arrow function' : 'Closure';
}

/**
* Label of the innermost named scope declaring this anonymous function —
* the enclosing class-like, else the enclosing named function — or
* {@see self::FILE_SCOPE} for one declared in top-level procedural code.
*/
public function enclosingScopeName(): string
{
return $this->enclosingClassName ?? $this->enclosingFunctionName ?? self::FILE_SCOPE;
}
}
33 changes: 33 additions & 0 deletions src/Analyser/ScopeTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

namespace Boundwize\StructArmed\Analyser;

/**
* Enclosing-scope label shared by {@see AnonymousClassNode} and
* {@see AnonymousFunctionNode}.
*
* @internal
*
* @property-read string|null $enclosingClassName Innermost named class-like the node is declared in
* @property-read string|null $enclosingFunctionName Innermost named function the node is declared in
*/
trait ScopeTrait
{
/**
* Scope label reported by {@see enclosingScopeName()} for an anonymous
* class/function declared outside any class-like or named function.
*/
public const FILE_SCOPE = 'file scope';

/**
* Label of the innermost named scope declaring this anonymous class/function —
* the enclosing class-like, else the enclosing named function — or
* {@see self::FILE_SCOPE} for one declared in top-level procedural code.
*/
public function enclosingScopeName(): string
{
return $this->enclosingClassName ?? $this->enclosingFunctionName ?? self::FILE_SCOPE;
}
}
Loading