diff --git a/Makefile b/Makefile index 14e0671f..b3b7837c 100644 --- a/Makefile +++ b/Makefile @@ -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 @@ -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. @@ -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 $@ diff --git a/README.md b/README.md index b6c34c84..212cd3ac 100644 --- a/README.md +++ b/README.md @@ -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) @@ -132,6 +133,7 @@ return [ 'patchers' => [], // callable[] 'whitelist' => [], // string[] 'whitelist-global-constants' => true, // bool + 'whitelist-global-functions' => true, // bool ]; ``` @@ -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 + true, +]; +``` + +So when you declare a function like so: + +```php +=5.3.3" + }, + "suggest": { + "ext-mbstring": "For best performance" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.8-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Mbstring\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill for the Mbstring extension", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "mbstring", + "polyfill", + "portable", + "shim" + ], + "time": "2018-04-26T10:06:28+00:00" + }, + { + "name": "symfony/polyfill-php72", + "version": "v1.8.0", + "source": { + "type": "git", + "url": "https://github.com/symfony/polyfill-php72.git", + "reference": "a4576e282d782ad82397f3e4ec1df8e0f0cafb46" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/polyfill-php72/zipball/a4576e282d782ad82397f3e4ec1df8e0f0cafb46", + "reference": "a4576e282d782ad82397f3e4ec1df8e0f0cafb46", + "shasum": "" + }, + "require": { + "php": ">=5.3.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "1.8-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Polyfill\\Php72\\": "" + }, + "files": [ + "bootstrap.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions", + "homepage": "https://symfony.com", + "keywords": [ + "compatibility", + "polyfill", + "portable", + "shim" + ], + "time": "2018-04-26T10:06:28+00:00" + }, + { + "name": "symfony/var-dumper", + "version": "v4.1.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/var-dumper.git", + "reference": "b2eebaec085d1f2cafbad7644733d494a3bbbc9b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/var-dumper/zipball/b2eebaec085d1f2cafbad7644733d494a3bbbc9b", + "reference": "b2eebaec085d1f2cafbad7644733d494a3bbbc9b", + "shasum": "" + }, + "require": { + "php": "^7.1.3", + "symfony/polyfill-mbstring": "~1.0", + "symfony/polyfill-php72": "~1.5" + }, + "conflict": { + "phpunit/phpunit": "<4.8.35|<5.4.3,>=5.0", + "symfony/console": "<3.4" + }, + "require-dev": { + "ext-iconv": "*", + "symfony/process": "~3.4|~4.0", + "twig/twig": "~1.34|~2.4" + }, + "suggest": { + "ext-iconv": "To convert non-UTF-8 strings to UTF-8 (or symfony/polyfill-iconv in case ext-iconv cannot be used).", + "ext-intl": "To show region name in time zone dump", + "symfony/console": "To use the ServerDumpCommand and/or the bin/var-dump-server script" + }, + "bin": [ + "Resources/bin/var-dump-server" + ], + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "4.1-dev" + } + }, + "autoload": { + "files": [ + "Resources/functions/dump.php" + ], + "psr-4": { + "Symfony\\Component\\VarDumper\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Nicolas Grekas", + "email": "p@tchwork.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony mechanism for exploring and dumping PHP variables", + "homepage": "https://symfony.com", + "keywords": [ + "debug", + "dump" + ], + "time": "2018-06-23T12:23:56+00:00" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/fixtures/set024/expected-output b/fixtures/set024/expected-output new file mode 100644 index 00000000..ac590c76 --- /dev/null +++ b/fixtures/set024/expected-output @@ -0,0 +1,3 @@ +array:1 [ + 0 => "yo" +] diff --git a/fixtures/set024/main.php b/fixtures/set024/main.php new file mode 100644 index 00000000..16adce6a --- /dev/null +++ b/fixtures/set024/main.php @@ -0,0 +1,15 @@ + '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' diff --git a/specs/class-FQ.php b/specs/class-FQ.php index 4e7371c9..8861071f 100644 --- a/specs/class-FQ.php +++ b/specs/class-FQ.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-const/global-scope-single-level-with-single-level-use-statement-and-alias.php b/specs/class-const/global-scope-single-level-with-single-level-use-statement-and-alias.php index 7bef5185..a4be2f33 100644 --- a/specs/class-const/global-scope-single-level-with-single-level-use-statement-and-alias.php +++ b/specs/class-const/global-scope-single-level-with-single-level-use-statement-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-const/global-scope-single-level-with-single-level-use-statement.php b/specs/class-const/global-scope-single-level-with-single-level-use-statement.php index 306fdb43..38731bef 100644 --- a/specs/class-const/global-scope-single-level-with-single-level-use-statement.php +++ b/specs/class-const/global-scope-single-level-with-single-level-use-statement.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-const/global-scope-single-level.php b/specs/class-const/global-scope-single-level.php index 73385e46..79a4058e 100644 --- a/specs/class-const/global-scope-single-level.php +++ b/specs/class-const/global-scope-single-level.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-const/global-scope-two-level-with-single-level-use-and-alias.php b/specs/class-const/global-scope-two-level-with-single-level-use-and-alias.php index 9cf10810..692ceb8b 100644 --- a/specs/class-const/global-scope-two-level-with-single-level-use-and-alias.php +++ b/specs/class-const/global-scope-two-level-with-single-level-use-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-const/global-scope-two-level-with-single-level-use.php b/specs/class-const/global-scope-two-level-with-single-level-use.php index b484343b..c7e6ea41 100644 --- a/specs/class-const/global-scope-two-level-with-single-level-use.php +++ b/specs/class-const/global-scope-two-level-with-single-level-use.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-const/global-scope-two-level.php b/specs/class-const/global-scope-two-level.php index c971d0ed..e89f0dfc 100644 --- a/specs/class-const/global-scope-two-level.php +++ b/specs/class-const/global-scope-two-level.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-const/namespace-scope-single-level-with-single-level-use-statement-and-alias.php b/specs/class-const/namespace-scope-single-level-with-single-level-use-statement-and-alias.php index 73cbc569..b1cb71c1 100644 --- a/specs/class-const/namespace-scope-single-level-with-single-level-use-statement-and-alias.php +++ b/specs/class-const/namespace-scope-single-level-with-single-level-use-statement-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-const/namespace-scope-single-level.php b/specs/class-const/namespace-scope-single-level.php index 2da26ea8..cdcf7e12 100644 --- a/specs/class-const/namespace-scope-single-level.php +++ b/specs/class-const/namespace-scope-single-level.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-const/namespace-scope-two-level-with-single-level-use-and-alias.php b/specs/class-const/namespace-scope-two-level-with-single-level-use-and-alias.php index 139f1c0c..b85c51c8 100644 --- a/specs/class-const/namespace-scope-two-level-with-single-level-use-and-alias.php +++ b/specs/class-const/namespace-scope-two-level-with-single-level-use-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-const/namespace-scope-two-level-with-single-level-use.php b/specs/class-const/namespace-scope-two-level-with-single-level-use.php index f3d16810..e23e8712 100644 --- a/specs/class-const/namespace-scope-two-level-with-single-level-use.php +++ b/specs/class-const/namespace-scope-two-level-with-single-level-use.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-const/namespace-scope-two-level.php b/specs/class-const/namespace-scope-two-level.php index 632ba73e..32f34c28 100644 --- a/specs/class-const/namespace-scope-two-level.php +++ b/specs/class-const/namespace-scope-two-level.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-const/namespace-single-part-with-single-level-use-statement.php b/specs/class-const/namespace-single-part-with-single-level-use-statement.php index c5e1c646..b48401b2 100644 --- a/specs/class-const/namespace-single-part-with-single-level-use-statement.php +++ b/specs/class-const/namespace-single-part-with-single-level-use-statement.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-static-prop/global-scope-single-level-with-single-level-use-statement-and-alias.php b/specs/class-static-prop/global-scope-single-level-with-single-level-use-statement-and-alias.php index 6a113e28..34130853 100644 --- a/specs/class-static-prop/global-scope-single-level-with-single-level-use-statement-and-alias.php +++ b/specs/class-static-prop/global-scope-single-level-with-single-level-use-statement-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-static-prop/global-scope-single-level-with-single-level-use-statement.php b/specs/class-static-prop/global-scope-single-level-with-single-level-use-statement.php index e5f1acdd..8f51e159 100644 --- a/specs/class-static-prop/global-scope-single-level-with-single-level-use-statement.php +++ b/specs/class-static-prop/global-scope-single-level-with-single-level-use-statement.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-static-prop/global-scope-single-level.php b/specs/class-static-prop/global-scope-single-level.php index 10c94365..d9f8378d 100644 --- a/specs/class-static-prop/global-scope-single-level.php +++ b/specs/class-static-prop/global-scope-single-level.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-static-prop/global-scope-two-level-with-single-level-use-and-alias.php b/specs/class-static-prop/global-scope-two-level-with-single-level-use-and-alias.php index 9305d536..09dd2f03 100644 --- a/specs/class-static-prop/global-scope-two-level-with-single-level-use-and-alias.php +++ b/specs/class-static-prop/global-scope-two-level-with-single-level-use-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-static-prop/global-scope-two-level-with-single-level-use.php b/specs/class-static-prop/global-scope-two-level-with-single-level-use.php index 44ab9788..ee036c81 100644 --- a/specs/class-static-prop/global-scope-two-level-with-single-level-use.php +++ b/specs/class-static-prop/global-scope-two-level-with-single-level-use.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-static-prop/global-scope-two-level.php b/specs/class-static-prop/global-scope-two-level.php index 0519eb87..5a2816ab 100644 --- a/specs/class-static-prop/global-scope-two-level.php +++ b/specs/class-static-prop/global-scope-two-level.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-static-prop/namespace-scope-single-level-with-single-level-use-statement-and-alias.php b/specs/class-static-prop/namespace-scope-single-level-with-single-level-use-statement-and-alias.php index b49eca4e..1df1923a 100644 --- a/specs/class-static-prop/namespace-scope-single-level-with-single-level-use-statement-and-alias.php +++ b/specs/class-static-prop/namespace-scope-single-level-with-single-level-use-statement-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-static-prop/namespace-scope-single-level.php b/specs/class-static-prop/namespace-scope-single-level.php index eae86fd0..a7698554 100644 --- a/specs/class-static-prop/namespace-scope-single-level.php +++ b/specs/class-static-prop/namespace-scope-single-level.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-static-prop/namespace-scope-two-level-with-single-level-use-and-alias.php b/specs/class-static-prop/namespace-scope-two-level-with-single-level-use-and-alias.php index 1555e932..925364c3 100644 --- a/specs/class-static-prop/namespace-scope-two-level-with-single-level-use-and-alias.php +++ b/specs/class-static-prop/namespace-scope-two-level-with-single-level-use-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-static-prop/namespace-scope-two-level-with-single-level-use.php b/specs/class-static-prop/namespace-scope-two-level-with-single-level-use.php index 0bb58c1b..31e263a7 100644 --- a/specs/class-static-prop/namespace-scope-two-level-with-single-level-use.php +++ b/specs/class-static-prop/namespace-scope-two-level-with-single-level-use.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-static-prop/namespace-scope-two-level.php b/specs/class-static-prop/namespace-scope-two-level.php index 50961ea3..0c08d75f 100644 --- a/specs/class-static-prop/namespace-scope-two-level.php +++ b/specs/class-static-prop/namespace-scope-two-level.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class-static-prop/namespace-single-part-with-single-level-use-statement.php b/specs/class-static-prop/namespace-single-part-with-single-level-use-statement.php index 2d42ef55..b3d33489 100644 --- a/specs/class-static-prop/namespace-single-part-with-single-level-use-statement.php +++ b/specs/class-static-prop/namespace-single-part-with-single-level-use-statement.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/class/abstract.php b/specs/class/abstract.php index 8e25ce39..29dbe093 100644 --- a/specs/class/abstract.php +++ b/specs/class/abstract.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Declaration in the global namespace: add prefixed namespace' => <<<'PHP' diff --git a/specs/class/anonymous.php b/specs/class/anonymous.php index 12f72a5a..2bfe3d5e 100644 --- a/specs/class/anonymous.php +++ b/specs/class/anonymous.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Declaration in the global namespace: prefix non-internal classes' => <<<'PHP' diff --git a/specs/class/conditional.php b/specs/class/conditional.php index 09542f6d..0e2b63e4 100644 --- a/specs/class/conditional.php +++ b/specs/class/conditional.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Declaration in the global namespace: warp in a prefixed namespace.' => <<<'PHP' diff --git a/specs/class/final.php b/specs/class/final.php index d79bae2c..f9964fa7 100644 --- a/specs/class/final.php +++ b/specs/class/final.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Declaration in the global namespace: add prefixed namespace.' => <<<'PHP' diff --git a/specs/class/interface.php b/specs/class/interface.php index 0a8057b4..d0fafe94 100644 --- a/specs/class/interface.php +++ b/specs/class/interface.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Declaration in the global namespace: add a prefixed namespace.' => <<<'PHP' diff --git a/specs/class/regular-extend.php b/specs/class/regular-extend.php index fd86d466..a9f47871 100644 --- a/specs/class/regular-extend.php +++ b/specs/class/regular-extend.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Declaration in the global namespace: prefix only non-internal classes.' => <<<'PHP' diff --git a/specs/class/regular.php b/specs/class/regular.php index d8da326f..23d0004b 100644 --- a/specs/class/regular.php +++ b/specs/class/regular.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Declaration in the global namespace: add a prefixed namespace.' => <<<'PHP' diff --git a/specs/class/trait.php b/specs/class/trait.php index 29de77ed..a2e0b74f 100644 --- a/specs/class/trait.php +++ b/specs/class/trait.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Declaration in the global namespace: add prefixed namespace.' => <<<'PHP' diff --git a/specs/const/const-declaration-with-global-whitelisting.php b/specs/const/const-declaration-with-global-whitelisting.php index f1fe0298..3b4c6666 100644 --- a/specs/const/const-declaration-with-global-whitelisting.php +++ b/specs/const/const-declaration-with-global-whitelisting.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Constants declaration in the global namespace' => [ diff --git a/specs/const/const-declaration.php b/specs/const/const-declaration.php index 7187ce0d..9e3ea974 100644 --- a/specs/const/const-declaration.php +++ b/specs/const/const-declaration.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], 'Constants declaration in the global namespace' => [ diff --git a/specs/const/global-scope-global-with-global-whitelisting.php b/specs/const/global-scope-global-with-global-whitelisting.php index ed0220cf..a1f347c4 100644 --- a/specs/const/global-scope-global-with-global-whitelisting.php +++ b/specs/const/global-scope-global-with-global-whitelisting.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/const/global-scope-global-with-single-level-use-statement-and-alias.php b/specs/const/global-scope-global-with-single-level-use-statement-and-alias.php index f4728c8a..c9ef1d21 100644 --- a/specs/const/global-scope-global-with-single-level-use-statement-and-alias.php +++ b/specs/const/global-scope-global-with-single-level-use-statement-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/const/global-scope-global-with-single-level-use-statement-with-global-whitelisting.php b/specs/const/global-scope-global-with-single-level-use-statement-with-global-whitelisting.php index 1dbdc31c..8d1768e7 100644 --- a/specs/const/global-scope-global-with-single-level-use-statement-with-global-whitelisting.php +++ b/specs/const/global-scope-global-with-single-level-use-statement-with-global-whitelisting.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/const/global-scope-global-with-single-level-use-statement.php b/specs/const/global-scope-global-with-single-level-use-statement.php index db1d35e9..ac4ac5fb 100644 --- a/specs/const/global-scope-global-with-single-level-use-statement.php +++ b/specs/const/global-scope-global-with-single-level-use-statement.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/const/global-scope-global.php b/specs/const/global-scope-global.php index 80144b65..e62ae073 100644 --- a/specs/const/global-scope-global.php +++ b/specs/const/global-scope-global.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/const/global-scope-single-part-namespaced-with-single-level-use-alias.php b/specs/const/global-scope-single-part-namespaced-with-single-level-use-alias.php index cba39d47..d998503b 100644 --- a/specs/const/global-scope-single-part-namespaced-with-single-level-use-alias.php +++ b/specs/const/global-scope-single-part-namespaced-with-single-level-use-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/const/global-scope-single-part-namespaced-with-single-level-use.php b/specs/const/global-scope-single-part-namespaced-with-single-level-use.php index 016f3643..1ecb2b82 100644 --- a/specs/const/global-scope-single-part-namespaced-with-single-level-use.php +++ b/specs/const/global-scope-single-part-namespaced-with-single-level-use.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/const/global-scope-single-part-namespaced.php b/specs/const/global-scope-single-part-namespaced.php index ca781281..21e6d868 100644 --- a/specs/const/global-scope-single-part-namespaced.php +++ b/specs/const/global-scope-single-part-namespaced.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/const/global-scope-two-parts-namespaced-with-single-level-use-and-alias.php b/specs/const/global-scope-two-parts-namespaced-with-single-level-use-and-alias.php index 4b907ae1..5a149efd 100644 --- a/specs/const/global-scope-two-parts-namespaced-with-single-level-use-and-alias.php +++ b/specs/const/global-scope-two-parts-namespaced-with-single-level-use-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/const/global-scope-two-parts-namespaced-with-single-level-use.php b/specs/const/global-scope-two-parts-namespaced-with-single-level-use.php index eb8a2938..d9ab253c 100644 --- a/specs/const/global-scope-two-parts-namespaced-with-single-level-use.php +++ b/specs/const/global-scope-two-parts-namespaced-with-single-level-use.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/const/global-scope-two-parts-namespaced.php b/specs/const/global-scope-two-parts-namespaced.php index 59bef5d4..58d11869 100644 --- a/specs/const/global-scope-two-parts-namespaced.php +++ b/specs/const/global-scope-two-parts-namespaced.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/const/namespace-global-with-global-whitelisting.php b/specs/const/namespace-global-with-global-whitelisting.php index 243eb957..c4894dee 100644 --- a/specs/const/namespace-global-with-global-whitelisting.php +++ b/specs/const/namespace-global-with-global-whitelisting.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/const/namespace-global-with-single-level-use-statement-and-alias.php b/specs/const/namespace-global-with-single-level-use-statement-and-alias.php index 5609c480..f1f14519 100644 --- a/specs/const/namespace-global-with-single-level-use-statement-and-alias.php +++ b/specs/const/namespace-global-with-single-level-use-statement-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], // As it is extremely rare to use a `use const` statement for a built-in constant from the diff --git a/specs/const/namespace-global-with-single-level-use-statement.php b/specs/const/namespace-global-with-single-level-use-statement.php index f5bbd8b7..19d98aba 100644 --- a/specs/const/namespace-global-with-single-level-use-statement.php +++ b/specs/const/namespace-global-with-single-level-use-statement.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], // As it is extremely rare to use a `use const` statement for a built-in constant from the diff --git a/specs/const/namespace-global.php b/specs/const/namespace-global.php index 43c02b39..fe7ab60e 100644 --- a/specs/const/namespace-global.php +++ b/specs/const/namespace-global.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/const/namespace-single-part-namespaced.php b/specs/const/namespace-single-part-namespaced.php index 13337d38..d6467c11 100644 --- a/specs/const/namespace-single-part-namespaced.php +++ b/specs/const/namespace-single-part-namespaced.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/const/native-const-with-global-whitelisting.php b/specs/const/native-const-with-global-whitelisting.php index 526e1e8b..43f36fb5 100644 --- a/specs/const/native-const-with-global-whitelisting.php +++ b/specs/const/native-const-with-global-whitelisting.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Internal function in a namespace: make the call into a FQ call' => <<<'PHP' diff --git a/specs/const/native-const.php b/specs/const/native-const.php index 206e2fe7..39b6d290 100644 --- a/specs/const/native-const.php +++ b/specs/const/native-const.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], 'Internal function in a namespace: make the call into a FQ call' => <<<'PHP' diff --git a/specs/exp/cast.php b/specs/exp/cast.php index 03b0b2df..799cf51a 100644 --- a/specs/exp/cast.php +++ b/specs/exp/cast.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Cast variable' => <<<'PHP' diff --git a/specs/exp/catch.php b/specs/exp/catch.php index c2e5af61..1d8dcb59 100644 --- a/specs/exp/catch.php +++ b/specs/exp/catch.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Catch an internal class' => <<<'PHP' diff --git a/specs/exp/instanceof.php b/specs/exp/instanceof.php index c5d04ab6..b4588188 100644 --- a/specs/exp/instanceof.php +++ b/specs/exp/instanceof.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Instance of an internal class' => <<<'PHP' diff --git a/specs/func-declaration/global.php b/specs/func-declaration/global.php index 9c4c0f37..a5fe3cbb 100644 --- a/specs/func-declaration/global.php +++ b/specs/func-declaration/global.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/func-declaration/method.php b/specs/func-declaration/method.php index bb35c326..6e84eab1 100644 --- a/specs/func-declaration/method.php +++ b/specs/func-declaration/method.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/func-declaration/namespace.php b/specs/func-declaration/namespace.php index 7391106b..dbc8092e 100644 --- a/specs/func-declaration/namespace.php +++ b/specs/func-declaration/namespace.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/function/global-scope-global-func-with-single-level-use-statement-and-alias.php b/specs/function/global-scope-global-func-with-single-level-use-statement-and-alias.php index c63eb47e..ea709d21 100644 --- a/specs/function/global-scope-global-func-with-single-level-use-statement-and-alias.php +++ b/specs/function/global-scope-global-func-with-single-level-use-statement-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/function/global-scope-global-func-with-single-level-use-statement.php b/specs/function/global-scope-global-func-with-single-level-use-statement.php index 12032fd1..96712dae 100644 --- a/specs/function/global-scope-global-func-with-single-level-use-statement.php +++ b/specs/function/global-scope-global-func-with-single-level-use-statement.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/function/global-scope-global-func.php b/specs/function/global-scope-global-func.php index e1f06c99..a145ebae 100644 --- a/specs/function/global-scope-global-func.php +++ b/specs/function/global-scope-global-func.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/function/global-scope-single-part-namespaced-func-with-single-level-use-and-alias.php b/specs/function/global-scope-single-part-namespaced-func-with-single-level-use-and-alias.php index a29e52d4..48cc142c 100644 --- a/specs/function/global-scope-single-part-namespaced-func-with-single-level-use-and-alias.php +++ b/specs/function/global-scope-single-part-namespaced-func-with-single-level-use-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/function/global-scope-single-part-namespaced-func-with-single-level-use.php b/specs/function/global-scope-single-part-namespaced-func-with-single-level-use.php index f67de1c0..09aec115 100644 --- a/specs/function/global-scope-single-part-namespaced-func-with-single-level-use.php +++ b/specs/function/global-scope-single-part-namespaced-func-with-single-level-use.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/function/global-scope-single-part-namespaced-func.php b/specs/function/global-scope-single-part-namespaced-func.php index c514425c..736b14fd 100644 --- a/specs/function/global-scope-single-part-namespaced-func.php +++ b/specs/function/global-scope-single-part-namespaced-func.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/function/namespace-global-func-with-single-level-use-statement-and-alias.php b/specs/function/namespace-global-func-with-single-level-use-statement-and-alias.php index 01163492..8ae3ec89 100644 --- a/specs/function/namespace-global-func-with-single-level-use-statement-and-alias.php +++ b/specs/function/namespace-global-func-with-single-level-use-statement-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/function/namespace-global-func-with-single-level-use-statement.php b/specs/function/namespace-global-func-with-single-level-use-statement.php index 5ef89266..f1dd9ff6 100644 --- a/specs/function/namespace-global-func-with-single-level-use-statement.php +++ b/specs/function/namespace-global-func-with-single-level-use-statement.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/function/namespace-global-func.php b/specs/function/namespace-global-func.php index ccf400f1..c17cd1bc 100644 --- a/specs/function/namespace-global-func.php +++ b/specs/function/namespace-global-func.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/function/namespace-global-scope-func.php b/specs/function/namespace-global-scope-func.php index 3c2d2a1c..1ebcbbf2 100644 --- a/specs/function/namespace-global-scope-func.php +++ b/specs/function/namespace-global-scope-func.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], // We don't do anything as there is no ways to distinguish between a namespaced function call diff --git a/specs/function/namespace-single-part-namespaced-func.php b/specs/function/namespace-single-part-namespaced-func.php index b0d25c54..cad0228c 100644 --- a/specs/function/namespace-single-part-namespaced-func.php +++ b/specs/function/namespace-single-part-namespaced-func.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/function/native-func.php b/specs/function/native-func.php index fb21f970..fb1f3d75 100644 --- a/specs/function/native-func.php +++ b/specs/function/native-func.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Internal function in a namespace: make the call into a FQ call' => <<<'PHP' diff --git a/specs/misc.php b/specs/misc.php index 4d84ec02..f3cac692 100644 --- a/specs/misc.php +++ b/specs/misc.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Empty file: do nothing' => <<<'PHP' diff --git a/specs/namespace/after-hashbang.php b/specs/namespace/after-hashbang.php index f4246915..59810fd7 100644 --- a/specs/namespace/after-hashbang.php +++ b/specs/namespace/after-hashbang.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], <<<'PHP' diff --git a/specs/namespace/braces.php b/specs/namespace/braces.php index 122b4cc8..1c4d9e99 100644 --- a/specs/namespace/braces.php +++ b/specs/namespace/braces.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'One level namespace: prefix it' => <<<'PHP' diff --git a/specs/namespace/creation-for-whitelist.php b/specs/namespace/creation-for-whitelist.php index 5f332817..74172de2 100644 --- a/specs/namespace/creation-for-whitelist.php +++ b/specs/namespace/creation-for-whitelist.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Single class should receive namespace' => <<<'PHP' diff --git a/specs/namespace/no-braces.php b/specs/namespace/no-braces.php index d3ff2572..99f4ad18 100644 --- a/specs/namespace/no-braces.php +++ b/specs/namespace/no-braces.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Root namespace: prefix the namespace' => <<<'PHP' diff --git a/specs/namespace/outside-statements.php b/specs/namespace/outside-statements.php index 977c8d6b..f61e4018 100644 --- a/specs/namespace/outside-statements.php +++ b/specs/namespace/outside-statements.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'Declare statement' => <<<'PHP' diff --git a/specs/new/global-scope-single-part-with-single-level-use-statement-an-alias.php b/specs/new/global-scope-single-part-with-single-level-use-statement-an-alias.php index 04dea690..5532742c 100644 --- a/specs/new/global-scope-single-part-with-single-level-use-statement-an-alias.php +++ b/specs/new/global-scope-single-part-with-single-level-use-statement-an-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/new/global-scope-single-part-with-single-level-use-statement.php b/specs/new/global-scope-single-part-with-single-level-use-statement.php index 40eb355b..7f84e1da 100644 --- a/specs/new/global-scope-single-part-with-single-level-use-statement.php +++ b/specs/new/global-scope-single-part-with-single-level-use-statement.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/new/global-scope-single-part.php b/specs/new/global-scope-single-part.php index 7567f639..500ab8e8 100644 --- a/specs/new/global-scope-single-part.php +++ b/specs/new/global-scope-single-part.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/new/global-scope-two-parts-with-single-level-use-and-alias.php b/specs/new/global-scope-two-parts-with-single-level-use-and-alias.php index 6074c3a2..e78dd971 100644 --- a/specs/new/global-scope-two-parts-with-single-level-use-and-alias.php +++ b/specs/new/global-scope-two-parts-with-single-level-use-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/new/global-scope-two-parts-with-single-level-use.php b/specs/new/global-scope-two-parts-with-single-level-use.php index 1491ac6f..05bbf6fa 100644 --- a/specs/new/global-scope-two-parts-with-single-level-use.php +++ b/specs/new/global-scope-two-parts-with-single-level-use.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/new/global-scope-two-parts.php b/specs/new/global-scope-two-parts.php index ef1ee4fd..301bb80c 100644 --- a/specs/new/global-scope-two-parts.php +++ b/specs/new/global-scope-two-parts.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/new/namespace-single-part-with-single-level-use-statement.php b/specs/new/namespace-single-part-with-single-level-use-statement.php index 0ba053c5..a5f2f488 100644 --- a/specs/new/namespace-single-part-with-single-level-use-statement.php +++ b/specs/new/namespace-single-part-with-single-level-use-statement.php @@ -20,6 +20,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/new/namespace-single-part.php b/specs/new/namespace-single-part.php index 3622bd1c..04166dde 100644 --- a/specs/new/namespace-single-part.php +++ b/specs/new/namespace-single-part.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/new/namespace-two-parts-with-single-level-use.php b/specs/new/namespace-two-parts-with-single-level-use.php index b54187aa..8bc929fe 100644 --- a/specs/new/namespace-two-parts-with-single-level-use.php +++ b/specs/new/namespace-two-parts-with-single-level-use.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/new/namespace-two-parts-with-two-level-use.php b/specs/new/namespace-two-parts-with-two-level-use.php index 90f55ffb..9bf41b57 100644 --- a/specs/new/namespace-two-parts-with-two-level-use.php +++ b/specs/new/namespace-two-parts-with-two-level-use.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/new/namespace-two-parts.php b/specs/new/namespace-two-parts.php index 97d4a8aa..5790009f 100644 --- a/specs/new/namespace-two-parts.php +++ b/specs/new/namespace-two-parts.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/special-keywords/self-static-parent-const.php b/specs/special-keywords/self-static-parent-const.php index 34c4bfcf..33d7382b 100644 --- a/specs/special-keywords/self-static-parent-const.php +++ b/specs/special-keywords/self-static-parent-const.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/special-keywords/self-static-parent-method.php b/specs/special-keywords/self-static-parent-method.php index 8971af5f..9cdcb1a7 100644 --- a/specs/special-keywords/self-static-parent-method.php +++ b/specs/special-keywords/self-static-parent-method.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/special-keywords/self-static-parent-static-var.php b/specs/special-keywords/self-static-parent-static-var.php index b88c1909..be3522f8 100644 --- a/specs/special-keywords/self-static-parent-static-var.php +++ b/specs/special-keywords/self-static-parent-static-var.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/static-method/global-scope-single-part-with-single-level-use-statement-and-alias.php b/specs/static-method/global-scope-single-part-with-single-level-use-statement-and-alias.php index fb7d4c89..65a0dc45 100644 --- a/specs/static-method/global-scope-single-part-with-single-level-use-statement-and-alias.php +++ b/specs/static-method/global-scope-single-part-with-single-level-use-statement-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/static-method/global-scope-single-part-with-single-level-use-statement.php b/specs/static-method/global-scope-single-part-with-single-level-use-statement.php index f16ddfac..0e68dd1a 100644 --- a/specs/static-method/global-scope-single-part-with-single-level-use-statement.php +++ b/specs/static-method/global-scope-single-part-with-single-level-use-statement.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/static-method/global-scope-single-part.php b/specs/static-method/global-scope-single-part.php index 62198334..33946cf1 100644 --- a/specs/static-method/global-scope-single-part.php +++ b/specs/static-method/global-scope-single-part.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/static-method/global-scope-two-parts-with-single-level-use-and-alias.php b/specs/static-method/global-scope-two-parts-with-single-level-use-and-alias.php index ca634ccc..14d440c6 100644 --- a/specs/static-method/global-scope-two-parts-with-single-level-use-and-alias.php +++ b/specs/static-method/global-scope-two-parts-with-single-level-use-and-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/static-method/global-scope-two-parts-with-single-level-use.php b/specs/static-method/global-scope-two-parts-with-single-level-use.php index 0899cc25..67bee1dd 100644 --- a/specs/static-method/global-scope-two-parts-with-single-level-use.php +++ b/specs/static-method/global-scope-two-parts-with-single-level-use.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/static-method/global-scope-two-parts.php b/specs/static-method/global-scope-two-parts.php index 2edc6c7a..3fc52186 100644 --- a/specs/static-method/global-scope-two-parts.php +++ b/specs/static-method/global-scope-two-parts.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/static-method/namespace-single-part-with-single-level-use-statement.php b/specs/static-method/namespace-single-part-with-single-level-use-statement.php index df5c96bb..db23e3d7 100644 --- a/specs/static-method/namespace-single-part-with-single-level-use-statement.php +++ b/specs/static-method/namespace-single-part-with-single-level-use-statement.php @@ -20,6 +20,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/static-method/namespace-single-part.php b/specs/static-method/namespace-single-part.php index 83190c66..44baaa81 100644 --- a/specs/static-method/namespace-single-part.php +++ b/specs/static-method/namespace-single-part.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/static-method/namespace-two-parts-with-single-level-use.php b/specs/static-method/namespace-two-parts-with-single-level-use.php index 9a4dc1b6..a0f6eb90 100644 --- a/specs/static-method/namespace-two-parts-with-single-level-use.php +++ b/specs/static-method/namespace-two-parts-with-single-level-use.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/static-method/namespace-two-parts-with-two-level-use.php b/specs/static-method/namespace-two-parts-with-two-level-use.php index fa902bad..f5cac6ce 100644 --- a/specs/static-method/namespace-two-parts-with-two-level-use.php +++ b/specs/static-method/namespace-two-parts-with-two-level-use.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/static-method/namespace-two-parts.php b/specs/static-method/namespace-two-parts.php index 4a4a112d..e53bee09 100644 --- a/specs/static-method/namespace-two-parts.php +++ b/specs/static-method/namespace-two-parts.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/string-literal/array-var.php b/specs/string-literal/array-var.php index c051e4c1..090e7909 100644 --- a/specs/string-literal/array-var.php +++ b/specs/string-literal/array-var.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'String argument: transform into a FQCN and prefix it' => <<<'PHP' diff --git a/specs/string-literal/class-const-array.php b/specs/string-literal/class-const-array.php index 9b36581c..e97a8973 100644 --- a/specs/string-literal/class-const-array.php +++ b/specs/string-literal/class-const-array.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'FQCN string argument: transform into a FQCN and prefix it' => <<<'PHP' diff --git a/specs/string-literal/class-const.php b/specs/string-literal/class-const.php index 9e34bd01..6baeabe6 100644 --- a/specs/string-literal/class-const.php +++ b/specs/string-literal/class-const.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'FQCN string argument: transform into a FQCN and prefix it' => <<<'PHP' diff --git a/specs/string-literal/class-prop.php b/specs/string-literal/class-prop.php index 4cb18e2a..e8e6669b 100644 --- a/specs/string-literal/class-prop.php +++ b/specs/string-literal/class-prop.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'FQCN string argument: transform into a FQCN and prefix it' => <<<'PHP' diff --git a/specs/string-literal/class-var.php b/specs/string-literal/class-var.php index 163ed22e..f611ba4d 100644 --- a/specs/string-literal/class-var.php +++ b/specs/string-literal/class-var.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'FQCN string argument: transform into a FQCN and prefix it' => <<<'PHP' diff --git a/specs/string-literal/const.php b/specs/string-literal/const.php index a157b0ed..7dcbe81e 100644 --- a/specs/string-literal/const.php +++ b/specs/string-literal/const.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'FQCN string argument: transform into a FQCN and prefix it' => <<<'PHP' diff --git a/specs/string-literal/define.php b/specs/string-literal/define.php index 3671c742..7e34f4f6 100644 --- a/specs/string-literal/define.php +++ b/specs/string-literal/define.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'FQCN string argument: transform into a FQCN and prefix it' => <<<'PHP' diff --git a/specs/string-literal/func-arg.php b/specs/string-literal/func-arg.php index daffbf4e..c9f3d830 100644 --- a/specs/string-literal/func-arg.php +++ b/specs/string-literal/func-arg.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'FQCN string argument: transform into a FQCN and prefix it' => <<<'PHP' diff --git a/specs/string-literal/method-arg.php b/specs/string-literal/method-arg.php index 7fbf1934..35f3368a 100644 --- a/specs/string-literal/method-arg.php +++ b/specs/string-literal/method-arg.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'FQCN string argument: transform into a FQCN and prefix it' => <<<'PHP' diff --git a/specs/string-literal/misc.php b/specs/string-literal/misc.php index 3cff0d4a..28f40254 100644 --- a/specs/string-literal/misc.php +++ b/specs/string-literal/misc.php @@ -19,9 +19,10 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], - 'PHP heredoc as argument' => <<<'PHP' + 'PHP heredoc as argument' => <<<'PHP' 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], 'FQCN string argument: transform into a FQCN and prefix it' => <<<'PHP' diff --git a/specs/use/use-class-alias.php b/specs/use/use-class-alias.php index af72109f..727635db 100644 --- a/specs/use/use-class-alias.php +++ b/specs/use/use-class-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/use/use-class-group.php b/specs/use/use-class-group.php index 6ba4dead..7ba119e8 100644 --- a/specs/use/use-class-group.php +++ b/specs/use/use-class-group.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/use/use-class.php b/specs/use/use-class.php index 98362e9f..9f60f8a0 100644 --- a/specs/use/use-class.php +++ b/specs/use/use-class.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/use/use-const-alias.php b/specs/use/use-const-alias.php index e1bfc328..1bc7f097 100644 --- a/specs/use/use-const-alias.php +++ b/specs/use/use-const-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/use/use-const-group.php b/specs/use/use-const-group.php index e8f2880b..708d1a1c 100644 --- a/specs/use/use-const-group.php +++ b/specs/use/use-const-group.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], <<<'PHP' diff --git a/specs/use/use-const.php b/specs/use/use-const.php index 5009a881..3f4dc811 100644 --- a/specs/use/use-const.php +++ b/specs/use/use-const.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/use/use-func-alias.php b/specs/use/use-func-alias.php index c14061f9..4b354856 100644 --- a/specs/use/use-func-alias.php +++ b/specs/use/use-func-alias.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/use/use-func-group.php b/specs/use/use-func-group.php index 598dc27f..6434a618 100644 --- a/specs/use/use-func-group.php +++ b/specs/use/use-func-group.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], <<<'PHP' diff --git a/specs/use/use-func.php b/specs/use/use-func.php index f6094e99..85ca09d5 100644 --- a/specs/use/use-func.php +++ b/specs/use/use-func.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], [ diff --git a/specs/use/use-mix-group.php b/specs/use/use-mix-group.php index e47fdd3d..2bf2ad0d 100644 --- a/specs/use/use-mix-group.php +++ b/specs/use/use-mix-group.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => true, + 'whitelist-global-functions' => true, ], <<<'PHP' diff --git a/specs/whitelist-case-sensitiveness.php b/specs/whitelist-case-sensitiveness.php index c476d49d..5589171b 100644 --- a/specs/whitelist-case-sensitiveness.php +++ b/specs/whitelist-case-sensitiveness.php @@ -19,6 +19,7 @@ 'prefix' => 'Humbug', 'whitelist' => [], 'whitelist-global-constants' => false, + 'whitelist-global-functions' => true, ], 'Class whitelists are case insensitive' => [ diff --git a/src/Autoload/ScoperAutoloadGenerator.php b/src/Autoload/ScoperAutoloadGenerator.php index 315d35b9..f1fd60fe 100644 --- a/src/Autoload/ScoperAutoloadGenerator.php +++ b/src/Autoload/ScoperAutoloadGenerator.php @@ -14,8 +14,15 @@ namespace Humbug\PhpScoper\Autoload; +use Humbug\PhpScoper\PhpParser\NodeVisitor\Collection\UserGlobalFunctionCollection; use Humbug\PhpScoper\Whitelist; +use PhpParser\Node\Name\FullyQualified; +use const PHP_EOL; use function array_map; +use function array_unshift; +use function iterator_to_array; +use function sprintf; +use function str_replace; final class ScoperAutoloadGenerator { @@ -28,30 +35,39 @@ public function __construct(Whitelist $whitelist) public function dump(string $prefix): string { - $statements = $this->createStatements($prefix); + $classAliasStatements = $this->createClassAliasStatements($prefix); - $statements = implode(PHP_EOL, $statements); + $statements = implode(PHP_EOL, $classAliasStatements).PHP_EOL.PHP_EOL; + $statements .= implode(PHP_EOL, $this->createFunctionAliasStatements($this->whitelist->getUserGlobalFunctions())); - return <<whitelist->getClassWhitelistArray() ); + + if ([] === $statements) { + return $statements; + } + + array_unshift( + $statements, + <<<'EOF' +// Aliases for the whitelisted classes. For more information see: +// https://github.com/humbug/php-scoper/blob/master/README.md#classes--constants-whitelisting +EOF + ); + + return $statements; + } + + /** + * @return string[] + */ + public function createFunctionAliasStatements(UserGlobalFunctionCollection $userGlobalFunctions): array + { + $statements = array_map( + function (array $node): string { + /** + * @var FullyQualified + * @var FullyQualified $alias + */ + [$original, $alias] = $node; + + return sprintf( + <<<'PHP' +if (!function_exists('%1$s')) { + function %1$s() { + return \%2$s(func_get_args()); + } +} +PHP + , + $original->toString(), + $alias->toString() + ); + }, + iterator_to_array($userGlobalFunctions) + ); + + if ([] === $statements) { + return $statements; + } + + array_unshift( + $statements, + <<<'EOF' +// Functions whitelisting. For more information see: +// https://github.com/humbug/php-scoper/blob/master/README.md#global-user-functions +EOF + ); + + return $statements; } } diff --git a/src/Configuration.php b/src/Configuration.php index 6601d069..adce798f 100644 --- a/src/Configuration.php +++ b/src/Configuration.php @@ -33,12 +33,14 @@ class Configuration private const PATCHERS_KEYWORD = 'patchers'; private const WHITELIST_KEYWORD = 'whitelist'; private const WHITELIST_GLOBAL_CONSTANTS_KEYWORD = 'whitelist-global-constants'; + private const WHITELIST_GLOBAL_FUNCTIONS_KEYWORD = 'whitelist-global-functions'; private const KEYWORDS = [ self::PREFIX, self::FINDER_KEYWORD, self::PATCHERS_KEYWORD, self::WHITELIST_KEYWORD, + self::WHITELIST_GLOBAL_FUNCTIONS_KEYWORD, ]; private $path; @@ -287,7 +289,23 @@ private static function retrieveWhitelist(array $config): Whitelist } } - return Whitelist::create($whitelistGlobalConstants, ...$whitelist); + if (false === array_key_exists(self::WHITELIST_GLOBAL_FUNCTIONS_KEYWORD, $config)) { + $whitelistGlobalFunctions = true; + } else { + $whitelistGlobalFunctions = $config[self::WHITELIST_GLOBAL_FUNCTIONS_KEYWORD]; + + if (false === is_bool($whitelistGlobalFunctions)) { + throw new InvalidArgumentException( + sprintf( + 'Expected %s to be a boolean, found "%s" instead.', + self::WHITELIST_GLOBAL_FUNCTIONS_KEYWORD, + gettype($whitelist) + ) + ); + } + } + + return Whitelist::create($whitelistGlobalConstants, $whitelistGlobalFunctions, ...$whitelist); } private static function retrieveFinders(array $config): array diff --git a/src/PhpParser/NodeVisitor/Collection/NamespaceStmtCollection.php b/src/PhpParser/NodeVisitor/Collection/NamespaceStmtCollection.php index 8c0dd72b..abc7752a 100644 --- a/src/PhpParser/NodeVisitor/Collection/NamespaceStmtCollection.php +++ b/src/PhpParser/NodeVisitor/Collection/NamespaceStmtCollection.php @@ -21,6 +21,7 @@ use PhpParser\Node; use PhpParser\Node\Name; use PhpParser\Node\Stmt\Namespace_; +use function count; /** * Utility class collecting all the namespaces for the scoped files allowing to easily find the namespace to which diff --git a/src/PhpParser/NodeVisitor/Collection/UserGlobalFunctionCollection.php b/src/PhpParser/NodeVisitor/Collection/UserGlobalFunctionCollection.php new file mode 100644 index 00000000..e2d39b8c --- /dev/null +++ b/src/PhpParser/NodeVisitor/Collection/UserGlobalFunctionCollection.php @@ -0,0 +1,50 @@ +, + * Pádraic Brady + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Humbug\PhpScoper\PhpParser\NodeVisitor\Collection; + +use ArrayIterator; +use Countable; +use IteratorAggregate; +use PhpParser\Node\Name\FullyQualified; +use function count; + +final class UserGlobalFunctionCollection implements IteratorAggregate, Countable +{ + /** + * @var FullyQualified[][] + */ + private $nodes = []; + + public function add(FullyQualified $original, FullyQualified $alias): void + { + $this->nodes[] = [$original, $alias]; + } + + /** + * @inheritdoc + */ + public function count(): int + { + return count($this->nodes); + } + + /** + * @inheritdoc + */ + public function getIterator(): iterable + { + return new ArrayIterator($this->nodes); + } +} diff --git a/src/PhpParser/NodeVisitor/FunctionIdentifierRecorder.php b/src/PhpParser/NodeVisitor/FunctionIdentifierRecorder.php new file mode 100644 index 00000000..63d585b2 --- /dev/null +++ b/src/PhpParser/NodeVisitor/FunctionIdentifierRecorder.php @@ -0,0 +1,88 @@ +, + * Pádraic Brady + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Humbug\PhpScoper\PhpParser\NodeVisitor; + +use Humbug\PhpScoper\PhpParser\NodeVisitor\Resolver\FullyQualifiedNameResolver; +use Humbug\PhpScoper\Whitelist; +use PhpParser\Node; +use PhpParser\Node\Identifier; +use PhpParser\Node\Name; +use PhpParser\Node\Name\FullyQualified; +use PhpParser\Node\Stmt\Function_; +use PhpParser\NodeVisitorAbstract; +use function count; + +/** + * Records the user functions registered in the global namespace. + * + * @private + */ +final class FunctionIdentifierRecorder extends NodeVisitorAbstract +{ + private $prefix; + private $nameResolver; + private $whitelist; + + public function __construct( + string $prefix, + FullyQualifiedNameResolver $nameResolver, + Whitelist $whitelist + ) { + $this->prefix = $prefix; + $this->nameResolver = $nameResolver; + $this->whitelist = $whitelist; + } + + /** + * @inheritdoc + */ + public function enterNode(Node $node): Node + { + if (false === ($node instanceof Identifier) || false === AppendParentNode::hasParent($node)) { + return $node; + } + + $parent = AppendParentNode::getParent($node); + + if (false === ($parent instanceof Function_)) { + return $node; + } + + /** @var Identifier $node */ + $resolvedValue = $this->nameResolver->resolveName( + new Name( + $node->name, + $node->getAttributes() + ) + ); + $resolvedName = $resolvedValue->getName(); + + if (null !== $resolvedValue->getNamespace() + || false === ($resolvedName instanceof FullyQualified) + || count($resolvedName->parts) > 1 + ) { + return $node; + } + + /* @var FullyQualified $resolvedName */ + + $this->whitelist->recordUserGlobalFunction( + $resolvedName, + FullyQualified::concat($this->prefix, $resolvedName) + ); + + return $node; + } +} diff --git a/src/PhpParser/TraverserFactory.php b/src/PhpParser/TraverserFactory.php index d9002614..d7408ad7 100644 --- a/src/PhpParser/TraverserFactory.php +++ b/src/PhpParser/TraverserFactory.php @@ -49,6 +49,10 @@ public function create(string $prefix, Whitelist $whitelist): NodeTraverserInter $traverser->addVisitor(new NodeVisitor\UseStmt\UseStmtCollector($namespaceStatements, $useStatements)); $traverser->addVisitor(new NodeVisitor\UseStmt\UseStmtPrefixer($prefix, $whitelist, $this->reflector)); + if ($whitelist->whitelistGlobalFunctions()) { + $traverser->addVisitor(new NodeVisitor\FunctionIdentifierRecorder($prefix, $nameResolver, $whitelist)); + } + $traverser->addVisitor(new NodeVisitor\NameStmtPrefixer($prefix, $whitelist, $nameResolver, $this->reflector)); $traverser->addVisitor(new NodeVisitor\StringScalarPrefixer($prefix, $whitelist, $this->reflector)); diff --git a/src/Whitelist.php b/src/Whitelist.php index ea8f9b27..469059fe 100644 --- a/src/Whitelist.php +++ b/src/Whitelist.php @@ -15,7 +15,9 @@ namespace Humbug\PhpScoper; use Countable; +use Humbug\PhpScoper\PhpParser\NodeVisitor\Collection\UserGlobalFunctionCollection; use InvalidArgumentException; +use PhpParser\Node\Name\FullyQualified; use function array_filter; use function array_map; use function array_pop; @@ -36,8 +38,10 @@ final class Whitelist implements Countable private $constants; private $namespaces; private $whitelistGlobalConstants; + private $whitelistGlobalFunctions; + private $userGlobalFunctions; - public static function create(bool $whitelistGlobalConstants, string ...$elements): self + public static function create(bool $whitelistGlobalConstants, bool $whitelistGlobalFunctions, string ...$elements): self { $classes = []; $constants = []; @@ -72,6 +76,7 @@ public static function create(bool $whitelistGlobalConstants, string ...$element return new self( $whitelistGlobalConstants, + $whitelistGlobalFunctions, array_unique($original), array_unique($classes), array_unique($constants), @@ -87,16 +92,29 @@ public static function create(bool $whitelistGlobalConstants, string ...$element */ private function __construct( bool $whitelistGlobalConstants, + bool $whitelistGlobalFunctions, array $original, array $classes, array $constants, array $namespaces ) { $this->whitelistGlobalConstants = $whitelistGlobalConstants; + $this->whitelistGlobalFunctions = $whitelistGlobalFunctions; $this->original = $original; $this->classes = $classes; $this->constants = $constants; $this->namespaces = $namespaces; + $this->userGlobalFunctions = new UserGlobalFunctionCollection(); + } + + public function recordUserGlobalFunction(FullyQualified $original, FullyQualified $alias): void + { + $this->userGlobalFunctions->add($original, $alias); + } + + public function getUserGlobalFunctions(): UserGlobalFunctionCollection + { + return $this->userGlobalFunctions; } public function whitelistGlobalConstants(): bool @@ -104,6 +122,12 @@ public function whitelistGlobalConstants(): bool return $this->whitelistGlobalConstants; } + public function whitelistGlobalFunctions(): bool + { + // TODO: check that nothing is appended/collected if everything is being whitelisted; avoid the collection in this case to avoid performance issues + return $this->whitelistGlobalFunctions; + } + public function isClassWhitelisted(string $name): bool { return in_array(strtolower($name), $this->classes, true); diff --git a/tests/Autoload/ScoperAutoloadGeneratorTest.php b/tests/Autoload/ScoperAutoloadGeneratorTest.php index ccaac688..833f53f9 100644 --- a/tests/Autoload/ScoperAutoloadGeneratorTest.php +++ b/tests/Autoload/ScoperAutoloadGeneratorTest.php @@ -15,34 +15,145 @@ namespace Humbug\PhpScoper\Autoload; use Humbug\PhpScoper\Whitelist; +use PhpParser\Node\Name\FullyQualified; use PHPUnit\Framework\TestCase; class ScoperAutoloadGeneratorTest extends TestCase { - public function test_generate_the_autoload() + /** + * @dataProvider provideWhitelists + */ + public function test_generate_the_autoload(Whitelist $whitelist, string $expected) { - $whitelist = Whitelist::create(true, 'A\Foo', 'B\Bar'); - $prefix = 'Humbug'; $generator = new ScoperAutoloadGenerator($whitelist); - $expected = <<<'PHP' + $actual = $generator->dump($prefix); + + $this->assertSame($expected, $actual); + } + + public function provideWhitelists() + { + yield [ + Whitelist::create(true, true), + <<<'PHP' dump($prefix); + yield [ + (function () { + $whitelist = Whitelist::create(true, true); - $this->assertSame($expected, $actual); + $whitelist->recordUserGlobalFunction( + new FullyQualified('foo'), + new FullyQualified('Humbug\foo') + ); + + $whitelist->recordUserGlobalFunction( + new FullyQualified('bar'), + new FullyQualified('Humbug\bar') + ); + + return $whitelist; + })(), + <<<'PHP' +recordUserGlobalFunction( + new FullyQualified('foo'), + new FullyQualified('Humbug\foo') + ); + + $whitelist->recordUserGlobalFunction( + new FullyQualified('bar'), + new FullyQualified('Humbug\bar') + ); + + return $whitelist; + })(), + <<<'PHP' +willReturn($prefixedContents) ; @@ -237,7 +237,7 @@ public function test_let_the_file_unchanged_when_cannot_scope_a_file() $inputContents, 'MyPrefix', [], - Whitelist::create(true) + Whitelist::create(true, true) ) ->willReturn($prefixedContents) ; @@ -250,7 +250,7 @@ public function test_let_the_file_unchanged_when_cannot_scope_a_file() $inputContents, 'MyPrefix', [], - Whitelist::create(true) + Whitelist::create(true, true) ) ->willThrow(new \RuntimeException('Scoping of the file failed')) ; @@ -310,7 +310,7 @@ public function test_do_not_scope_duplicated_given_paths() $inputContents, 'MyPrefix', [], - Whitelist::create(true) + Whitelist::create(true, true) ) ->willReturn($prefixedContents) ; @@ -371,7 +371,7 @@ public function test_scope_the_given_paths_and_the_ones_found_by_the_finder() $inputContents, 'MyPrefix', [], - Whitelist::create(true) + Whitelist::create(true, true) ) ->willReturn($prefixedFileContents) ; @@ -425,7 +425,7 @@ function (string $prefix): bool { } ), [], - Whitelist::create(true) + Whitelist::create(true, true) ) ->willReturn('') ; @@ -483,7 +483,7 @@ public function test_scope_the_current_working_directory_if_no_path_given() $inputContents, 'MyPrefix', [], - Whitelist::create(true) + Whitelist::create(true, true) ) ->willReturn($prefixedContents) ; @@ -529,7 +529,7 @@ public function test_prefix_can_end_by_a_backslash() Argument::any(), 'MyPrefix', [], - Whitelist::create(true) + Whitelist::create(true, true) ) ->willReturn('') ; @@ -572,7 +572,7 @@ public function test_prefix_can_end_by_multiple_backslashes() Argument::any(), 'MyPrefix', [], - Whitelist::create(true) + Whitelist::create(true, true) ) ->willReturn('') ; @@ -630,7 +630,7 @@ public function test_an_output_directory_can_be_given() $inputContents, 'MyPrefix', [], - Whitelist::create(true) + Whitelist::create(true, true) ) ->willReturn($prefixedContents) ; @@ -692,7 +692,7 @@ public function test_relative_output_directory_are_made_absolute() $inputContents, 'MyPrefix', [], - Whitelist::create(true) + Whitelist::create(true, true) ) ->willReturn($prefixedContents) ; @@ -783,7 +783,7 @@ public function test_attempts_to_use_patch_file_in_current_directory() return true; }), - Whitelist::create(true) + Whitelist::create(true, true) ) ->willReturn($prefixedContents) ; @@ -868,7 +868,7 @@ public function test_can_scope_projects_with_invalid_files() $fileContents, 'MyPrefix', [], - Whitelist::create(true) + Whitelist::create(true, true) ) ->willThrow($scopingException = new RuntimeException('Could not scope file')) ; diff --git a/tests/PhpParser/TraverserFactoryTest.php b/tests/PhpParser/TraverserFactoryTest.php index 3ca806f4..5f6e3596 100644 --- a/tests/PhpParser/TraverserFactoryTest.php +++ b/tests/PhpParser/TraverserFactoryTest.php @@ -28,7 +28,7 @@ public function test_creates_a_new_traverser_at_each_call() { $prefix = 'Humbug'; - $whitelist = Whitelist::create(true, 'Foo'); + $whitelist = Whitelist::create(true, true, 'Foo'); $classReflector = new Reflector( (new BetterReflection())->classReflector(), diff --git a/tests/Scoper/Composer/InstalledPackagesScoperTest.php b/tests/Scoper/Composer/InstalledPackagesScoperTest.php index fafccf43..033ad349 100644 --- a/tests/Scoper/Composer/InstalledPackagesScoperTest.php +++ b/tests/Scoper/Composer/InstalledPackagesScoperTest.php @@ -39,7 +39,7 @@ public function test_delegates_scoping_to_the_decorated_scoper_if_is_not_a_insta $fileContents = ''; $prefix = 'Humbug'; $patchers = [create_fake_patcher()]; - $whitelist = Whitelist::create(true, 'Foo'); + $whitelist = Whitelist::create(true, true, 'Foo'); /** @var Scoper|ObjectProphecy $decoratedScoperProphecy */ $decoratedScoperProphecy = $this->prophesize(Scoper::class); @@ -72,7 +72,7 @@ public function test_it_prefixes_the_composer_autoloaders(string $fileContents, $prefix = 'Foo'; $patchers = [create_fake_patcher()]; - $whitelist = Whitelist::create(true, 'Foo'); + $whitelist = Whitelist::create(true, true, 'Foo'); $actual = $scoper->scope($filePath, $fileContents, $prefix, $patchers, $whitelist); diff --git a/tests/Scoper/Composer/JsonFileScoperTest.php b/tests/Scoper/Composer/JsonFileScoperTest.php index 0b42d2d1..94f3d7e0 100644 --- a/tests/Scoper/Composer/JsonFileScoperTest.php +++ b/tests/Scoper/Composer/JsonFileScoperTest.php @@ -39,7 +39,7 @@ public function test_delegates_scoping_to_the_decorated_scoper_if_is_not_a_compo $fileContents = ''; $prefix = 'Humbug'; $patchers = [create_fake_patcher()]; - $whitelist = Whitelist::create(true, 'Foo'); + $whitelist = Whitelist::create(true, true, 'Foo'); /** @var Scoper|ObjectProphecy $decoratedScoperProphecy */ $decoratedScoperProphecy = $this->prophesize(Scoper::class); @@ -72,7 +72,7 @@ public function test_it_prefixes_the_composer_autoloaders(string $fileContents, $prefix = 'Foo'; $patchers = [create_fake_patcher()]; - $whitelist = Whitelist::create(true, 'Foo'); + $whitelist = Whitelist::create(true, true, 'Foo'); $actual = $scoper->scope($filePath, $fileContents, $prefix, $patchers, $whitelist); @@ -146,7 +146,7 @@ public function test_it_prefixes_psr0_autoloaders(string $fileContents, string $ $prefix = 'Foo'; $patchers = [create_fake_patcher()]; - $whitelist = Whitelist::create(true, 'Foo'); + $whitelist = Whitelist::create(true, true, 'Foo'); $actual = $scoper->scope($filePath, $fileContents, $prefix, $patchers, $whitelist); diff --git a/tests/Scoper/NullScoperTest.php b/tests/Scoper/NullScoperTest.php index eb7b0432..dd9ab6ab 100644 --- a/tests/Scoper/NullScoperTest.php +++ b/tests/Scoper/NullScoperTest.php @@ -38,7 +38,7 @@ public function test_returns_the_file_content_unchanged() $patchers = [create_fake_patcher()]; - $whitelist = Whitelist::create(true, 'Foo'); + $whitelist = Whitelist::create(true, true, 'Foo'); $scoper = new NullScoper(); diff --git a/tests/Scoper/PatchScoperTest.php b/tests/Scoper/PatchScoperTest.php index bf36064e..6cec6738 100644 --- a/tests/Scoper/PatchScoperTest.php +++ b/tests/Scoper/PatchScoperTest.php @@ -73,7 +73,7 @@ function (string $patcherFilePath, string $patcherPrefix, string $contents) use }, ]; - $whitelist = Whitelist::create(true, 'Foo'); + $whitelist = Whitelist::create(true, true, 'Foo'); $this->decoratedScoperProphecy ->scope($filePath, $contents, $prefix, $patchers, $whitelist) diff --git a/tests/Scoper/PhpScoperTest.php b/tests/Scoper/PhpScoperTest.php index 02350aca..20193755 100644 --- a/tests/Scoper/PhpScoperTest.php +++ b/tests/Scoper/PhpScoperTest.php @@ -157,7 +157,7 @@ public function test_can_scope_a_PHP_file() $prefix = 'Humbug'; $filePath = 'file.php'; $patchers = [create_fake_patcher()]; - $whitelist = Whitelist::create(true, 'Foo'); + $whitelist = Whitelist::create(true, true, 'Foo'); $contents = <<<'PHP' decoratedScoperProphecy ->scope($filePath, $fileContents, $prefix, $patchers, $whitelist) @@ -217,7 +217,7 @@ public function test_can_scope_a_PHP_file_with_the_wrong_extension() $prefix = 'Humbug'; $filePath = 'file'; $patchers = [create_fake_patcher()]; - $whitelist = Whitelist::create(true, 'Foo'); + $whitelist = Whitelist::create(true, true, 'Foo'); $contents = <<<'PHP' scoper->scope($filePath, $contents, $prefix, $patchers, $whitelist); @@ -347,7 +347,7 @@ public function test_creates_a_new_traverser_for_each_file() $prefix = 'Humbug'; $patchers = [create_fake_patcher()]; - $whitelist = Whitelist::create(true, 'Foo'); + $whitelist = Whitelist::create(true, true, 'Foo'); $this->decoratedScoperProphecy ->scope(Argument::any(), Argument::any(), $prefix, $patchers, $whitelist) @@ -579,6 +579,7 @@ private function parseSpecFile(array $meta, $fixtureTitle, $fixtureSet): Generat $fixtureSet['prefix'] ?? $meta['prefix'], Whitelist::create( $fixtureSet['whitelist-global-constants'] ?? $meta['whitelist-global-constants'], + $fixtureSet['whitelist-global-functions'] ?? $meta['whitelist-global-functions'], ...($fixtureSet['whitelist'] ?? $meta['whitelist']) ), '' === $payloadParts[1] ? null : $payloadParts[1], // Expected output diff --git a/tests/WhitelistTest.php b/tests/WhitelistTest.php index d903576c..f7f1ddae 100644 --- a/tests/WhitelistTest.php +++ b/tests/WhitelistTest.php @@ -31,7 +31,7 @@ public function test_it_can_be_created_from_a_list_of_strings( array $expectedClasses, array $expectedConstants ) { - $whitelistObject = Whitelist::create(true, ...$whitelist); + $whitelistObject = Whitelist::create(true, true, ...$whitelist); $whitelistReflection = new ReflectionClass(Whitelist::class); @@ -50,7 +50,7 @@ public function test_it_can_be_created_from_a_list_of_strings( $this->assertSame($expectedClasses, $actualClasses); $this->assertSame($expectedConstants, $actualConstants); - $whitelistObject = Whitelist::create(false, ...$whitelist); + $whitelistObject = Whitelist::create(false, false, ...$whitelist); $this->assertFalse($whitelistObject->whitelistGlobalConstants()); $this->assertSame($expectedClasses, $actualClasses); @@ -111,37 +111,37 @@ public function provideWhitelists() public function provideClassWhitelists() { yield [ - Whitelist::create(true), + Whitelist::create(true, true), 'Acme\Foo', false, ]; yield [ - Whitelist::create(true, 'Acme\Foo'), + Whitelist::create(true, true, 'Acme\Foo'), 'Acme\Foo', true, ]; yield [ - Whitelist::create(true, 'Acme\Foo'), + Whitelist::create(true, true, 'Acme\Foo'), 'Acme\Foo\Bar', false, ]; yield [ - Whitelist::create(true, 'Acme\Foo'), + Whitelist::create(true, true, 'Acme\Foo'), 'Acme', false, ]; yield [ - Whitelist::create(true, 'Acme'), + Whitelist::create(true, true, 'Acme'), 'Acme', true, ]; yield [ - Whitelist::create(true, 'Acme\*'), + Whitelist::create(true, true, 'Acme\*'), 'Acme', false, ]; @@ -150,67 +150,67 @@ public function provideClassWhitelists() public function provideNamespaceWhitelists() { yield [ - Whitelist::create(true), + Whitelist::create(true, true), 'Acme\Foo', false, ]; yield [ - Whitelist::create(true, 'Acme\Foo\*'), + Whitelist::create(true, true, 'Acme\Foo\*'), 'Acme\Foo', true, ]; yield [ - Whitelist::create(true, 'Acme\Foo\*'), + Whitelist::create(true, true, 'Acme\Foo\*'), 'acme\foo', true, ]; yield [ - Whitelist::create(true, 'Acme\*'), + Whitelist::create(true, true, 'Acme\*'), 'Acme\Foo', true, ]; yield [ - Whitelist::create(true, 'Acme\*'), + Whitelist::create(true, true, 'Acme\*'), 'acme\foo', true, ]; yield [ - Whitelist::create(true, 'Acme\Foo\*'), + Whitelist::create(true, true, 'Acme\Foo\*'), 'Acme\Foo\Bar', true, ]; yield [ - Whitelist::create(true, 'Acme\Foo\*'), + Whitelist::create(true, true, 'Acme\Foo\*'), 'acme\foo\bar', true, ]; yield [ - Whitelist::create(true, '\*'), + Whitelist::create(true, true, '\*'), 'Acme', true, ]; yield [ - Whitelist::create(true, '\*'), + Whitelist::create(true, true, '\*'), 'acme', true, ]; yield [ - Whitelist::create(true, '\*'), + Whitelist::create(true, true, '\*'), 'Acme\Foo', true, ]; yield [ - Whitelist::create(true, '\*'), + Whitelist::create(true, true, '\*'), 'acme\foo', true, ]; @@ -219,47 +219,47 @@ public function provideNamespaceWhitelists() public function provideWhitelistToConvert() { yield [ - Whitelist::create(true), + Whitelist::create(true, true), [], ]; yield [ - Whitelist::create(true, 'Acme\Foo'), + Whitelist::create(true, true, 'Acme\Foo'), ['Acme\Foo'], ]; yield [ - Whitelist::create(true, '\Acme\Foo'), + Whitelist::create(true, true, '\Acme\Foo'), ['Acme\Foo'], ]; yield [ - Whitelist::create(true, 'Acme\Foo\*'), + Whitelist::create(true, true, 'Acme\Foo\*'), ['Acme\Foo\*'], ]; yield [ - Whitelist::create(true, '\Acme\Foo\*'), + Whitelist::create(true, true, '\Acme\Foo\*'), ['Acme\Foo\*'], ]; yield [ - Whitelist::create(true, '*'), + Whitelist::create(true, true, '*'), ['*'], ]; yield [ - Whitelist::create(true, '\*'), + Whitelist::create(true, true, '\*'), ['*'], ]; yield [ - Whitelist::create(true, 'Acme', 'Acme\Foo', 'Acme\Foo\*', '*'), + Whitelist::create(true, true, 'Acme', 'Acme\Foo', 'Acme\Foo\*', '*'), ['Acme', 'Acme\Foo', 'Acme\Foo\*', '*'], ]; yield [ - Whitelist::create(true, 'Acme', 'Acme'), + Whitelist::create(true, true, 'Acme', 'Acme'), ['Acme'], ]; }