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

UnusedMethod for private constructor #4656

Closed
theofidry opened this issue Nov 22, 2020 · 3 comments
Closed

UnusedMethod for private constructor #4656

theofidry opened this issue Nov 22, 2020 · 3 comments

Comments

@theofidry
Copy link
Contributor

If you have the following class:

final class Foo {
  private function __construct() {}

  public static function doSomething(): void { /* ... */ }
}

Then Psalm will detect UnusedMethod - 4:20 - Cannot find any calls to private method Foo::__construct (see https://psalm.dev/r/9641f1dbd6).

IMO this is a false-positive. Unlike a regular private method which if unused is dead code, a private constructor also make the class non-instantiable by "conventional" means, hence even if unused is not dead code.

@psalm-github-bot
Copy link

I found these snippets:

https://psalm.dev/r/9641f1dbd6
<?php

final class Foo {
  private function __construct() {}

  public static function doSomething(): void { /* ... */ }
}

Foo::doSomething();
Psalm output (using commit 60b3086):

INFO: UnusedMethod - 4:20 - Cannot find any calls to private method Foo::__construct

@muglug muglug closed this as completed in 2c77424 Nov 22, 2020
@muglug
Copy link
Collaborator

muglug commented Nov 22, 2020

a private constructor also make the class non-instantiable by "conventional" means

I've separated this out into a separate issue that can be suppressed if you need

@theofidry
Copy link
Contributor Author

theofidry commented Nov 22, 2020 via email

danog pushed a commit to danog/psalm that referenced this issue Jan 29, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants