Skip to content

Add an option to skip importing root namespace classes (like \DateTime)#2077

Merged
TomasVotruba merged 7 commits intorectorphp:masterfrom
gnutix:optional-root-namespace-import
Oct 4, 2019
Merged

Add an option to skip importing root namespace classes (like \DateTime)#2077
TomasVotruba merged 7 commits intorectorphp:masterfrom
gnutix:optional-root-namespace-import

Conversation

@gnutix
Copy link
Copy Markdown
Contributor

@gnutix gnutix commented Oct 2, 2019

Trying to fix #1911 (comment)

services:
    Rector\CodingStyle\Rector\Namespace_\ImportFullyQualifiedNamesRector:
        $shouldImportRootNamespaceClasses: false

I still need to prevent changing DocBlock annotations like @var \DateTime... which seems way harder, looking at the code.

@gnutix gnutix changed the title Add an option to skip importing root namespace classes (like \DateTime) WIP: Add an option to skip importing root namespace classes (like \DateTime) Oct 2, 2019
@TomasVotruba
Copy link
Copy Markdown
Member

Great pick!

Definitely start with a test with optional parameter to be on

@gnutix
Copy link
Copy Markdown
Contributor Author

gnutix commented Oct 2, 2019

Test added.

But some help to get the condition $node is NonRootNamespace right would be more than welcome. Currently my condition broke the tests in packages/CodingStyle/tests/Rector/Namespace_/ImportFullyQualifiedNamesRector/NonNamespacedTest.php.

And I'm not sure I'll manage to get the DocBlock right either, as the code is quite complex in there (DocBlockManipulator, DocBlockNameImporter, PhpDocNodeTraverser, etc); I wouldn't know how/where to pass that new option around and where to use it exactly so I wouldn't break half the project.

(and I don't mean that as a way of saying : "please do it all". I'd love to get to learn some of these internals, but some initial guidance might be welcome to find my way around)

@TomasVotruba
Copy link
Copy Markdown
Member

TomasVotruba commented Oct 2, 2019

But some help to get the condition $node is NonRootNamespace right would be more than welcome.

From top of my head, I'd detect \DateTime like:

$name = $this->getName($node);

if (Strings::startsWith($name, '\\')) {
   if (substr_count($name, '\\') === 1) {
        // skip
	    return null;
   }
}

As for docblock, I'm sure there will be another trick, let's deal with them later.

@gnutix
Copy link
Copy Markdown
Contributor Author

gnutix commented Oct 2, 2019

Here's a working implementation. I've not handled DocBlock though, so the test is still failing. Shall you take over from here? :) (I've invited you as a collaborator on my fork, so you can work from there directly if you'd like)

@TomasVotruba
Copy link
Copy Markdown
Member

TomasVotruba commented Oct 3, 2019

As for doblocks, this might be place to add very same check:
https://github.com/rectorphp/rector/blob/master/packages/NodeTypeResolver/src/PhpDoc/NodeAnalyzer/DocBlockNameImporter.php#L125

The parameter should be passed via importNames() method as 3rd argument

… bulletproof (one example of root namespace class, one example of non root namespace class).
@gnutix gnutix changed the title WIP: Add an option to skip importing root namespace classes (like \DateTime) Add an option to skip importing root namespace classes (like \DateTime) Oct 4, 2019
@gnutix gnutix requested a review from TomasVotruba October 4, 2019 10:56
@gnutix
Copy link
Copy Markdown
Contributor Author

gnutix commented Oct 4, 2019

I've finished the implementation. 👍 Would there be some documentation to write for this new option ?

@TomasVotruba
Copy link
Copy Markdown
Member

Feature looks ready, good job 👍

Would there be some documentation to write for this new option ?

The documentation is generated from code samples, so adding one ConfiguredCodeSample() in getDefinition() will be enough

@TomasVotruba
Copy link
Copy Markdown
Member

I've finished the implementation. +1 Would there be some documentation to write for this new option ?

Feel free to send that in next PR. I'm merging this, since feature is complete

@TomasVotruba TomasVotruba merged commit 7e16b2f into rectorphp:master Oct 4, 2019
@TomasVotruba TomasVotruba deleted the optional-root-namespace-import branch October 4, 2019 12:33
@gnutix
Copy link
Copy Markdown
Contributor Author

gnutix commented Oct 4, 2019

Not sure how I can describe the new parameter within new CodeSample(...) as this only provides before/after code examples - but not the constructor parameters involved to achieve such results.

@TomasVotruba
Copy link
Copy Markdown
Member

ConfiguredCodeSample

@gnutix
Copy link
Copy Markdown
Contributor Author

gnutix commented Oct 4, 2019

Sorry, I should have read more attentively! 🙄

@TomasVotruba
Copy link
Copy Markdown
Member

No troubles 👍

@gnutix
Copy link
Copy Markdown
Contributor Author

gnutix commented Dec 11, 2019

Note : this option was changed into parameters.import_short_classes: false

@TomasVotruba
Copy link
Copy Markdown
Member

Could you add that to README as well?

@Aerendir
Copy link
Copy Markdown
Contributor

@TomasVotruba I documented it

TomasVotruba added a commit that referenced this pull request Apr 15, 2022
rectorphp/rector-src@8be11db [Naming] Skip used by trait on RenamePropertyToMatchTypeRector (#2077)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants