1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-21 21:31:13 +01:00

Add dead code detection for possibly unused public methods

This commit is contained in:
Matt Brown 2017-02-23 19:52:23 -05:00
parent 0049e4deb4
commit 182f715b62
4 changed files with 30 additions and 10 deletions

View File

@ -149,6 +149,7 @@
<xs:element name="PossiblyNullPropertyFetch" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyNullReference" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyUnusedVariable" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PossiblyUnusedMethod" type="IssueHandlerType" minOccurs="0" />
<xs:element name="PropertyNotSetInConstructor" type="IssueHandlerType" minOccurs="0" />
<xs:element name="ReferenceConstraintViolation" type="IssueHandlerType" minOccurs="0" />
<xs:element name="TooFewArguments" type="IssueHandlerType" minOccurs="0" />

View File

@ -5,6 +5,7 @@ use Psalm\Config;
use Psalm\Context;
use Psalm\Exception;
use Psalm\IssueBuffer;
use Psalm\Issue\PossiblyUnusedMethod;
use Psalm\Issue\UnusedClass;
use Psalm\Issue\UnusedMethod;
use Psalm\Provider\CacheProvider;
@ -362,19 +363,29 @@ class ProjectChecker
{
foreach ($classlike_storage->methods as $method_name => $method_storage) {
if ($method_storage->references === 0 &&
$method_storage->visibility !== ClassLikeChecker::VISIBILITY_PUBLIC &&
!$classlike_storage->overridden_method_ids[$method_name] &&
$method_storage->location
) {
$method_id = $classlike_storage->name . '::' . $method_name;
$method_id = $classlike_storage->name . '::' . $method_storage->cased_name;
if (IssueBuffer::accepts(
new UnusedMethod(
'Method ' . $method_id . ' is never used',
$method_storage->location
)
)) {
// fall through
if ($method_storage->visibility === ClassLikeChecker::VISIBILITY_PUBLIC) {
if (IssueBuffer::accepts(
new PossiblyUnusedMethod(
'Cannot find public calls to method ' . $method_id,
$method_storage->location
)
)) {
// fall through
}
} else {
if (IssueBuffer::accepts(
new UnusedMethod(
'Method ' . $method_id . ' is never used',
$method_storage->location
)
)) {
// fall through
}
}
}
}

View File

@ -0,0 +1,6 @@
<?php
namespace Psalm\Issue;
class PossiblyUnusedMethod extends CodeError
{
}

View File

@ -136,7 +136,9 @@ class UnusedCodeTest extends PHPUnit_Framework_TestCase
}
/**
* @return void
* @expectedException \Psalm\Exception\CodeException
* @expectedExceptionMessage PossiblyUnusedMethod
* @return void
*/
public function testPublicUnusedMethod()
{