Skip to content

Commit 243d41c

Browse files
authored
Prefix strings (#164)
1 parent a44080a commit 243d41c

File tree

15 files changed

+657
-294
lines changed

15 files changed

+657
-294
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ potentially very difficult to debug due to dissimilar or unsupported package ver
4444
- [Step 3: Build, test, and cleanup](#step-3-build-test-and-cleanup)
4545
- [Limitations](#limitations)
4646
- [PSR-0 support](#psr-0-support)
47+
- [String values](#string-values)
4748
- [Contributing](#contributing)
4849
- [Credits](#credits)
4950

@@ -415,6 +416,27 @@ add a significant overhead besides being more complex. As a result PHP-Scoper do
415416
support the exotic case above.
416417

417418

419+
### String values
420+
421+
PHP-Scoper tries whenever possible to prefix strings as well:
422+
423+
```php
424+
class_exists('Acme\Foo');
425+
426+
// Will be prefixed into:
427+
428+
\class_exists('Humbug\Acme\Foo');
429+
```
430+
431+
PHP-Scoper uses a regex to determine if the string is a class name that must be prefixed. But there is
432+
bound to have confusing cases. For example:
433+
434+
- If you have a plain string `'Acme\Foo'` which has nothing to do with a class, PHP-Parser will not be
435+
able to tell and will prefix it
436+
- Classes belonging to the global scope: `'Acme_Foo'`, because there is no way to know if it is a class
437+
name or a random string.
438+
439+
418440
## Contributing
419441

420442
[Contribution Guide](CONTRIBUTING.md)

specs/func-args/func.php

Lines changed: 0 additions & 130 deletions
This file was deleted.

specs/func-args/whitelisted-func.php

Lines changed: 0 additions & 148 deletions
This file was deleted.

specs/string-literal/array-var.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the humbug/php-scoper package.
7+
*
8+
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
9+
* Pádraic Brady <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
return [
16+
'meta' => [
17+
'title' => 'Scalar literal assigned as key or value in an array',
18+
// Default values. If not specified will be the one used
19+
'prefix' => 'Humbug',
20+
'whitelist' => [],
21+
],
22+
23+
'String argument: transform into a FQCN and prefix it' => <<<'PHP'
24+
<?php
25+
26+
$x = [
27+
'Symfony\\Component\\Yaml\\Yaml' => 'Symfony\\Component\\Yaml\\Yaml',
28+
'\\Symfony\\Component\\Yaml\\Yaml' => '\\Symfony\\Component\\Yaml\\Yaml',
29+
'Humbug\\Symfony\\Component\\Yaml\\Yaml' => 'Humbug\\Symfony\\Component\\Yaml\\Yaml',
30+
'\\Humbug\\Symfony\\Component\\Yaml\\Yaml' => '\\Humbug\\Symfony\\Component\\Yaml\\Yaml',
31+
];
32+
33+
----
34+
<?php
35+
36+
namespace Humbug;
37+
38+
$x = ['Humbug\\Symfony\\Component\\Yaml\\Yaml' => 'Humbug\\Symfony\\Component\\Yaml\\Yaml', 'Humbug\\Symfony\\Component\\Yaml\\Yaml' => 'Humbug\\Symfony\\Component\\Yaml\\Yaml', 'Humbug\\Symfony\\Component\\Yaml\\Yaml' => 'Humbug\\Symfony\\Component\\Yaml\\Yaml', 'Humbug\\Symfony\\Component\\Yaml\\Yaml' => 'Humbug\\Symfony\\Component\\Yaml\\Yaml'];
39+
40+
PHP
41+
,
42+
];

0 commit comments

Comments
 (0)