Skip to content

Commit 356be9a

Browse files
authored
Whitelist classes belonging to the global namespace by default (#252)
1 parent 2008685 commit 356be9a

File tree

128 files changed

+747
-160
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

128 files changed

+747
-160
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ return [
137137
'patchers' => [], // callable[]
138138
'whitelist' => [], // string[]
139139
'whitelist-global-constants' => true, // bool
140+
'whitelist-global-classes' => true, // bool
140141
'whitelist-global-functions' => true, // bool
141142
];
142143
```
@@ -270,11 +271,11 @@ a PHPUnit PHAR with isolated code, you still want the PHAR to be able to
270271
understand the `PHPUnit\Framework\TestCase` class.
271272

272273

273-
### Constants & functions from the global namespace
274+
### Constants & Classes & functions from the global namespace
274275

275-
By default, PHP-Scoper will not prefix the user defined constants and functions
276-
belonging to the global namespace. You can however change that setting for them
277-
to be prefixed as usual unless explicitly whitelisted:
276+
By default, PHP-Scoper will prefix the user defined constants, classes and
277+
functions belonging to the global namespace. You can however change that
278+
setting for them to be prefixed as usual unless explicitly whitelisted:
278279

279280
```php
280281
<?php declare(strict_types=1);
@@ -283,6 +284,7 @@ to be prefixed as usual unless explicitly whitelisted:
283284

284285
return [
285286
'whitelist-global-constants' => false,
287+
'whitelist-global-classes' => false,
286288
'whitelist-global-functions' => false,
287289
];
288290
```

specs/class/abstract.php renamed to class/abstract.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
22+
'whitelist-global-classes' => false,
2223
'whitelist-global-functions' => true,
2324
'registered-classes' => [],
2425
'registered-functions' => [],
@@ -47,6 +48,35 @@ public abstract function b();
4748
PHP
4849
,
4950

51+
'Declaration in the global namespace with global classes whitelisted' => [
52+
'whitelist-global-classes' => true,
53+
'registered-classes' => [
54+
['A', 'Humbug\A'],
55+
],
56+
'payload' => <<<'PHP'
57+
<?php
58+
59+
abstract class A {
60+
public function a() {}
61+
abstract public function b();
62+
}
63+
----
64+
<?php
65+
66+
namespace Humbug;
67+
68+
abstract class A
69+
{
70+
public function a()
71+
{
72+
}
73+
public abstract function b();
74+
}
75+
76+
PHP
77+
,
78+
],
79+
5080
'Declaration in the global namespace with the global namespace which is namespaced whitelisted' => [
5181
'whitelist' => ['\*'],
5282
'payload' => <<<'PHP'
@@ -154,6 +184,34 @@ public abstract function b();
154184
PHP
155185
,
156186

187+
'Declaration in a namespace with global classes whitelisted' => [
188+
'whitelist-global-classes' => true,
189+
'payload' => <<<'PHP'
190+
<?php
191+
192+
namespace Foo;
193+
194+
abstract class A {
195+
public function a() {}
196+
abstract public function b();
197+
}
198+
----
199+
<?php
200+
201+
namespace Humbug\Foo;
202+
203+
abstract class A
204+
{
205+
public function a()
206+
{
207+
}
208+
public abstract function b();
209+
}
210+
211+
PHP
212+
,
213+
],
214+
157215
'Declaration in a whitelisted namespace' => [
158216
'whitelist' => ['Foo\*'],
159217
'payload' => <<<'PHP'

specs/class/anonymous.php renamed to class/anonymous.php

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
22+
'whitelist-global-classes' => false,
2223
'whitelist-global-functions' => true,
2324
'registered-classes' => [],
2425
'registered-functions' => [],
@@ -90,6 +91,80 @@ public function test()
9091
PHP
9192
,
9293

94+
'Declaration in the global namespace with global classes whitelisted' => [
95+
'whitelist-global-classes' => true,
96+
'registered-classes' => [
97+
['A', 'Humbug\A'],
98+
['B', 'Humbug\B'],
99+
['C', 'Humbug\C'],
100+
],
101+
'payload' => <<<'PHP'
102+
<?php
103+
104+
interface B {}
105+
interface C {}
106+
107+
new class {
108+
public function test() {}
109+
};
110+
new class extends A implements B, C, Iterator {};
111+
new class() {
112+
public $foo;
113+
};
114+
new class($a, $b) extends A {
115+
use T;
116+
};
117+
118+
class A {
119+
public function test() {
120+
return new class($this) extends A {
121+
const A = 'B';
122+
};
123+
}
124+
}
125+
----
126+
<?php
127+
128+
namespace Humbug;
129+
130+
interface B
131+
{
132+
}
133+
interface C
134+
{
135+
}
136+
new class
137+
{
138+
public function test()
139+
{
140+
}
141+
};
142+
new class extends \Humbug\A implements \Humbug\B, \Humbug\C, \Iterator
143+
{
144+
};
145+
new class
146+
{
147+
public $foo;
148+
};
149+
new class($a, $b) extends \Humbug\A
150+
{
151+
use T;
152+
};
153+
class A
154+
{
155+
public function test()
156+
{
157+
return new class($this) extends \Humbug\A
158+
{
159+
const A = 'B';
160+
};
161+
}
162+
}
163+
164+
PHP
165+
,
166+
],
167+
93168
'Declaration in the global namespace which is whitelisted' => [
94169
'whitelist' => ['\*'],
95170
'payload' => <<<'PHP'

specs/class/conditional.php renamed to class/conditional.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
22+
'whitelist-global-classes' => false,
2223
'whitelist-global-functions' => true,
2324
'registered-classes' => [],
2425
'registered-functions' => [],

specs/class/final.php renamed to class/final.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
22+
'whitelist-global-classes' => false,
2223
'whitelist-global-functions' => true,
2324
'registered-classes' => [],
2425
'registered-functions' => [],
@@ -40,6 +41,27 @@ final class A
4041
PHP
4142
,
4243

44+
'Declaration in the global namespace with global classes whitelisted' => [
45+
'whitelist-global-classes' => true,
46+
'registered-classes' => [
47+
['A', 'Humbug\A'],
48+
],
49+
'payload' => <<<'PHP'
50+
<?php
51+
52+
final class A {}
53+
----
54+
<?php
55+
56+
namespace Humbug;
57+
58+
final class A
59+
{
60+
}
61+
62+
PHP
63+
],
64+
4365
'Declaration in a namespace' => <<<'PHP'
4466
<?php
4567
@@ -58,6 +80,26 @@ final class A
5880
PHP
5981
,
6082

83+
'Declaration in a namespace with global classes whitelisted' => [
84+
'whitelist-global-classes' => true,
85+
'payload' => <<<'PHP'
86+
<?php
87+
88+
namespace Foo;
89+
90+
final class A {}
91+
----
92+
<?php
93+
94+
namespace Humbug\Foo;
95+
96+
final class A
97+
{
98+
}
99+
100+
PHP
101+
],
102+
61103
'Declaration of a whitelisted final class' => [
62104
'whitelist' => ['Foo\A'],
63105
'registered-classes' => [

specs/class/interface.php renamed to class/interface.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
22+
'whitelist-global-classes' => false,
2223
'whitelist-global-functions' => true,
2324
'registered-classes' => [],
2425
'registered-functions' => [],
@@ -52,6 +53,41 @@ public function a();
5253
PHP
5354
,
5455

56+
'Declaration in the global namespace with global classes whitelisted' => [
57+
'whitelist-global-classes' => true,
58+
'registered-classes' => [
59+
['A', 'Humbug\A'],
60+
['C', 'Humbug\C'],
61+
['D', 'Humbug\D'],
62+
],
63+
'payload' => <<<'PHP'
64+
<?php
65+
66+
class C {}
67+
class D {}
68+
69+
interface A extends C, D, Iterator {
70+
public function a();
71+
}
72+
----
73+
<?php
74+
75+
namespace Humbug;
76+
77+
class C
78+
{
79+
}
80+
class D
81+
{
82+
}
83+
interface A extends \Humbug\C, \Humbug\D, \Iterator
84+
{
85+
public function a();
86+
}
87+
88+
PHP
89+
],
90+
5591
'Declaration in a namespace' => <<<'PHP'
5692
<?php
5793
@@ -86,6 +122,42 @@ public function a();
86122
PHP
87123
,
88124

125+
'Declaration in a namespace with global classes whitelisted' => [
126+
'whitelist-global-classes' => true,
127+
'payload' => <<<'PHP'
128+
<?php
129+
130+
namespace Foo;
131+
132+
use Iterator;
133+
134+
class C {}
135+
class D {}
136+
137+
interface A extends C, D, Iterator
138+
{
139+
public function a();
140+
}
141+
----
142+
<?php
143+
144+
namespace Humbug\Foo;
145+
146+
use Iterator;
147+
class C
148+
{
149+
}
150+
class D
151+
{
152+
}
153+
interface A extends \Humbug\Foo\C, \Humbug\Foo\D, \Iterator
154+
{
155+
public function a();
156+
}
157+
158+
PHP
159+
],
160+
89161
'Declaration of a whitelisted interface' => [
90162
'whitelist' => ['Foo\A'],
91163
'registered-classes' => [

specs/class/regular-extend.php renamed to class/regular-extend.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
2121
'whitelist-global-constants' => true,
22+
'whitelist-global-classes' => false,
2223
'whitelist-global-functions' => true,
2324
'registered-classes' => [],
2425
'registered-functions' => [],

0 commit comments

Comments
 (0)