1
0
mirror of https://github.com/danog/psalm.git synced 2024-11-26 20:34:47 +01:00

Fix #4656 - separate UnusedConstructor from UnusedMethod

This commit is contained in:
Matt Brown 2020-11-22 11:48:05 -05:00
parent 97f0a78ac1
commit 2c77424e16
5 changed files with 45 additions and 7 deletions

View File

@ -426,6 +426,7 @@
<xs:element name="UnsafeInstantiation" type="IssueHandlerType" minOccurs="0" />
<xs:element name="UnusedClass" type="ClassIssueHandlerType" minOccurs="0" />
<xs:element name="UnusedClosureParam" type="IssueHandlerType" minOccurs="0" />
<xs:element name="UnusedConstructor" type="MethodIssueHandlerType" minOccurs="0" />
<xs:element name="UnusedFunctionCall" type="FunctionIssueHandlerType" minOccurs="0" />
<xs:element name="UnusedMethod" type="MethodIssueHandlerType" minOccurs="0" />
<xs:element name="UnusedMethodCall" type="MethodIssueHandlerType" minOccurs="0" />

View File

@ -0,0 +1,14 @@
# UnusedConstructor
Emitted when `--find-dead-code` is turned on and Psalm cannot find any uses of a given private constructor or function
```php
<?php
class A {
private function __construct() {}
public static function createInstance() : void {}
}
A::createInstance();
```

View File

@ -1557,6 +1557,10 @@ class Config
return 'UnusedParam';
}
if ($issue_type === 'UnusedConstructor') {
return 'UnusedMethod';
}
if ($issue_type === 'StringIncrement') {
return 'InvalidOperand';
}

View File

@ -26,6 +26,7 @@ use Psalm\Issue\PossiblyUnusedMethod;
use Psalm\Issue\PossiblyUnusedParam;
use Psalm\Issue\PossiblyUnusedProperty;
use Psalm\Issue\UnusedClass;
use Psalm\Issue\UnusedConstructor;
use Psalm\Issue\UnusedMethod;
use Psalm\Issue\UnusedProperty;
use Psalm\IssueBuffer;
@ -1659,13 +1660,23 @@ class ClassLikes
strtolower($classlike_storage->name . '::')
) || $codebase->analyzer->hasMixedMemberName($method_name);
$issue = new UnusedMethod(
'Cannot find ' . ($has_variable_calls ? 'explicit' : 'any')
. ' calls to private method ' . $method_id
. ($has_variable_calls ? ' (but did find some potential callers)' : ''),
$method_location,
$method_id
);
if ($method_name === '__construct') {
$issue = new UnusedConstructor(
'Cannot find ' . ($has_variable_calls ? 'explicit' : 'any')
. ' calls to private constructor ' . $method_id
. ($has_variable_calls ? ' (but did find some potential callers)' : ''),
$method_location,
$method_id
);
} else {
$issue = new UnusedMethod(
'Cannot find ' . ($has_variable_calls ? 'explicit' : 'any')
. ' calls to private method ' . $method_id
. ($has_variable_calls ? ' (but did find some potential callers)' : ''),
$method_location,
$method_id
);
}
if ($codebase->alter_code) {
if ($method_storage->stmt_location

View File

@ -0,0 +1,8 @@
<?php
namespace Psalm\Issue;
class UnusedConstructor extends MethodIssue
{
public const ERROR_LEVEL = -2;
public const SHORTCODE = 258;
}