Skip to content

Commit

Permalink
Whitelist classes belonging to the global namespace by default (#252)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored Aug 3, 2018
1 parent 2008685 commit 356be9a
Show file tree
Hide file tree
Showing 128 changed files with 747 additions and 160 deletions.
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ return [
'patchers' => [], // callable[]
'whitelist' => [], // string[]
'whitelist-global-constants' => true, // bool
'whitelist-global-classes' => true, // bool
'whitelist-global-functions' => true, // bool
];
```
Expand Down Expand Up @@ -270,11 +271,11 @@ a PHPUnit PHAR with isolated code, you still want the PHAR to be able to
understand the `PHPUnit\Framework\TestCase` class.


### Constants & functions from the global namespace
### Constants & Classes & functions from the global namespace

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

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

return [
'whitelist-global-constants' => false,
'whitelist-global-classes' => false,
'whitelist-global-functions' => false,
];
```
Expand Down
58 changes: 58 additions & 0 deletions specs/class/abstract.php → class/abstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'prefix' => 'Humbug',
'whitelist' => [],
'whitelist-global-constants' => true,
'whitelist-global-classes' => false,
'whitelist-global-functions' => true,
'registered-classes' => [],
'registered-functions' => [],
Expand Down Expand Up @@ -47,6 +48,35 @@ public abstract function b();
PHP
,

'Declaration in the global namespace with global classes whitelisted' => [
'whitelist-global-classes' => true,
'registered-classes' => [
['A', 'Humbug\A'],
],
'payload' => <<<'PHP'
<?php
abstract class A {
public function a() {}
abstract public function b();
}
----
<?php
namespace Humbug;
abstract class A
{
public function a()
{
}
public abstract function b();
}

PHP
,
],

'Declaration in the global namespace with the global namespace which is namespaced whitelisted' => [
'whitelist' => ['\*'],
'payload' => <<<'PHP'
Expand Down Expand Up @@ -154,6 +184,34 @@ public abstract function b();
PHP
,

'Declaration in a namespace with global classes whitelisted' => [
'whitelist-global-classes' => true,
'payload' => <<<'PHP'
<?php
namespace Foo;
abstract class A {
public function a() {}
abstract public function b();
}
----
<?php
namespace Humbug\Foo;
abstract class A
{
public function a()
{
}
public abstract function b();
}

PHP
,
],

'Declaration in a whitelisted namespace' => [
'whitelist' => ['Foo\*'],
'payload' => <<<'PHP'
Expand Down
75 changes: 75 additions & 0 deletions specs/class/anonymous.php → class/anonymous.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'prefix' => 'Humbug',
'whitelist' => [],
'whitelist-global-constants' => true,
'whitelist-global-classes' => false,
'whitelist-global-functions' => true,
'registered-classes' => [],
'registered-functions' => [],
Expand Down Expand Up @@ -90,6 +91,80 @@ public function test()
PHP
,

'Declaration in the global namespace with global classes whitelisted' => [
'whitelist-global-classes' => true,
'registered-classes' => [
['A', 'Humbug\A'],
['B', 'Humbug\B'],
['C', 'Humbug\C'],
],
'payload' => <<<'PHP'
<?php
interface B {}
interface C {}
new class {
public function test() {}
};
new class extends A implements B, C, Iterator {};
new class() {
public $foo;
};
new class($a, $b) extends A {
use T;
};
class A {
public function test() {
return new class($this) extends A {
const A = 'B';
};
}
}
----
<?php
namespace Humbug;
interface B
{
}
interface C
{
}
new class
{
public function test()
{
}
};
new class extends \Humbug\A implements \Humbug\B, \Humbug\C, \Iterator
{
};
new class
{
public $foo;
};
new class($a, $b) extends \Humbug\A
{
use T;
};
class A
{
public function test()
{
return new class($this) extends \Humbug\A
{
const A = 'B';
};
}
}

PHP
,
],

'Declaration in the global namespace which is whitelisted' => [
'whitelist' => ['\*'],
'payload' => <<<'PHP'
Expand Down
1 change: 1 addition & 0 deletions specs/class/conditional.php → class/conditional.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'prefix' => 'Humbug',
'whitelist' => [],
'whitelist-global-constants' => true,
'whitelist-global-classes' => false,
'whitelist-global-functions' => true,
'registered-classes' => [],
'registered-functions' => [],
Expand Down
42 changes: 42 additions & 0 deletions specs/class/final.php → class/final.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'prefix' => 'Humbug',
'whitelist' => [],
'whitelist-global-constants' => true,
'whitelist-global-classes' => false,
'whitelist-global-functions' => true,
'registered-classes' => [],
'registered-functions' => [],
Expand All @@ -40,6 +41,27 @@ final class A
PHP
,

'Declaration in the global namespace with global classes whitelisted' => [
'whitelist-global-classes' => true,
'registered-classes' => [
['A', 'Humbug\A'],
],
'payload' => <<<'PHP'
<?php
final class A {}
----
<?php
namespace Humbug;
final class A
{
}

PHP
],

'Declaration in a namespace' => <<<'PHP'
<?php
Expand All @@ -58,6 +80,26 @@ final class A
PHP
,

'Declaration in a namespace with global classes whitelisted' => [
'whitelist-global-classes' => true,
'payload' => <<<'PHP'
<?php
namespace Foo;
final class A {}
----
<?php
namespace Humbug\Foo;
final class A
{
}

PHP
],

'Declaration of a whitelisted final class' => [
'whitelist' => ['Foo\A'],
'registered-classes' => [
Expand Down
72 changes: 72 additions & 0 deletions specs/class/interface.php → class/interface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'prefix' => 'Humbug',
'whitelist' => [],
'whitelist-global-constants' => true,
'whitelist-global-classes' => false,
'whitelist-global-functions' => true,
'registered-classes' => [],
'registered-functions' => [],
Expand Down Expand Up @@ -52,6 +53,41 @@ public function a();
PHP
,

'Declaration in the global namespace with global classes whitelisted' => [
'whitelist-global-classes' => true,
'registered-classes' => [
['A', 'Humbug\A'],
['C', 'Humbug\C'],
['D', 'Humbug\D'],
],
'payload' => <<<'PHP'
<?php
class C {}
class D {}
interface A extends C, D, Iterator {
public function a();
}
----
<?php
namespace Humbug;
class C
{
}
class D
{
}
interface A extends \Humbug\C, \Humbug\D, \Iterator
{
public function a();
}

PHP
],

'Declaration in a namespace' => <<<'PHP'
<?php
Expand Down Expand Up @@ -86,6 +122,42 @@ public function a();
PHP
,

'Declaration in a namespace with global classes whitelisted' => [
'whitelist-global-classes' => true,
'payload' => <<<'PHP'
<?php
namespace Foo;
use Iterator;
class C {}
class D {}
interface A extends C, D, Iterator
{
public function a();
}
----
<?php
namespace Humbug\Foo;
use Iterator;
class C
{
}
class D
{
}
interface A extends \Humbug\Foo\C, \Humbug\Foo\D, \Iterator
{
public function a();
}

PHP
],

'Declaration of a whitelisted interface' => [
'whitelist' => ['Foo\A'],
'registered-classes' => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
'prefix' => 'Humbug',
'whitelist' => [],
'whitelist-global-constants' => true,
'whitelist-global-classes' => false,
'whitelist-global-functions' => true,
'registered-classes' => [],
'registered-functions' => [],
Expand Down
Loading

0 comments on commit 356be9a

Please sign in to comment.