-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Auto import Nextcloud classes in rector runs #48371
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Psalm found more than 20 potential problems in the proposed changes. Check the Files changed tab for more details.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure if I like it. In quite a few places it is nice to use the fqn to avoid mistakes.
Then there is also the case when two classes have the same name where I would actually prefer that both use the fqn and not one fqn and one import.
|
||
$nextcloudDir = dirname(__DIR__); | ||
|
||
class NextcloudNamespaceSkipVoter implements ClassNameImportSkipVoterInterface { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should go into nextcloud/rector then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could, yeah.
The thing is we cannot make it configurable, because it’s added as a service by class name only.
But if it stays general like this we can put in nextcloud/rector, yes. Also we can make it extendable with a protected property to override if needed.
Do you have examples in mind so that we can look if there is a pattern we could apply?
That is quite hard to implement I think. The SkipVoter which avoid use conflict is based on the already imported use list: https://github.com/rectorphp/rector/blob/main/rules/CodingStyle/ClassNameImport/ClassNameImportSkipVoter/UsesClassNameImportSkipVoter.php |
Could we maybe limit this to a few classes like \OCP\Server and \OCP\Util for now? I don't know how many other cases we have where this happens a lot. |
My problem is this rule set: https://github.com/nextcloud-libraries/rector/blob/8061985c44798bac1657ba5bc2d6efa5b10e9089/config/nextcloud-25/nextcloud-25-deprecations.php#L17 |
So, the approach in https://github.com/nextcloud-libraries/rector/pull/11/files failed. @provokateurin What do you think about using the strategy in this PR (auto import from OC/OCA/OCP), but with a manually maitained list of exempted class names? (The list would be something like Plugin,Manager,IManager,Exception). |
Sounds good to me, excluding common duplicate names 👍 |
63099de
to
1ab34b4
Compare
Can you check at the current diff if the exclusion list and the result makes sense to you? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good.
Maybe Node
, File
and Folder
should be excluded as well since they also exist in \OCA\DAV\Connector\Sabre
but this mostly affects the dav app so I'd leave that decision to @ChristophWurst and the likes.
1ab34b4
to
3c0b0b7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clicked into a bunch of random files and can't spot any issues with the imports
We skip all files outside of OC/OCA/OCP from "use" auto-import because it’s not always desirable to import classes when they have generic names, so it should be the developer choice. Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
…ment Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
7daf971
to
1580c86
Compare
Signed-off-by: Côme Chilliet <come.chilliet@nextcloud.com>
Summary
When using rector, fully qualified names are often outputted by the rules, like
\OCP\Server
or\OCP\Http\Client\IClientService
. To avoid that, one can usewithImportNames()
to tell rector to import names with use statements, but then it will import all names.This is not always desirable, for instance
\LDAP\Connection
in user_ldap would be confusing because there is an\OCA\User_LDAP\Connection
class as well.So I added a custom "SkipVoter" which skips auto-import for all classes outside of OC/OCA/OCP.
It may still be a bit confusing for common names like IManager, but I think we can live with that? Or I can add other exceptions if needed.
Note that rector is smart enough to not import two classes with the same name, but it will randomly import one of them and keep the fully qualified name for the other one.
I’m doing that so that when we apply Nextcloud rules set later we do not get fully qualified names everywhere but nice use statements instead.
Checklist