fix(compiler): 修复接口中self返回类型解析错误 #22
Merged
韩天峰
merged 2 commits from fix-interface-method-self-return into master 1 month ago
5 changed files with 106 additions and 2 deletions
@ -0,0 +1,42 @@ |
|||||||
|
--TEST-- |
||||||
|
interface method with `self` return type implemented by class (fluent interface), and namespace block containing comments |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
|
||||||
|
namespace { |
||||||
|
interface TestInterface |
||||||
|
{ |
||||||
|
public function get(): self; |
||||||
|
} |
||||||
|
|
||||||
|
class TestClass implements TestInterface |
||||||
|
{ |
||||||
|
public int $value = 0; |
||||||
|
|
||||||
|
public function get(): self |
||||||
|
{ |
||||||
|
return $this; |
||||||
|
} |
||||||
|
|
||||||
|
public function setValue(int $value): self |
||||||
|
{ |
||||||
|
$this->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) |
||||||
@ -0,0 +1,56 @@ |
|||||||
|
--TEST-- |
||||||
|
interface method `self` return type resolves to the interface's fully-qualified name inside a named namespace |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
|
||||||
|
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) |
||||||
|
class Widget implements Chainable |
||||||
|
{ |
||||||
|
public array $log = []; |
||||||
|
|
||||||
|
public function chain(): self |
||||||
|
{ |
||||||
|
$this->log[] = 'chain'; |
||||||
|
return $this; |
||||||
|
} |
||||||
|
|
||||||
|
public function maybe(bool $present): ?self |
||||||
|
{ |
||||||
|
return $present ? $this : null; |
||||||
|
} |
||||||
|
|
||||||
|
public function combine(Chainable $other): self |
||||||
|
{ |
||||||
|
return $this; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
namespace { |
||||||
|
function main() |
||||||
|
{ |
||||||
|
$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) |
||||||
Loading…
Reference in new issue