Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prefix strings #164

Merged
merged 5 commits into from
Feb 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ potentially very difficult to debug due to dissimilar or unsupported package ver
- [Step 3: Build, test, and cleanup](#step-3-build-test-and-cleanup)
- [Limitations](#limitations)
- [PSR-0 support](#psr-0-support)
- [String values](#string-values)
- [Contributing](#contributing)
- [Credits](#credits)

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


### String values

PHP-Scoper tries whenever possible to prefix strings as well:

```php
class_exists('Acme\Foo');

// Will be prefixed into:

\class_exists('Humbug\Acme\Foo');
```

PHP-Scoper uses a regex to determine if the string is a class name that must be prefixed. But there is
bound to have confusing cases. For example:

- If you have a plain string `'Acme\Foo'` which has nothing to do with a class, PHP-Parser will not be
able to tell and will prefix it
- Classes belonging to the global scope: `'Acme_Foo'`, because there is no way to know if it is a class
name or a random string.


## Contributing

[Contribution Guide](CONTRIBUTING.md)
Expand Down
130 changes: 0 additions & 130 deletions specs/func-args/func.php

This file was deleted.

148 changes: 0 additions & 148 deletions specs/func-args/whitelisted-func.php

This file was deleted.

42 changes: 42 additions & 0 deletions specs/string-literal/array-var.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

/*
* This file is part of the humbug/php-scoper package.
*
* Copyright (c) 2017 Théo FIDRY <[email protected]>,
* Pádraic Brady <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

return [
'meta' => [
'title' => 'Scalar literal assigned as key or value in an array',
// Default values. If not specified will be the one used
'prefix' => 'Humbug',
'whitelist' => [],
],

'String argument: transform into a FQCN and prefix it' => <<<'PHP'
<?php

$x = [
'Symfony\\Component\\Yaml\\Yaml' => 'Symfony\\Component\\Yaml\\Yaml',
'\\Symfony\\Component\\Yaml\\Yaml' => '\\Symfony\\Component\\Yaml\\Yaml',
'Humbug\\Symfony\\Component\\Yaml\\Yaml' => 'Humbug\\Symfony\\Component\\Yaml\\Yaml',
'\\Humbug\\Symfony\\Component\\Yaml\\Yaml' => '\\Humbug\\Symfony\\Component\\Yaml\\Yaml',
];

----
<?php

namespace Humbug;

$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'];

PHP
,
];
Loading