Skip to content

Commit 227a4fd

Browse files
authored
Fix special keywords and whitelisted declarations (#97)
- Fix usage of the keywords `static`, `self` and `parent` - Do not prefix the namespace if a whitelisted class is declared in it
2 parents 9154d8c + f2cbbe8 commit 227a4fd

17 files changed

+1073
-24
lines changed

specs/class/abstract.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,33 @@ public abstract function b();
6666
PHP
6767
,
6868

69+
'Declaration of a whitelisted namespaced class: do not prefix the namespace.' => [
70+
'whitelist' => ['Foo\A'],
71+
'payload' => <<<'PHP'
72+
<?php
73+
74+
namespace Foo;
75+
76+
abstract class A {
77+
public function a() {}
78+
abstract public function b();
79+
}
80+
----
81+
<?php
82+
83+
namespace Foo;
84+
85+
abstract class A
86+
{
87+
public function a()
88+
{
89+
}
90+
public abstract function b();
91+
}
92+
93+
PHP
94+
],
95+
6996
'Multiple declarations in different namespaces: prefix each namespace.' => <<<'PHP'
7097
<?php
7198

specs/class/anonymous.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,22 @@ public function test()
5050
{
5151
}
5252
};
53-
new class extends A implements B, C
53+
new class extends \A implements \B, \C
5454
{
5555
};
5656
new class
5757
{
5858
public $foo;
5959
};
60-
new class($a, $b) extends A
60+
new class($a, $b) extends \A
6161
{
6262
use T;
6363
};
6464
class A
6565
{
6666
public function test()
6767
{
68-
return new class($this) extends A
68+
return new class($this) extends \A
6969
{
7070
const A = 'B';
7171
};
@@ -112,22 +112,22 @@ public function test()
112112
{
113113
}
114114
};
115-
new class extends A implements B, C
115+
new class extends \Humbug\Foo\A implements \Humbug\Foo\B, \Humbug\Foo\C
116116
{
117117
};
118118
new class
119119
{
120120
public $foo;
121121
};
122-
new class($a, $b) extends A
122+
new class($a, $b) extends \Humbug\Foo\A
123123
{
124124
use T;
125125
};
126126
class A
127127
{
128128
public function test()
129129
{
130-
return new class($this) extends A
130+
return new class($this) extends \Humbug\Foo\A
131131
{
132132
const A = 'B';
133133
};
@@ -176,7 +176,7 @@ class A
176176
{
177177
public function test()
178178
{
179-
return new class($this) extends A
179+
return new class($this) extends \A
180180
{
181181
const A = 'B';
182182
};
@@ -190,7 +190,7 @@ public function test()
190190
{
191191
}
192192
};
193-
new class extends A implements B, C
193+
new class extends \Humbug\Foo\A implements \Humbug\Foo\B, \Humbug\Foo\C
194194
{
195195
};
196196
}
@@ -199,7 +199,7 @@ public function test()
199199
{
200200
public $foo;
201201
};
202-
new class($a, $b) extends A
202+
new class($a, $b) extends \Humbug\Bar\A
203203
{
204204
use T;
205205
};

specs/class/conditional.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,30 @@ class A
6060
PHP
6161
,
6262

63+
'Declaration of a whitelisted namespaced class: prefix each namespace, too dynamic to account for.' => [
64+
'whitelist' => ['Foo\A'],
65+
'payload' => <<<'PHP'
66+
<?php
67+
68+
namespace Foo;
69+
70+
if (true) {
71+
class A {}
72+
}
73+
----
74+
<?php
75+
76+
namespace Humbug\Foo;
77+
78+
if (true) {
79+
class A
80+
{
81+
}
82+
}
83+
84+
PHP
85+
],
86+
6387
'Multiple declarations in different namespaces: prefix each namespace.' => <<<'PHP'
6488
<?php
6589

specs/class/final.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,26 @@ final class A
5252
PHP
5353
,
5454

55+
'Declaration of a namespaced whitelisted final class: do not prefix the namespace.' => [
56+
'whitelist' => ['Foo\A'],
57+
'payload' => <<<'PHP'
58+
<?php
59+
60+
namespace Foo;
61+
62+
final class A {}
63+
----
64+
<?php
65+
66+
namespace Foo;
67+
68+
final class A
69+
{
70+
}
71+
72+
PHP
73+
],
74+
5575
'Multiple declarations in different namespaces: prefix each namespace.' => <<<'PHP'
5676
<?php
5777

specs/class/interface.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ public function a();
5959
PHP
6060
,
6161

62+
'Declaration of a whitelisted namespaced interface: do not prefix the namespace.' => [
63+
'whitelist' => ['Foo\A'],
64+
'payload' => <<<'PHP'
65+
<?php
66+
67+
namespace Foo;
68+
69+
interface A extends C, D
70+
{
71+
public function a();
72+
}
73+
----
74+
<?php
75+
76+
namespace Foo;
77+
78+
interface A extends C, D
79+
{
80+
public function a();
81+
}
82+
83+
PHP
84+
],
85+
6286
'Multiple declarations in different namespaces: prefix each namespace.' => <<<'PHP'
6387
<?php
6488

specs/class/regular-extend.php

Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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' => 'Class declaration with an extend',
18+
// Default values. If not specified will be the one used
19+
'prefix' => 'Humbug',
20+
'whitelist' => [],
21+
],
22+
23+
'Declaration in the global namespace: do not do anything.' => <<<'PHP'
24+
<?php
25+
26+
class A {
27+
public function a() {}
28+
}
29+
30+
class B extends A {
31+
}
32+
----
33+
<?php
34+
35+
class A
36+
{
37+
public function a()
38+
{
39+
}
40+
}
41+
class B extends \A
42+
{
43+
}
44+
45+
PHP
46+
,
47+
48+
'Declaration in a namespace: prefix the namespace.' => <<<'PHP'
49+
<?php
50+
51+
namespace Foo;
52+
53+
class A {
54+
public function a() {}
55+
}
56+
57+
class B extends A {
58+
}
59+
----
60+
<?php
61+
62+
namespace Humbug\Foo;
63+
64+
class A
65+
{
66+
public function a()
67+
{
68+
}
69+
}
70+
class B extends \Humbug\Foo\A
71+
{
72+
}
73+
74+
PHP
75+
,
76+
77+
'Declaration of a namespaced whitelisted class: do not prefix the namespace.' => [
78+
'whitelist' => ['Foo\B'],
79+
'payload' => <<<'PHP'
80+
<?php
81+
82+
namespace Foo;
83+
84+
class A {
85+
public function a() {}
86+
}
87+
88+
class B extends A {
89+
}
90+
----
91+
<?php
92+
93+
namespace Humbug\Foo;
94+
95+
class A
96+
{
97+
public function a()
98+
{
99+
}
100+
}
101+
class B extends \Humbug\Foo\A
102+
{
103+
}
104+
105+
PHP
106+
],
107+
108+
'Declaration in a different namespace imported via a use statement: prefix the namespace.' => <<<'PHP'
109+
<?php
110+
111+
namespace Foo;
112+
113+
class A {
114+
public function a() {}
115+
}
116+
117+
namespace Bar;
118+
119+
use Foo\A;
120+
121+
class B extends A {
122+
}
123+
----
124+
<?php
125+
126+
namespace Humbug\Foo;
127+
128+
class A
129+
{
130+
public function a()
131+
{
132+
}
133+
}
134+
namespace Humbug\Bar;
135+
136+
use Humbug\Foo\A;
137+
class B extends \Humbug\Foo\A
138+
{
139+
}
140+
141+
PHP
142+
,
143+
144+
'Declaration in a different namespace imported via a FQ call: prefix the namespace.' => <<<'PHP'
145+
<?php
146+
147+
namespace Foo;
148+
149+
class A {
150+
public function a() {}
151+
}
152+
153+
namespace Bar;
154+
155+
class B extends \Foo\A {
156+
}
157+
----
158+
<?php
159+
160+
namespace Humbug\Foo;
161+
162+
class A
163+
{
164+
public function a()
165+
{
166+
}
167+
}
168+
namespace Humbug\Bar;
169+
170+
class B extends \Humbug\Foo\A
171+
{
172+
}
173+
174+
PHP
175+
,
176+
];

0 commit comments

Comments
 (0)