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

Add no_root_imports fixer #4347

Closed
Bilge opened this issue Mar 22, 2019 · 1 comment
Closed

Add no_root_imports fixer #4347

Bilge opened this issue Mar 22, 2019 · 1 comment

Comments

@Bilge
Copy link

Bilge commented Mar 22, 2019

With this fixer, an import such as use PDO; would be removed and all references to PDO in that file would be replaced with \PDO. Root imports are those which do not contain a backslash (\) character, excluding the first. The following are root imports:

  • PDO
  • \PDO

The following are not root imports:

  • Foo\Bar.

The rationale behind this fixer is to make it clear, when reading the code, whether the built-in implementation is used or a custom implementation. That is, when code follows the no root imports style, we can be confident that code occurences of \PDO refer to the standard implementation, and PDO refers to a custom implementation, because PHP built-in classes are not namespaced.

SpacePossum added a commit that referenced this issue Oct 11, 2019
This PR was squashed before being merged into the 2.16-dev branch (closes #4355).

Discussion
----------

GlobalNamespaceImportFixer - Introduction

* closes #1309
* refs #2166 (this pr does not differientiate between native/non-native elements, only between global/non-global)
* closes #2739
* closes #4347

---

The fixer can import global classes/functions/constants:

Input:

```php
<?php

namespace Foo;

if (\count($x)) {
    /** @var \DateTimeImmutable $d */
    $d = new \DateTimeImmutable();
    $p = \M_PI;
}
```

Output:

```php
<?php

namespace Foo;
use DateTimeImmutable;
use function count;
use const M_PI;

if (count($x)) {
    /** @var DateTimeImmutable $d */
    $d = new DateTimeImmutable();
    $p = M_PI;
}
```

Global functions/constants without leading `\` are not imported. The slash can be added by `Native(Constant|Function)InvocationFixer` before.

The fixer can also do the reverse fix, adding the backslash to imported global classes/functions/constants. But the fixer does not remove the imports, this can be done by `NoUnusedImportsFixer`.

Commits
-------

41fe1f4 GlobalNamespaceImportFixer - Introduction
@SpacePossum
Copy link
Contributor

done in #4355

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants