Permalink
Browse files

Whitelist classes belonging to the global namespace by default (#252)

  • Loading branch information...
theofidry committed Aug 3, 2018
1 parent 2008685 commit 356be9adff7e000868df510c723fea850747fc86
Showing with 747 additions and 160 deletions.
  1. +6 βˆ’4 README.md
  2. +58 βˆ’0 {specs β†’ }/class/abstract.php
  3. +75 βˆ’0 {specs β†’ }/class/anonymous.php
  4. +1 βˆ’0 {specs β†’ }/class/conditional.php
  5. +42 βˆ’0 {specs β†’ }/class/final.php
  6. +72 βˆ’0 {specs β†’ }/class/interface.php
  7. +1 βˆ’0 {specs β†’ }/class/regular-extend.php
  8. +52 βˆ’0 {specs β†’ }/class/regular.php
  9. +57 βˆ’0 {specs β†’ }/class/trait.php
  10. +1 βˆ’0 specs/binary/simple.php
  11. +1 βˆ’0 specs/class-FQ.php
  12. +1 βˆ’0 specs/class-const/global-scope-single-level-with-single-level-use-statement-and-alias.php
  13. +1 βˆ’0 specs/class-const/global-scope-single-level-with-single-level-use-statement.php
  14. +1 βˆ’0 specs/class-const/global-scope-single-level.php
  15. +1 βˆ’0 specs/class-const/global-scope-two-level-with-single-level-use-and-alias.php
  16. +1 βˆ’0 specs/class-const/global-scope-two-level-with-single-level-use.php
  17. +1 βˆ’0 specs/class-const/global-scope-two-level.php
  18. +1 βˆ’0 specs/class-const/namespace-scope-single-level-with-single-level-use-statement-and-alias.php
  19. +1 βˆ’0 specs/class-const/namespace-scope-single-level.php
  20. +1 βˆ’0 specs/class-const/namespace-scope-two-level-with-single-level-use-and-alias.php
  21. +1 βˆ’0 specs/class-const/namespace-scope-two-level-with-single-level-use.php
  22. +1 βˆ’0 specs/class-const/namespace-scope-two-level.php
  23. +1 βˆ’0 specs/class-const/namespace-single-part-with-single-level-use-statement.php
  24. +1 βˆ’0 specs/class-static-prop/global-scope-single-level-with-single-level-use-statement-and-alias.php
  25. +1 βˆ’0 specs/class-static-prop/global-scope-single-level-with-single-level-use-statement.php
  26. +1 βˆ’0 specs/class-static-prop/global-scope-single-level.php
  27. +1 βˆ’0 specs/class-static-prop/global-scope-two-level-with-single-level-use-and-alias.php
  28. +1 βˆ’0 specs/class-static-prop/global-scope-two-level-with-single-level-use.php
  29. +1 βˆ’0 specs/class-static-prop/global-scope-two-level.php
  30. +1 βˆ’0 specs/class-static-prop/namespace-scope-single-level-with-single-level-use-statement-and-alias.php
  31. +1 βˆ’0 specs/class-static-prop/namespace-scope-single-level.php
  32. +1 βˆ’0 specs/class-static-prop/namespace-scope-two-level-with-single-level-use-and-alias.php
  33. +1 βˆ’0 specs/class-static-prop/namespace-scope-two-level-with-single-level-use.php
  34. +1 βˆ’0 specs/class-static-prop/namespace-scope-two-level.php
  35. +1 βˆ’0 specs/class-static-prop/namespace-single-part-with-single-level-use-statement.php
  36. +1 βˆ’0 specs/const/const-declaration-with-global-whitelisting.php
  37. +1 βˆ’0 specs/const/const-declaration.php
  38. +1 βˆ’0 specs/const/global-scope-global-with-global-whitelisting.php
  39. +1 βˆ’0 specs/const/global-scope-global-with-single-level-use-statement-and-alias.php
  40. +1 βˆ’0 specs/const/global-scope-global-with-single-level-use-statement-with-global-whitelisting.php
  41. +1 βˆ’0 specs/const/global-scope-global-with-single-level-use-statement.php
  42. +1 βˆ’0 specs/const/global-scope-global.php
  43. +1 βˆ’0 specs/const/global-scope-single-part-namespaced-with-single-level-use-alias.php
  44. +1 βˆ’0 specs/const/global-scope-single-part-namespaced-with-single-level-use.php
  45. +1 βˆ’0 specs/const/global-scope-single-part-namespaced.php
  46. +1 βˆ’0 specs/const/global-scope-two-parts-namespaced-with-single-level-use-and-alias.php
  47. +1 βˆ’0 specs/const/global-scope-two-parts-namespaced-with-single-level-use.php
  48. +1 βˆ’0 specs/const/global-scope-two-parts-namespaced.php
  49. +1 βˆ’0 specs/const/namespace-global-with-global-whitelisting.php
  50. +1 βˆ’0 specs/const/namespace-global-with-single-level-use-statement-and-alias.php
  51. +1 βˆ’0 specs/const/namespace-global-with-single-level-use-statement.php
  52. +1 βˆ’0 specs/const/namespace-global.php
  53. +1 βˆ’0 specs/const/namespace-single-part-namespaced.php
  54. +1 βˆ’0 specs/const/native-const-with-global-whitelisting.php
  55. +1 βˆ’0 specs/const/native-const.php
  56. +1 βˆ’0 specs/exp/cast.php
  57. +1 βˆ’0 specs/exp/catch.php
  58. +1 βˆ’0 specs/exp/instanceof.php
  59. +1 βˆ’0 specs/func-declaration/global.php
  60. +1 βˆ’0 specs/func-declaration/method.php
  61. +1 βˆ’0 specs/func-declaration/namespace.php
  62. +1 βˆ’0 specs/function/global-scope-global-func-with-single-level-use-statement-and-alias.php
  63. +1 βˆ’0 specs/function/global-scope-global-func-with-single-level-use-statement.php
  64. +1 βˆ’0 specs/function/global-scope-global-func.php
  65. +1 βˆ’0 specs/function/global-scope-single-part-namespaced-func-with-single-level-use-and-alias.php
  66. +1 βˆ’0 specs/function/global-scope-single-part-namespaced-func-with-single-level-use.php
  67. +1 βˆ’0 specs/function/global-scope-single-part-namespaced-func.php
  68. +1 βˆ’0 specs/function/namespace-global-func-with-single-level-use-statement-and-alias.php
  69. +1 βˆ’0 specs/function/namespace-global-func-with-single-level-use-statement.php
  70. +1 βˆ’0 specs/function/namespace-global-func.php
  71. +1 βˆ’0 specs/function/namespace-global-scope-func.php
  72. +1 βˆ’0 specs/function/namespace-single-part-namespaced-func.php
  73. +1 βˆ’0 specs/function/native-func.php
  74. +1 βˆ’0 specs/misc.php
  75. +1 βˆ’0 specs/namespace/after-hashbang.php
  76. +1 βˆ’0 specs/namespace/braces.php
  77. +1 βˆ’0 specs/namespace/creation-for-whitelist.php
  78. +1 βˆ’0 specs/namespace/no-braces.php
  79. +1 βˆ’0 specs/namespace/outside-statements.php
  80. +1 βˆ’0 specs/new/global-scope-single-part-with-single-level-use-statement-an-alias.php
  81. +1 βˆ’0 specs/new/global-scope-single-part-with-single-level-use-statement.php
  82. +1 βˆ’0 specs/new/global-scope-single-part.php
  83. +1 βˆ’0 specs/new/global-scope-two-parts-with-single-level-use-and-alias.php
  84. +1 βˆ’0 specs/new/global-scope-two-parts-with-single-level-use.php
  85. +1 βˆ’0 specs/new/global-scope-two-parts.php
  86. +1 βˆ’0 specs/new/namespace-single-part-with-single-level-use-statement.php
  87. +1 βˆ’0 specs/new/namespace-single-part.php
  88. +1 βˆ’0 specs/new/namespace-two-parts-with-single-level-use.php
  89. +1 βˆ’0 specs/new/namespace-two-parts-with-two-level-use.php
  90. +1 βˆ’0 specs/new/namespace-two-parts.php
  91. +1 βˆ’0 specs/special-keywords/self-static-parent-const.php
  92. +1 βˆ’0 specs/special-keywords/self-static-parent-method.php
  93. +1 βˆ’0 specs/special-keywords/self-static-parent-static-var.php
  94. +1 βˆ’0 specs/static-method/global-scope-single-part-with-single-level-use-statement-and-alias.php
  95. +1 βˆ’0 specs/static-method/global-scope-single-part-with-single-level-use-statement.php
  96. +1 βˆ’0 specs/static-method/global-scope-single-part.php
  97. +1 βˆ’0 specs/static-method/global-scope-two-parts-with-single-level-use-and-alias.php
  98. +1 βˆ’0 specs/static-method/global-scope-two-parts-with-single-level-use.php
  99. +1 βˆ’0 specs/static-method/global-scope-two-parts.php
  100. +1 βˆ’0 specs/static-method/namespace-single-part-with-single-level-use-statement.php
  101. +1 βˆ’0 specs/static-method/namespace-single-part.php
  102. +1 βˆ’0 specs/static-method/namespace-two-parts-with-single-level-use.php
  103. +1 βˆ’0 specs/static-method/namespace-two-parts-with-two-level-use.php
  104. +1 βˆ’0 specs/static-method/namespace-two-parts.php
  105. +1 βˆ’0 specs/use/use-class-alias.php
  106. +1 βˆ’0 specs/use/use-class-group.php
  107. +1 βˆ’0 specs/use/use-class.php
  108. +1 βˆ’0 specs/use/use-const-alias.php
  109. +1 βˆ’0 specs/use/use-const-group.php
  110. +1 βˆ’0 specs/use/use-const.php
  111. +1 βˆ’0 specs/use/use-func-alias.php
  112. +1 βˆ’0 specs/use/use-func-group.php
  113. +1 βˆ’0 specs/use/use-func.php
  114. +1 βˆ’0 specs/use/use-mix-group.php
  115. +1 βˆ’0 specs/whitelist-case-sensitiveness.php
  116. +20 βˆ’3 src/Configuration.php
  117. +12 βˆ’4 src/Whitelist.php
  118. +7 βˆ’2 src/scoper.inc.php.tpl
  119. +5 βˆ’5 tests/Autoload/ScoperAutoloadGeneratorTest.php
  120. +13 βˆ’13 tests/Console/Command/AddPrefixCommandTest.php
  121. +1 βˆ’1 tests/PhpParser/TraverserFactoryTest.php
  122. +2 βˆ’2 tests/Scoper/Composer/InstalledPackagesScoperTest.php
  123. +3 βˆ’3 tests/Scoper/Composer/JsonFileScoperTest.php
  124. +1 βˆ’1 tests/Scoper/NullScoperTest.php
  125. +1 βˆ’1 tests/Scoper/PatchScoperTest.php
  126. +3 βˆ’0 tests/Scoper/PhpScoperSpecTest.php
  127. +7 βˆ’7 tests/Scoper/PhpScoperTest.php
  128. +202 βˆ’114 tests/WhitelistTest.php
View
@@ -137,6 +137,7 @@ return [
'patchers' => [], // callable[]
'whitelist' => [], // string[]
'whitelist-global-constants' => true, // bool
'whitelist-global-classes' => true, // bool
'whitelist-global-functions' => true, // bool
];
```
@@ -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);
@@ -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,
];
```
@@ -19,6 +19,7 @@
'prefix' => 'Humbug',
'whitelist' => [],
'whitelist-global-constants' => true,
'whitelist-global-classes' => false,
'whitelist-global-functions' => true,
'registered-classes' => [],
'registered-functions' => [],
@@ -47,6 +48,35 @@ public function a()
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'
@@ -154,6 +184,34 @@ public function a()
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'
@@ -19,6 +19,7 @@
'prefix' => 'Humbug',
'whitelist' => [],
'whitelist-global-constants' => true,
'whitelist-global-classes' => false,
'whitelist-global-functions' => true,
'registered-classes' => [],
'registered-functions' => [],
@@ -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'
@@ -19,6 +19,7 @@
'prefix' => 'Humbug',
'whitelist' => [],
'whitelist-global-constants' => true,
'whitelist-global-classes' => false,
'whitelist-global-functions' => true,
'registered-classes' => [],
'registered-functions' => [],
@@ -19,6 +19,7 @@
'prefix' => 'Humbug',
'whitelist' => [],
'whitelist-global-constants' => true,
'whitelist-global-classes' => false,
'whitelist-global-functions' => true,
'registered-classes' => [],
'registered-functions' => [],
@@ -40,6 +41,27 @@
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
@@ -58,6 +80,26 @@
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' => [
@@ -19,6 +19,7 @@
'prefix' => 'Humbug',
'whitelist' => [],
'whitelist-global-constants' => true,
'whitelist-global-classes' => false,
'whitelist-global-functions' => true,
'registered-classes' => [],
'registered-functions' => [],
@@ -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
@@ -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' => [
@@ -19,6 +19,7 @@
'prefix' => 'Humbug',
'whitelist' => [],
'whitelist-global-constants' => true,
'whitelist-global-classes' => false,
'whitelist-global-functions' => true,
'registered-classes' => [],
'registered-functions' => [],
Oops, something went wrong.

0 comments on commit 356be9a

Please sign in to comment.