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

[RFC] Import types with use statements only for non native types #2166

Closed
phansys opened this issue Sep 1, 2016 · 7 comments
Closed

[RFC] Import types with use statements only for non native types #2166

phansys opened this issue Sep 1, 2016 · 7 comments

Comments

@phansys
Copy link
Contributor

phansys commented Sep 1, 2016

This proposed fixer should perform the following transformation:
ACTUAL:

namespace Example;

use SomeCustomType;
use Some\Qcn;
use RuntimeException;

class SomeClass
{
    public function myMethod()
    {
        throw new RuntimeException();
    }
}

EXPECTED:

namespace Example;

use SomeCustomType;
use Some\Qcn;

class SomeClass
{
    public function myMethod()
    {
        throw new \RuntimeException();
    }
}
@keradus
Copy link
Member

keradus commented Sep 1, 2016

i remember there were request to do the other way around

@keradus
Copy link
Member

keradus commented Sep 1, 2016

👎 for it from me, hard to implement when you want to remove import only for PHP-defined global classes and not user-defined global classes and have it working for all different PHPs, also, it makes execution slower... if one want to have it he could keep it without fixer I would say, if one want to automatically do the importing (Eg only before creating release) the fixer would be nice.

but yeah, let us wait for more comments.

@GrahamCampbell
Copy link
Contributor

Looks doable with little pain, but we might have to be careful when it comes to updating phpdoc.

@GrahamCampbell
Copy link
Contributor

The phpdoc side of things maybe doesn;t make this worth the effort.

@localheinz
Copy link
Member

I was just thinking about it, and wanted to open a ticket, then I ended up here.

I like it!

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
@gharlan
Copy link
Contributor

gharlan commented Oct 11, 2019

#4355 is merged, should we close here?
(the fixer does not differientiate between native/non-native elements, only between global/non-global)

@SpacePossum
Copy link
Contributor

Yeah, looks good to me. Feel free to comment if I missed a detail in the discussion. Thanks again @gharlan for writing the fixer!

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

6 participants