From 1d5c775db58f0a443c76b461e251fe76d5b0c493 Mon Sep 17 00:00:00 2001 From: Abdul Malik Ikhsan Date: Sat, 7 Nov 2026 19:15:03 +0700 Subject: [PATCH] Extract shared enclosing-scope label into ScopeTrait --- src/Analyser/AnonymousClassNode.php | 17 +------------ src/Analyser/AnonymousFunctionNode.php | 17 +------------ src/Analyser/ScopeTrait.php | 33 ++++++++++++++++++++++++++ 3 files changed, 35 insertions(+), 32 deletions(-) create mode 100644 src/Analyser/ScopeTrait.php diff --git a/src/Analyser/AnonymousClassNode.php b/src/Analyser/AnonymousClassNode.php index a7777877..b3f77cb4 100644 --- a/src/Analyser/AnonymousClassNode.php +++ b/src/Analyser/AnonymousClassNode.php @@ -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 */ public readonly array $layers; @@ -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; - } } diff --git a/src/Analyser/AnonymousFunctionNode.php b/src/Analyser/AnonymousFunctionNode.php index e24bd417..a8af8150 100644 --- a/src/Analyser/AnonymousFunctionNode.php +++ b/src/Analyser/AnonymousFunctionNode.php @@ -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 */ public readonly array $layers; @@ -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; - } } diff --git a/src/Analyser/ScopeTrait.php b/src/Analyser/ScopeTrait.php new file mode 100644 index 00000000..9139616e --- /dev/null +++ b/src/Analyser/ScopeTrait.php @@ -0,0 +1,33 @@ +enclosingClassName ?? $this->enclosingFunctionName ?? self::FILE_SCOPE; + } +}