Skip to content

Commit

Permalink
Whitelist global functions by default (#231)
Browse files Browse the repository at this point in the history
Closes #226
  • Loading branch information
theofidry authored Jul 1, 2018
1 parent c4a7db1 commit 8049ea6
Show file tree
Hide file tree
Showing 147 changed files with 878 additions and 73 deletions.
16 changes: 16 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,15 @@ e2e_023: bin/php-scoper.phar fixtures/set023/vendor
php build/set023/main.php > build/set023/output
diff fixtures/set023/expected-output build/set023/output

.PHONY: e2e_024
e2e_024: ## Run end-to-end tests for the fixture set 024 — Whitelisting user functions registered in the global namespace
e2e_024: bin/php-scoper.phar fixtures/set024/vendor
$(PHPNOGC) $(PHPSCOPER) add-prefix --working-dir=fixtures/set024 --output-dir=../../build/set024 --force --no-interaction --stop-on-failure --no-config
composer --working-dir=build/set024 dump-autoload

php build/set024/main.php > build/set024/output
diff fixtures/set024/expected-output build/set024/output


.PHONY: tb
BLACKFIRE=blackfire
Expand Down Expand Up @@ -261,6 +270,10 @@ fixtures/set023/vendor: fixtures/set023/composer.lock
composer --working-dir=fixtures/set023 install
touch $@

fixtures/set024/vendor: fixtures/set024/composer.lock
composer --working-dir=fixtures/set024 install
touch $@

composer.lock: composer.json
@echo composer.lock is not up to date.

Expand Down Expand Up @@ -294,6 +307,9 @@ fixtures/set021-composer/composer.lock: fixtures/set021-composer/composer.json
fixtures/set023/composer.lock: fixtures/set023/composer.json
@echo fixtures/set023/composer.lock is not up to date.

fixtures/set024/composer.lock: fixtures/set024/composer.json
@echo fixtures/set024/composer.lock is not up to date.

bin/php-scoper.phar: bin/php-scoper src vendor scoper.inc.php box.json
$(BOX) compile
touch $@
Expand Down
63 changes: 63 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ potentially very difficult to debug due to dissimilar or unsupported package ver
- [Whitelist][whitelist]
- [Constants from the global namespace whitelisting](#constants-from-the-global-namespace-whitelisting)
- [Classes & Constants whitelisting](#classes--constants-whitelisting)
- [Global user functions](#global-user-functions)
- [Namespaces whitelisting](#namespaces-whitelisting)
- [Building a scoped PHAR](#building-a-scoped-phar)
- [With Box](#with-box)
Expand Down Expand Up @@ -132,6 +133,7 @@ return [
'patchers' => [], // callable[]
'whitelist' => [], // string[]
'whitelist-global-constants' => true, // bool
'whitelist-global-functions' => true, // bool
];
```

Expand Down Expand Up @@ -322,6 +324,67 @@ post regarding the differences
[here](https://stackoverflow.com/a/3193704/3902761)


### Global user functions

By default, functions declared by users in the global namespace are whitelisted:

```php
<?php declare(strict_types=1);

// scoper.inc.php

use Isolated\Symfony\Component\Finder\Finder;

return [
'whitelist-global-functions' => true,
];
```

So when you declare a function like so:

```php
<?php

// No namespace: this is the global namespace

if (!function_exists('dd')) {
function dd() {}
}
```

The file will be scoped as usual in order to avoid any autoloading issue:

```
<?php
namespace PhpScoperPrefix;
if (!function_exists('PhpScoperPrefix\dd')) {
function dd() {...}
}
```

And the following function which will serve as an alias will be
declared in the `scoper-autoload.php` file:


```php
<?php

// scoper-autoload.php @generated by PhpScoper

$loader = require_once __DIR__.'/autoload.php';

if (!function_exists('dd')) {
function dd() {
return \PhpScoperPrefix\dd(func_get_args());
}
}

return $loader;
```


### Namespaces whitelisting

If you want to be more generic and whitelist a whole namespace, you can
Expand Down
5 changes: 5 additions & 0 deletions fixtures/set024/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"require": {
"symfony/var-dumper": "^4.1"
}
}
207 changes: 207 additions & 0 deletions fixtures/set024/composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions fixtures/set024/expected-output
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
array:1 [
0 => "yo"
]
15 changes: 15 additions & 0 deletions fixtures/set024/main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Acme;

use function file_exists;

if (file_exists($autoload = __DIR__ . '/vendor/scoper-autoload.php')) {
require_once $autoload;
} else {
require_once __DIR__ . '/vendor/autoload.php';
}

dump('yo');
1 change: 1 addition & 0 deletions specs/binary/simple.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-functions' => true,
],

'some statements made directly in the global namespace: wrap them in a namespace statement' => <<<'PHP'
Expand Down
1 change: 1 addition & 0 deletions specs/class-FQ.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-functions' => true,
],

[
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-functions' => true,
],

[
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-functions' => true,
],

[
Expand Down
1 change: 1 addition & 0 deletions specs/class-const/global-scope-single-level.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-functions' => true,
],

[
Expand Down
Loading

0 comments on commit 8049ea6

Please sign in to comment.