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

Feature request: add missing class method visibility #7098

Closed
Firehed opened this issue Apr 6, 2022 · 3 comments · Fixed by rectorphp/rector-src#2022
Closed

Feature request: add missing class method visibility #7098

Firehed opened this issue Apr 6, 2022 · 3 comments · Fixed by rectorphp/rector-src#2022
Labels

Comments

@Firehed
Copy link

Firehed commented Apr 6, 2022

Feature Request

Nearly identical to Rector\Php71\Rector\ClassConst\PublicConstantVisibilityRector: it would be a great addition to have a rule to add missing visibility declarations to class methods.

Note: I was able to achieve this locally by changing the aforementioned class's getNodeTypes() to include \PhpParser\Node\Stmt\ClassMethod::class. That alone seemed to be sufficient, and I didn't get any false-positives with global functions or closures.

Diff

 class Foo
 {
-    function bar()
+    public function bar()
     {
@Firehed Firehed added the feature label Apr 6, 2022
@TomasVotruba
Copy link
Member

TomasVotruba commented Apr 6, 2022

Hi,
this could be quite easy to implement.
Just add public modifier to ClassMethod in case it's null.

Would you like to contribute this rule?

@Firehed
Copy link
Author

Firehed commented Apr 6, 2022

I'm happy to look into it!

A couple questions before getting started:

New rule or extend existing one to have broader scope?

If new:

  • Any naming preference for the new class?
  • Should it extend the existing rule (currently declared final), copy-paste most of the internals, or compose the existing one in as a proxy of sorts?

If existing:

  • Should the behavior be configurable? Should it default to the current, constant-only scope?

Thanks!

@TomasVotruba
Copy link
Member

TomasVotruba commented Apr 6, 2022

New rule that extends AbstractRector class would be the way to go.
Check this docs on how to add a rule: https://github.com/rectorphp/rector/blob/main/docs/create_own_rule.md

Name like "ExplicitPublicClassMethodRector" sounds clear.

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

Successfully merging a pull request may close this issue.

2 participants