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

Detect unused protected methods #144

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/PublicClassMethodMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public function isUsedByParentClassOrInterface(ClassReflection $classReflection,

public function shouldSkipClassMethod(ClassMethod $classMethod): bool
{
if (! $classMethod->isPublic()) {
if ($classMethod->isPrivate()) {
return true;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicClassMethodRule\Fixture;

final class SkipLocallyUsedProtectedMethod
{
protected function runHere()
{
}

private function run()
{
$this->runHere();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicClassMethodRule\Fixture;

final class StaticProtectedMethod
{
protected static function runHere()
{
}

protected static function anotherMethod()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicClassMethodRule\Source;

use TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicClassMethodRule\Fixture\StaticProtectedMethod;
use TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicClassMethodRule\Fixture\StaticPublicMethod;

final class StaticProtectedCalls
{
private function go()
{
return StaticProtectedMethod::anotherMethod();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use TomasVotruba\UnusedPublic\Enum\RuleTips;
use TomasVotruba\UnusedPublic\Rules\UnusedPublicClassMethodRule;
use TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicClassMethodRule\Fixture\Interface\InterfaceWithExtraMethod;
use TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicClassMethodRule\Fixture\StaticProtectedMethod;
use TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicClassMethodRule\Fixture\StaticPublicMethod;
use TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicClassMethodRule\Fixture\Tests\MethodForTests;
use TomasVotruba\UnusedPublic\Tests\Rules\UnusedPublicClassMethodRule\Fixture\UsedInTestCaseOnly;
Expand All @@ -35,6 +36,7 @@ final class UnusedPublicClassMethodRuleTest extends RuleTestCase
#[DataProvider('provideData')]
#[DataProvider('provideDataTests')]
#[DataProvider('provideDataSymfony')]
#[DataProvider('provideProtectedMethodData')]
public function testRule(array $filePaths, array $expectedErrorMessagesWithLines): void
{
$this->analyse($filePaths, $expectedErrorMessagesWithLines);
Expand Down Expand Up @@ -76,6 +78,17 @@ public static function provideDataSymfony(): Iterator
], []];
}

public static function provideProtectedMethodData(): Iterator
{
yield [[__DIR__ . '/Fixture/SkipLocallyUsedProtectedMethod.php'], []];

$errorMessage = sprintf(UnusedPublicClassMethodRule::ERROR_MESSAGE, StaticProtectedMethod::class, 'runHere');
yield [[
__DIR__ . '/Source/StaticProtectedCalls.php',
__DIR__ . '/Fixture/StaticProtectedMethod.php',
], [[$errorMessage, 9, RuleTips::SOLUTION_MESSAGE]]];
}

public static function provideData(): Iterator
{
yield [[__DIR__ . '/Fixture/SkipLocallyUsedPublicMethod.php'], []];
Expand Down
Loading