From f9516c43c98226a4d33cb0d3377feadbdab58dbe Mon Sep 17 00:00:00 2001 From: Yurun Date: Wed, 15 Jul 2026 19:47:04 +0800 Subject: [PATCH 1/2] =?UTF-8?q?fix(compiler):=20=E4=BF=AE=E5=A4=8D?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E4=B8=ADself=E8=BF=94=E5=9B=9E=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E8=A7=A3=E6=9E=90=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CompilerBase.php | 5 ++- .../class/interface-method-self-return.phpt | 42 +++++++++++++++++++ .../interface-self-return-namespaced.phpt | 36 ++++++++++++++++ 3 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 tests/compiler/class/interface-method-self-return.phpt create mode 100644 tests/compiler/namespace/interface-self-return-namespaced.phpt diff --git a/src/CompilerBase.php b/src/CompilerBase.php index 074a1517..fab95bf8 100644 --- a/src/CompilerBase.php +++ b/src/CompilerBase.php @@ -1017,7 +1017,10 @@ class CompilerBase implements PropertyAccessContext protected function getFullClassName(): string { - return ltrim($this->namespace . '\\' . $this->class, '\\'); + // 在接口上下文中,$this->class 为空但 $this->interface 已设置, + // `self` 类型声明应解析为接口自身的完整名称。 + $classLike = $this->class !== '' ? $this->class : $this->interface; + return ltrim($this->namespace . '\\' . $classLike, '\\'); } protected function getFullMethodName(string $fullClassName, string $method): string diff --git a/tests/compiler/class/interface-method-self-return.phpt b/tests/compiler/class/interface-method-self-return.phpt new file mode 100644 index 00000000..90e44eab --- /dev/null +++ b/tests/compiler/class/interface-method-self-return.phpt @@ -0,0 +1,42 @@ +--TEST-- +interface method with `self` return type implemented by class (fluent interface), and namespace block containing comments +--FILE-- +value = $value; + return $this; + } + } + + function main() + { + $test = new TestClass; + // get() returns self, so the result still satisfies the interface + var_dump($test->get() instanceof TestInterface); + var_dump($test === $test->get()); + // fluent chaining of self-returning methods + var_dump($test->get()->setValue(42)->value); + } +} +?> +--EXPECT-- +bool(true) +bool(true) +int(42) diff --git a/tests/compiler/namespace/interface-self-return-namespaced.phpt b/tests/compiler/namespace/interface-self-return-namespaced.phpt new file mode 100644 index 00000000..cfda0bad --- /dev/null +++ b/tests/compiler/namespace/interface-self-return-namespaced.phpt @@ -0,0 +1,36 @@ +--TEST-- +interface method `self` return type resolves to the interface's fully-qualified name inside a named namespace +--FILE-- +log[] = 'chain'; + return $this; + } + } +} + +namespace { + function main() + { + $w = new \App\Widget(); + var_dump($w->chain()->chain() instanceof \App\Chainable); + var_dump(count($w->log)); + } +} +?> +--EXPECT-- +bool(true) +int(2) From 6162185bd76c283c2a669370d24949e19c7fd5cc Mon Sep 17 00:00:00 2001 From: tianfenghan Date: Sun, 19 Jul 2026 11:04:28 +0800 Subject: [PATCH 2/2] fix(compiler): scope interface self type resolution --- src/CompilerBase.php | 11 ++++++---- src/Generator/TypeCheckGenerator.php | 2 +- src/Resolver/NameResolutionTrait.php | 2 +- .../interface-self-return-namespaced.phpt | 20 +++++++++++++++++++ 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/CompilerBase.php b/src/CompilerBase.php index fab95bf8..310a79b7 100644 --- a/src/CompilerBase.php +++ b/src/CompilerBase.php @@ -1017,10 +1017,13 @@ class CompilerBase implements PropertyAccessContext protected function getFullClassName(): string { - // 在接口上下文中,$this->class 为空但 $this->interface 已设置, - // `self` 类型声明应解析为接口自身的完整名称。 - $classLike = $this->class !== '' ? $this->class : $this->interface; - return ltrim($this->namespace . '\\' . $classLike, '\\'); + return ltrim($this->namespace . '\\' . $this->class, '\\'); + } + + protected function getFullClassLikeName(): string + { + $name = $this->class !== '' ? $this->class : $this->interface; + return ltrim($this->namespace . '\\' . $name, '\\'); } protected function getFullMethodName(string $fullClassName, string $method): string diff --git a/src/Generator/TypeCheckGenerator.php b/src/Generator/TypeCheckGenerator.php index 16dc4ae3..292967a1 100644 --- a/src/Generator/TypeCheckGenerator.php +++ b/src/Generator/TypeCheckGenerator.php @@ -104,7 +104,7 @@ trait TypeCheckGenerator } if ($name === 'self') { - $class = $this->getFullClassName(); + $class = $this->getFullClassLikeName(); } elseif ($name === 'parent') { $class = $this->classDef->extends ?? ''; } elseif ($name === 'static') { diff --git a/src/Resolver/NameResolutionTrait.php b/src/Resolver/NameResolutionTrait.php index 307117e5..827db4a3 100644 --- a/src/Resolver/NameResolutionTrait.php +++ b/src/Resolver/NameResolutionTrait.php @@ -166,7 +166,7 @@ trait NameResolutionTrait return $this->getTypeFromZendType($typeNameLower); } else { if ($typeName === 'self') { - $class = $this->getFullClassName(); + $class = $this->getFullClassLikeName(); } elseif ($typeName === 'parent') { if (!$this->classDef) { $this->fatalError($type, 'Cannot use "parent" type declaration outside a class'); diff --git a/tests/compiler/namespace/interface-self-return-namespaced.phpt b/tests/compiler/namespace/interface-self-return-namespaced.phpt index cfda0bad..4d05a98a 100644 --- a/tests/compiler/namespace/interface-self-return-namespaced.phpt +++ b/tests/compiler/namespace/interface-self-return-namespaced.phpt @@ -7,6 +7,10 @@ namespace App { interface Chainable { public function chain(): self; + + public function maybe(bool $present): ?self; + + public function combine(self $other): self; } // comment inside a named namespace block (Stmt_Nop) @@ -19,6 +23,16 @@ namespace App { $this->log[] = 'chain'; return $this; } + + public function maybe(bool $present): ?self + { + return $present ? $this : null; + } + + public function combine(Chainable $other): self + { + return $this; + } } } @@ -28,9 +42,15 @@ namespace { $w = new \App\Widget(); var_dump($w->chain()->chain() instanceof \App\Chainable); var_dump(count($w->log)); + var_dump($w->maybe(true) instanceof \App\Chainable); + var_dump($w->maybe(false)); + var_dump($w->combine(new \App\Widget()) === $w); } } ?> --EXPECT-- bool(true) int(2) +bool(true) +NULL +bool(true)