Skip to content

Commit 023b231

Browse files
author
Simon Mönch
authored
Support parent return type (#466)
1 parent dbda568 commit 023b231

File tree

2 files changed

+176
-1
lines changed

2 files changed

+176
-1
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
return [
16+
'meta' => [
17+
'title' => 'Self and parent keywords as return types',
18+
// Default values. If not specified will be the one used
19+
'prefix' => 'Humbug',
20+
'whitelist' => [],
21+
'whitelist-global-constants' => true,
22+
'whitelist-global-classes' => false,
23+
'whitelist-global-functions' => true,
24+
'registered-classes' => [],
25+
'registered-functions' => [],
26+
],
27+
28+
'Usage for classes in the global scope' => <<<'PHP'
29+
<?php
30+
31+
class A {
32+
protected $name;
33+
public function __construct(string $name)
34+
{
35+
$this->name = $name;
36+
}
37+
public function normalize() : self
38+
{
39+
$instance = clone $this;
40+
$instance->name = \strtolower($this->name);
41+
return $instance;
42+
}
43+
public function getName() : string
44+
{
45+
return $this->name;
46+
}
47+
}
48+
49+
class B extends A {
50+
51+
public function normalize() : parent
52+
{
53+
$instance = clone $this;
54+
$instance->name = \strtoupper($this->name);
55+
return $instance;
56+
}
57+
}
58+
59+
echo (new B('yo'))->normalize()->getName().PHP_EOL;
60+
61+
----
62+
<?php
63+
64+
namespace Humbug;
65+
66+
class A
67+
{
68+
protected $name;
69+
public function __construct(string $name)
70+
{
71+
$this->name = $name;
72+
}
73+
public function normalize() : self
74+
{
75+
$instance = clone $this;
76+
$instance->name = \strtolower($this->name);
77+
return $instance;
78+
}
79+
public function getName() : string
80+
{
81+
return $this->name;
82+
}
83+
}
84+
class B extends \Humbug\A
85+
{
86+
public function normalize() : parent
87+
{
88+
$instance = clone $this;
89+
$instance->name = \strtoupper($this->name);
90+
return $instance;
91+
}
92+
}
93+
echo (new \Humbug\B('yo'))->normalize()->getName() . \PHP_EOL;
94+
95+
PHP
96+
,
97+
98+
'Usage for classes in a namespaced' => <<<'PHP'
99+
<?php
100+
101+
namespace Foo {
102+
class A {
103+
protected $name;
104+
public function __construct(string $name)
105+
{
106+
$this->name = $name;
107+
}
108+
public function normalize() : self
109+
{
110+
$instance = clone $this;
111+
$instance->name = \strtolower($this->name);
112+
return $instance;
113+
}
114+
public function getName() : string
115+
{
116+
return $this->name;
117+
}
118+
}
119+
120+
class B extends A {
121+
public function normalize() : parent
122+
{
123+
$instance = clone $this;
124+
$instance->name = strtoupper($this->name);
125+
return $instance;
126+
}
127+
}
128+
}
129+
130+
namespace {
131+
use Foo\B;
132+
133+
echo (new B('yo'))->normalize()->getName().PHP_EOL;
134+
}
135+
136+
----
137+
<?php
138+
139+
namespace Humbug\Foo;
140+
141+
class A
142+
{
143+
protected $name;
144+
public function __construct(string $name)
145+
{
146+
$this->name = $name;
147+
}
148+
public function normalize() : self
149+
{
150+
$instance = clone $this;
151+
$instance->name = \strtolower($this->name);
152+
return $instance;
153+
}
154+
public function getName() : string
155+
{
156+
return $this->name;
157+
}
158+
}
159+
class B extends \Humbug\Foo\A
160+
{
161+
public function normalize() : parent
162+
{
163+
$instance = clone $this;
164+
$instance->name = \strtoupper($this->name);
165+
return $instance;
166+
}
167+
}
168+
namespace Humbug;
169+
170+
use Humbug\Foo\B;
171+
echo (new \Humbug\Foo\B('yo'))->normalize()->getName() . \PHP_EOL;
172+
173+
PHP
174+
,
175+
];

src/PhpParser/NodeVisitor/NameStmtPrefixer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private function prefixName(Name $name): Node
194194
}
195195
}
196196

197-
if ('self' === (string) $resolvedName && $parentNode instanceof ClassMethod) {
197+
if ($parentNode instanceof ClassMethod && $resolvedName->isSpecialClassName()) {
198198
return $name;
199199
}
200200

0 commit comments

Comments
 (0)