Skip to content

Commit

Permalink
feature #4355 GlobalNamespaceImportFixer - Introduction (gharlan)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SpacePossum committed Oct 11, 2019
2 parents c0d9d5a + 41fe1f4 commit 18e95e4
Show file tree
Hide file tree
Showing 12 changed files with 2,084 additions and 0 deletions.
13 changes: 13 additions & 0 deletions README.rst
Expand Up @@ -743,6 +743,19 @@ Choose from the list of available rules:
- ``annotations`` (``array``): list of annotations to remove, e.g. ``["author"]``;
defaults to ``[]``

* **global_namespace_import**

Imports or fully qualifies global classes/functions/constants.

Configuration options:

- ``import_classes`` (``false``, ``null``, ``true``): whether to import, not import or
ignore global classes; defaults to ``true``
- ``import_constants`` (``false``, ``null``, ``true``): whether to import, not import or
ignore global constants; defaults to ``null``
- ``import_functions`` (``false``, ``null``, ``true``): whether to import, not import or
ignore global functions; defaults to ``null``

* **hash_to_slash_comment**

Single line comments should use double slashes ``//`` and not hash ``#``.
Expand Down
9 changes: 9 additions & 0 deletions src/Fixer/ConstantNotation/NativeConstantInvocationFixer.php
Expand Up @@ -92,6 +92,15 @@ public function getDefinition()
);
}

/**
* {@inheritdoc}
*/
public function getPriority()
{
// must be run before GlobalNamespaceImportFixer
return 10;
}

/**
* {@inheritdoc}
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Fixer/FunctionNotation/NativeFunctionInvocationFixer.php
Expand Up @@ -160,6 +160,15 @@ function baz($options)
);
}

/**
* {@inheritdoc}
*/
public function getPriority()
{
// must be run before GlobalNamespaceImportFixer
return 10;
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit 18e95e4

Please sign in to comment.