Skip to content

Commit bc3061c

Browse files
authored
Reworking the whitelist API (#247)
1 parent 53494e8 commit bc3061c

File tree

62 files changed

+1659
-1296
lines changed

Some content is hidden

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

62 files changed

+1659
-1296
lines changed

Makefile

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.DEFAULT_GOAL := help
22

3-
PHPNOGC=php -d zend.enable_gc=0
3+
PHPBIN=php
44

55
.PHONY: help
66
help:
@@ -35,7 +35,7 @@ test: tc e2e
3535
PHPUNIT=bin/phpunit
3636
tu: ## Run PHPUnit tests
3737
tu: bin/phpunit
38-
$(PHPNOGC) $(PHPUNIT)
38+
$(PHPBIN) $(PHPUNIT)
3939

4040
.PHONY: tc
4141
tc: ## Run PHPUnit tests with test coverage
@@ -55,23 +55,23 @@ PHPSCOPER=bin/php-scoper.phar
5555
.PHONY: e2e_004
5656
e2e_004: ## Run end-to-end tests for the fixture set 004 — Source code case
5757
e2e_004: bin/php-scoper.phar
58-
$(PHPNOGC) $(BOX) compile --working-dir fixtures/set004
58+
$(PHPBIN) $(BOX) compile --working-dir fixtures/set004
5959

6060
php build/set004/bin/greet.phar > build/set004/output
6161
diff fixtures/set004/expected-output build/set004/output
6262

6363
.PHONY: e2e_005
6464
e2e_005: ## Run end-to-end tests for the fixture set 005 — Third-party code case
6565
e2e_005: bin/php-scoper.phar fixtures/set005/vendor
66-
$(PHPNOGC) $(BOX) compile --working-dir fixtures/set005
66+
$(PHPBIN) $(BOX) compile --working-dir fixtures/set005
6767

6868
php build/set005/bin/greet.phar > build/set005/output
6969
diff fixtures/set005/expected-output build/set005/output
7070

7171
.PHONY: e2e_011
7272
e2e_011: ## Run end-to-end tests for the fixture set 011 — Whitelist case
7373
e2e_011: bin/php-scoper.phar fixtures/set011/vendor
74-
$(PHPNOGC) $(BOX) compile --working-dir fixtures/set011
74+
$(PHPBIN) $(BOX) compile --working-dir fixtures/set011
7575
cp -R fixtures/set011/tests/ build/set011/tests/
7676

7777
php build/set011/bin/greet.phar > build/set011/output
@@ -88,23 +88,23 @@ e2e_013: bin/php-scoper.phar
8888
.PHONY: e2e_014
8989
e2e_014: ## Run end-to-end tests for the fixture set 014 — Source code case with PSR-0
9090
e2e_014: bin/php-scoper.phar
91-
$(PHPNOGC) $(BOX) compile --working-dir fixtures/set014
91+
$(PHPBIN) $(BOX) compile --working-dir fixtures/set014
9292

9393
php build/set014/bin/greet.phar > build/set014/output
9494
diff fixtures/set014/expected-output build/set014/output
9595

9696
.PHONY: e2e_015
9797
e2e_015: ## Run end-to-end tests for the fixture set 015 — Third-party code case with PSR-0
9898
e2e_015: bin/php-scoper.phar fixtures/set015/vendor
99-
$(PHPNOGC) $(BOX) compile --working-dir fixtures/set015
99+
$(PHPBIN) $(BOX) compile --working-dir fixtures/set015
100100

101101
php build/set015/bin/greet.phar > build/set015/output
102102
diff fixtures/set015/expected-output build/set015/output
103103

104104
.PHONY: e2e_016
105105
e2e_016: ## Run end-to-end tests for the fixture set 016 — Symfony Finder
106106
e2e_016: bin/php-scoper.phar fixtures/set016-symfony-finder/vendor
107-
$(PHPNOGC) $(PHPSCOPER) add-prefix \
107+
$(PHPBIN) $(PHPSCOPER) add-prefix \
108108
--working-dir=fixtures/set016-symfony-finder \
109109
--output-dir=../../build/set016-symfony-finder \
110110
--force \
@@ -119,7 +119,7 @@ e2e_016: bin/php-scoper.phar fixtures/set016-symfony-finder/vendor
119119
.PHONY: e2e_017
120120
e2e_017: ## Run end-to-end tests for the fixture set 017 — Symfony DependencyInjection
121121
e2e_017: bin/php-scoper.phar fixtures/set017-symfony-di/vendor
122-
$(PHPNOGC) $(PHPSCOPER) add-prefix \
122+
$(PHPBIN) $(PHPSCOPER) add-prefix \
123123
--working-dir=fixtures/set017-symfony-di \
124124
--output-dir=../../build/set017-symfony-di \
125125
--force \
@@ -134,7 +134,7 @@ e2e_017: bin/php-scoper.phar fixtures/set017-symfony-di/vendor
134134
.PHONY: e2e_018
135135
e2e_018: ## Run end-to-end tests for the fixture set 018 — Nikic PHP-Parser
136136
e2e_018: bin/php-scoper.phar fixtures/set018-nikic-parser/vendor
137-
$(PHPNOGC) $(PHPSCOPER) add-prefix \
137+
$(PHPBIN) $(PHPSCOPER) add-prefix \
138138
--working-dir=fixtures/set018-nikic-parser \
139139
--prefix=_Prefixed \
140140
--output-dir=../../build/set018-nikic-parser \
@@ -149,7 +149,7 @@ e2e_018: bin/php-scoper.phar fixtures/set018-nikic-parser/vendor
149149
.PHONY: e2e_019
150150
e2e_019: ## Run end-to-end tests for the fixture set 019 — Symfony Console
151151
e2e_019: bin/php-scoper.phar fixtures/set019-symfony-console/vendor
152-
$(PHPNOGC) $(PHPSCOPER) add-prefix --working-dir=fixtures/set019-symfony-console \
152+
$(PHPBIN) $(PHPSCOPER) add-prefix --working-dir=fixtures/set019-symfony-console \
153153
--prefix=_Prefixed \
154154
--output-dir=../../build/set019-symfony-console \
155155
--force \
@@ -164,7 +164,7 @@ e2e_019: bin/php-scoper.phar fixtures/set019-symfony-console/vendor
164164
.PHONY: e2e_020
165165
e2e_020: ## Run end-to-end tests for the fixture set 020 — Infection
166166
e2e_020: bin/php-scoper.phar fixtures/set020-infection/vendor clover.xml
167-
$(PHPNOGC) $(PHPSCOPER) add-prefix --working-dir=fixtures/set020-infection \
167+
$(PHPBIN) $(PHPSCOPER) add-prefix --working-dir=fixtures/set020-infection \
168168
--output-dir=../../build/set020-infection \
169169
--force \
170170
--no-interaction \
@@ -182,8 +182,8 @@ e2e_020: bin/php-scoper.phar fixtures/set020-infection/vendor clover.xml
182182

183183
.PHONY: e2e_021
184184
e2e_021: ## Run end-to-end tests for the fixture set 020 — Composer
185-
e2e_021: bin/php-scoper.phar fixtures/set021-composer/vendor clover.xml
186-
$(PHPNOGC) $(PHPSCOPER) add-prefix --working-dir=fixtures/set021-composer \
185+
e2e_021: bin/php-scoper.phar fixtures/set021-composer/vendor
186+
$(PHPBIN) $(PHPSCOPER) add-prefix --working-dir=fixtures/set021-composer \
187187
--output-dir=../../build/set021-composer \
188188
--force \
189189
--no-interaction \
@@ -203,7 +203,7 @@ e2e_021: bin/php-scoper.phar fixtures/set021-composer/vendor clover.xml
203203
.PHONY: e2e_022
204204
e2e_022: ## Run end-to-end tests for the fixture set 022 — Whitelist the project code with namespace whitelisting
205205
e2e_022: bin/php-scoper.phar fixtures/set022/vendor
206-
$(PHPNOGC) $(BOX) compile --working-dir fixtures/set022
206+
$(PHPBIN) $(BOX) compile --working-dir fixtures/set022
207207
cp -R fixtures/set022/tests/ build/set022/tests/
208208

209209
php build/set022/bin/greet.phar > build/set022/output
@@ -213,7 +213,7 @@ e2e_022: bin/php-scoper.phar fixtures/set022/vendor
213213
.PHONY: e2e_023
214214
e2e_023: ## Run end-to-end tests for the fixture set 023 — Whitelisting a whole third-party component with namespace whitelisting
215215
e2e_023: bin/php-scoper.phar fixtures/set023/vendor
216-
$(PHPNOGC) $(PHPSCOPER) add-prefix --working-dir=fixtures/set023 \
216+
$(PHPBIN) $(PHPSCOPER) add-prefix --working-dir=fixtures/set023 \
217217
--output-dir=../../build/set023 \
218218
--force \
219219
--no-interaction \
@@ -226,7 +226,7 @@ e2e_023: bin/php-scoper.phar fixtures/set023/vendor
226226
.PHONY: e2e_024
227227
e2e_024: ## Run end-to-end tests for the fixture set 024 — Whitelisting user functions registered in the global namespace
228228
e2e_024: bin/php-scoper.phar fixtures/set024/vendor
229-
$(PHPNOGC) $(PHPSCOPER) add-prefix \
229+
$(PHPBIN) $(PHPSCOPER) add-prefix \
230230
--working-dir=fixtures/set024 \
231231
--output-dir=../../build/set024 \
232232
--force \
@@ -241,7 +241,7 @@ e2e_024: bin/php-scoper.phar fixtures/set024/vendor
241241
.PHONY: e2e_025
242242
e2e_025: ## Run end-to-end tests for the fixture set 025 — Whitelisting a vendor function
243243
e2e_025: bin/php-scoper.phar fixtures/set025/vendor
244-
$(PHPNOGC) $(PHPSCOPER) add-prefix \
244+
$(PHPBIN) $(PHPSCOPER) add-prefix \
245245
--working-dir=fixtures/set025 \
246246
--output-dir=../../build/set025 \
247247
--force \
@@ -255,7 +255,7 @@ e2e_025: bin/php-scoper.phar fixtures/set025/vendor
255255
.PHONY: e2e_026
256256
e2e_026: ## Run end-to-end tests for the fixture set 026 — Whitelisting classes and functions with pattern matching
257257
e2e_026: bin/php-scoper.phar fixtures/set026/vendor
258-
$(PHPNOGC) $(PHPSCOPER) add-prefix \
258+
$(PHPBIN) $(PHPSCOPER) add-prefix \
259259
--working-dir=fixtures/set026 \
260260
--output-dir=../../build/set026 \
261261
--force \
@@ -270,32 +270,26 @@ e2e_026: bin/php-scoper.phar fixtures/set026/vendor
270270
.PHONY: tb
271271
BLACKFIRE=blackfire
272272
tb: ## Run Blackfire profiling
273-
tb: vendor
274-
rm -rf build
275-
rm -rf vendor-bin/*/vendor
276-
277-
mv -f vendor tmp-back
278-
composer install --no-dev --prefer-dist --classmap-authoritative
279-
280-
$(BLACKFIRE) --new-reference run $(PHPNOGC) bin/php-scoper add-prefix --output-dir=build/php-scoper --force --quiet
281-
282-
rm -rf vendor
283-
mv -f tmp-back vendor
273+
tb: bin/php-scoper.phar vendor
274+
$(BLACKFIRE) --new-reference run $(PHPBIN) bin/php-scoper.phar add-prefix --output-dir=build/php-scoper --force --quiet
284275

285276
#
286277
# Rules from files
287278
#---------------------------------------------------------------------------
288279

289280
vendor: composer.lock
290-
composer install
281+
export COMPOSER_ROOT_VERSION='0.8.99'; composer install
282+
unset "COMPOSER_ROOT_VERSION"
291283
touch $@
292284

293285
vendor/bamarni: composer.lock
294-
composer install
286+
export COMPOSER_ROOT_VERSION='0.8.99'; composer install
287+
unset "COMPOSER_ROOT_VERSION"
295288
touch $@
296289

297290
bin/phpunit: composer.lock
298-
composer install
291+
export COMPOSER_ROOT_VERSION='0.8.99'; composer install
292+
unset "COMPOSER_ROOT_VERSION"
299293
touch $@
300294

301295
vendor-bin/covers-validator/vendor: vendor-bin/covers-validator/composer.lock vendor/bamarni
@@ -404,7 +398,7 @@ bin/php-scoper.phar: bin/php-scoper src vendor scoper.inc.php box.json
404398
box.json: box.json.dist
405399
cat box.json.dist | sed -E 's/\"key\": \".+\",//g' | sed -E 's/\"algorithm\": \".+\",//g' > box.json
406400

407-
COVERS_VALIDATOR=$(PHPNOGC) vendor-bin/covers-validator/bin/covers-validator
401+
COVERS_VALIDATOR=$(PHPBIN) vendor-bin/covers-validator/bin/covers-validator
408402
clover.xml: src
409403
$(COVERS_VALIDATOR)
410404
phpdbg -qrr -d zend.enable_gc=0 $(PHPUNIT) \

fixtures/set013/scoper.inc.php

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,8 @@
55
use Isolated\Symfony\Component\Finder\Finder;
66

77
return [
8-
'global_namespace_whitelist' => [
9-
'AppKernel',
10-
function (string $className): bool {
11-
return 'PHPUnit' === substr($className, 0, 6);
12-
},
13-
],
8+
// The prefix configuration. If a non null value will be used, a random prefix will be generated.
9+
'prefix' => null,
1410

1511
// By default when running php-scoper add-prefix, it will prefix all relevant code found in the current working
1612
// directory. You can however define which files should be scoped by defining a collection of Finders in the
@@ -45,25 +41,12 @@ function (string $className): bool {
4541
// For more see: https://github.com/humbug/php-scoper#patchers
4642
'patchers' => [
4743
function (string $filePath, string $prefix, string $contents): string {
48-
// Change the content here.
44+
// Change the contents here.
4945

5046
return $contents;
5147
},
5248
],
5349

54-
// By default, PHP-Scoper only prefixes code where the namespace is non-global. In other words, non-namespaced
55-
// code is not prefixed. This leaves the majority of classes, functions and constants in PHP - and most extensions,
56-
// untouched.
57-
//
58-
// This is not necessarily a desirable outcome for vendor dependencies which are also not namespaced. To ensure
59-
// they are isolated, you can configure the following which can be a list of strings or callables taking a string
60-
// (the class name) as an argument and return a boolean (true meaning the class is going to prefixed).
61-
//
62-
// For more, see https://github.com/humbug/php-scoper#global-namespace-whitelisting
63-
'global_namespace_whitelist' => [
64-
'AppKernel',
65-
],
66-
6750
// PHP-Scoper's goal is to make sure that all code for a project lies in a distinct PHP namespace. However, you
6851
// may want to share a common API between the bundled code of your PHAR and the consumer code. For example if
6952
// you have a PHPUnit PHAR with isolated code, you still want the PHAR to be able to understand the
@@ -74,6 +57,18 @@ function (string $filePath, string $prefix, string $contents): string {
7457
//
7558
// Fore more see https://github.com/humbug/php-scoper#whitelist
7659
'whitelist' => [
77-
'PHPUnit\Framework\TestCase',
60+
// 'PHPUnit\Framework\TestCase', // A specific class
61+
// 'PHPUnit\Framework\*', // The whole namespace
62+
// '*', // Everything
7863
],
64+
65+
// If `true` then the user defined constants belonging to the global namespace will not be prefixed.
66+
//
67+
// For more see https://github.com/humbug/php-scoper#constants-from-the-global-namespace-whitelisting
68+
'whitelist-global-constants' => true,
69+
70+
// If `true` then the user defined functions belonging to the global namespace will not be prefixed.
71+
//
72+
// For more see https://github.com/humbug/php-scoper#global-user-functions
73+
'whitelist-global-functions' => true,
7974
];

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

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@
2222
'whitelist-global-functions' => true,
2323
],
2424

25-
[
26-
'spec' => <<<'SPEC'
27-
Constant call on a aliased class which is imported via an aliased use statement and which belongs to the global namespace:
28-
- prefix the use statement (cf. class belonging to the global scope tests)
29-
- prefix the constant
30-
SPEC
31-
,
25+
'Constant call on a aliased class which is imported via an aliased use statement and which belongs to the global namespace' => [
3226
'payload' => <<<'PHP'
3327
<?php
3428
@@ -51,13 +45,7 @@ class Foo
5145
PHP
5246
],
5347

54-
[
55-
'spec' => <<<'SPEC'
56-
FQ constant call on a aliased class which is imported via an aliased use statement and which belongs to the global namespace:
57-
- do not prefix the class (cf. class belonging to the global scope tests)
58-
- do nothing
59-
SPEC
60-
,
48+
'FQ constant call on a aliased class which is imported via an aliased use statement and which belongs to the global namespace' => [
6149
'payload' => <<<'PHP'
6250
<?php
6351
@@ -84,13 +72,7 @@ class X
8472
PHP
8573
],
8674

87-
[
88-
'spec' => <<<'SPEC'
89-
Constant call on a whitelisted class which is imported via an aliased use statement and which belongs to the global namespace:
90-
- prefix the use statement (cf. class belonging to the global scope tests and `scope.inc.php` for the built-in global whitelisted classes)
91-
- transform the call into a FQ call
92-
SPEC
93-
,
75+
'Constant call on a whitelisted class which is imported via an aliased use statement and which belongs to the global namespace' => [
9476
'payload' => <<<'PHP'
9577
<?php
9678
@@ -108,13 +90,7 @@ class X
10890
PHP
10991
],
11092

111-
[
112-
'spec' => <<<'SPEC'
113-
FQ constant call on a whitelisted class which is imported via an aliased use statement and which belongs to the global namespace:
114-
- prefix the use statement (cf. class belonging to the global scope tests and `scope.inc.php` for the built-in global whitelisted classes)
115-
- do nothing
116-
SPEC
117-
,
93+
'FQ constant call on a whitelisted class which is imported via an aliased use statement and which belongs to the global namespace' => [
11894
'payload' => <<<'PHP'
11995
<?php
12096

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

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,7 @@
2222
'whitelist-global-functions' => true,
2323
],
2424

25-
[
26-
'spec' => <<<'SPEC'
27-
Constant call on a class which is imported via a use statement and which belongs to the global namespace:
28-
- do not prefix the use statement (cf. class belonging to the global scope tests)
29-
- transforms the call into a FQ call
30-
SPEC
31-
,
25+
'Constant call on a class which is imported via a use statement and which belongs to the global namespace' => [
3226
'payload' => <<<'PHP'
3327
<?php
3428
@@ -51,13 +45,7 @@ class Command
5145
PHP
5246
],
5347

54-
[
55-
'spec' => <<<'SPEC'
56-
FQ constant call on a class which is imported via a use statement and which belongs to the global namespace:
57-
- do not prefix the use statement (cf. class belonging to the global scope tests)
58-
- do nothing
59-
SPEC
60-
,
48+
'FQ constant call on a class which is imported via a use statement and which belongs to the global namespace' => [
6149
'payload' => <<<'PHP'
6250
<?php
6351
@@ -80,13 +68,7 @@ class Command
8068
PHP
8169
],
8270

83-
[
84-
'spec' => <<<'SPEC'
85-
Constant call on a whitelisted class which is imported via a use statement and which belongs to the global namespace:
86-
- transform the call in a FQ call (cf. class belonging to the global scope tests and `scope.inc.php` for the built-in
87-
global whitelisted classes)
88-
SPEC
89-
,
71+
'Constant call on a whitelisted class which is imported via a use statement and which belongs to the global namespace' => [
9072
'payload' => <<<'PHP'
9173
<?php
9274
@@ -104,13 +86,7 @@ class Command
10486
PHP
10587
],
10688

107-
[
108-
'spec' => <<<'SPEC'
109-
FQ constant call on a whitelisted class which is imported via a use statement and which belongs to the global namespace:
110-
- prefix the class (cf. class belonging to the global scope tests and `scope.inc.php` for the built-in global
111-
whitelisted classes)
112-
SPEC
113-
,
89+
'FQ constant call on a whitelisted class which is imported via a use statement and which belongs to the global namespace' => [
11490
'payload' => <<<'PHP'
11591
<?php
11692

0 commit comments

Comments
 (0)