Skip to content

Commit d56d480

Browse files
authored
Allow to whitelist constants from global namespace (#218)
Closes #175 BC Break: constants from the global namespace are now whitelisted by default
1 parent 89ec5a1 commit d56d480

File tree

137 files changed

+949
-112
lines changed

Some content is hidden

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

137 files changed

+949
-112
lines changed

README.md

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,10 @@ potentially very difficult to debug due to dissimilar or unsupported package ver
3838
- [Finders and paths](#finders-and-paths)
3939
- [Patchers](#patchers)
4040
- [Whitelist][whitelist]
41-
- [Class & Constant Whitelisting](#class-constant-whitelisting)
42-
- [Namespace Whitelisting](#namespace-whitelisting)
43-
- [Building A Scoped PHAR](#building-a-scoped-phar)
41+
- [Constants from the global namespace whitelisting](#constants-from-the-global-namespace-whitelisting)
42+
- [Classes & Constants whitelisting](#classes-constants-whitelisting)
43+
- [Namespaces whitelisting](#namespaces-whitelisting)
44+
- [Building a scoped PHAR](#building-a-scoped-phar)
4445
- [With Box](#with-box)
4546
- [Step 1: Configure build location and prep vendors](#step-1-configure-build-location-and-prep-vendors)
4647
- [Step 2: Run PHP-Scoper](#step-2-run-php-scoper)
@@ -125,10 +126,11 @@ with a `--config` option.
125126
use Isolated\Symfony\Component\Finder\Finder;
126127

127128
return [
128-
'prefix' => null,
129-
'finders' => [],
130-
'patchers' => [],
131-
'whitelist' => [],
129+
'prefix' => null, // string|null
130+
'finders' => [], // Finder[]
131+
'patchers' => [], // callable[]
132+
'whitelist' => [], // string[]
133+
'whitelist-global-constants' => true, // bool
132134
];
133135
```
134136

@@ -261,7 +263,24 @@ a PHPUnit PHAR with isolated code, you still want the PHAR to be able to
261263
understand the `PHPUnit\Framework\TestCase` class.
262264

263265

264-
### Class & Constant whitelisting
266+
### Constants from the global namespace whitelisting
267+
268+
By default, PHP-Scoper will not prefix the user defined constants belonging to
269+
the global namespace. You can however change that setting for them to be
270+
prefixed as usual unless explicitely whitelisted:
271+
272+
```php
273+
<?php declare(strict_types=1);
274+
275+
// scoper.inc.php
276+
277+
return [
278+
'whitelist-global-constants' => false,
279+
];
280+
```
281+
282+
283+
### Classes & Constants whitelisting
265284

266285
You can whitelist classes, interfaces and constants like so like so:
267286

@@ -282,23 +301,27 @@ This will _not_ work on traits or functions.
282301

283302
The class aliasing mechanism is done like follows:
284303
- Prefix the class or interface as usual
285-
- Append a `class_alias()` statement at the end of the class/interface declaration to link the prefixed symbol to the
286-
non prefixed one
287-
- Append a `class_exists()` statement right after the autoloader is registered to trigger the loading of the method
288-
which will ensure the `class_alias()` statement is executed
289-
290-
It is done this way to ensure prefixed and whitelisted classes can co-exist together without breaking the autoloading.
291-
The `class_exists()` statements are dumped in `vendor/scoper-autoload.php`, do not forget to include this file in favour
292-
of `vendor/autoload.php`. This part is however sorted out by [Box][box] if you are using it with the
293-
[`PhpScoper` compactor][php-scoper-integration].
294-
295-
The constant aliasing mechanism is done by transforming the constant declaration into a `define()` statement when this
296-
is not already the case. Note that there is a difference here since `define()` defines a constant at runtime whereas
297-
`const` defines it at compile time. You have a more details post regarding the differences
304+
- Append a `class_alias()` statement at the end of the class/interface
305+
declaration to link the prefixed symbol to the non prefixed one
306+
- Append a `class_exists()` statement right after the autoloader is
307+
registered to trigger the loading of the method which will ensure the
308+
`class_alias()` statement is executed
309+
310+
It is done this way to ensure prefixed and whitelisted classes can co-exist
311+
together without breaking the autoloading. The `class_exists()` statements are
312+
dumped in `vendor/scoper-autoload.php`, do not forget to include this file in
313+
favour of `vendor/autoload.php`. This part is however sorted out by [Box][box]
314+
if you are using it with the [`PhpScoper` compactor][php-scoper-integration].
315+
316+
The constant aliasing mechanism is done by transforming the constant
317+
declaration into a `define()` statement when this is not already the case.
318+
Note that there is a difference here since `define()` defines a constant at
319+
runtime whereas `const` defines it at compile time. You have a more details
320+
post regarding the differences
298321
[here](https://stackoverflow.com/a/3193704/3902761)
299322

300323

301-
### Namespace whitelisting
324+
### Namespaces whitelisting
302325

303326
If you want to be more generic and whitelist a whole namespace, you can
304327
do it so like this:
@@ -331,7 +354,7 @@ return [
331354
```
332355

333356

334-
## Building A Scoped PHAR
357+
## Building a Scoped PHAR
335358

336359
### With Box
337360

specs/binary/simple.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// Default values. If not specified will be the one used
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
21+
'whitelist-global-constants' => true,
2122
],
2223

2324
'some statements made directly in the global namespace: wrap them in a namespace statement' => <<<'PHP'

specs/class-FQ.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// Default values. If not specified will be the one used
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
21+
'whitelist-global-constants' => true,
2122
],
2223

2324
[

specs/class-const/global-scope-single-level-with-single-level-use-statement-and-alias.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// Default values. If not specified will be the one used
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
21+
'whitelist-global-constants' => true,
2122
],
2223

2324
[

specs/class-const/global-scope-single-level-with-single-level-use-statement.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// Default values. If not specified will be the one used
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
21+
'whitelist-global-constants' => true,
2122
],
2223

2324
[

specs/class-const/global-scope-single-level.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// Default values. If not specified will be the one used
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
21+
'whitelist-global-constants' => true,
2122
],
2223

2324
[

specs/class-const/global-scope-two-level-with-single-level-use-and-alias.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// Default values. If not specified will be the one used
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
21+
'whitelist-global-constants' => true,
2122
],
2223

2324
[

specs/class-const/global-scope-two-level-with-single-level-use.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// Default values. If not specified will be the one used
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
21+
'whitelist-global-constants' => true,
2122
],
2223

2324
[

specs/class-const/global-scope-two-level.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// Default values. If not specified will be the one used
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
21+
'whitelist-global-constants' => true,
2122
],
2223

2324
[

specs/class-const/namespace-scope-single-level-with-single-level-use-statement-and-alias.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
// Default values. If not specified will be the one used
1919
'prefix' => 'Humbug',
2020
'whitelist' => [],
21+
'whitelist-global-constants' => true,
2122
],
2223

2324
[

0 commit comments

Comments
 (0)