Skip to content

Commit

Permalink
Implement MethodVisibitiliyInInterfaceRule
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Nov 21, 2023
1 parent 6e14549 commit 2e3b89a
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 36 deletions.
2 changes: 1 addition & 1 deletion conf/config.level0.neon
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@ rules:
- PHPStan\Rules\Keywords\ContinueBreakInLoopRule
- PHPStan\Rules\Methods\AbstractMethodInNonAbstractClassRule
- PHPStan\Rules\Methods\AbstractPrivateMethodRule
- PHPStan\Rules\Methods\AbstractProtectedMethodRule
- PHPStan\Rules\Methods\CallMethodsRule
- PHPStan\Rules\Methods\CallStaticMethodsRule
- PHPStan\Rules\Methods\ConstructorReturnTypeRule
- PHPStan\Rules\Methods\ExistingClassesInTypehintsRule
- PHPStan\Rules\Methods\FinalPrivateMethodRule
- PHPStan\Rules\Methods\MethodCallableRule
- PHPStan\Rules\Methods\MethodVisibitiliyInInterfaceRule
- PHPStan\Rules\Methods\MissingMethodImplementationRule
- PHPStan\Rules\Methods\MethodAttributesRule
- PHPStan\Rules\Methods\StaticMethodCallableRule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
use function sprintf;

/** @implements Rule<InClassMethodNode> */
class AbstractProtectedMethodRule implements Rule
class MethodVisibitiliyInInterfaceRule implements Rule
{

public function getNodeType(): string
Expand All @@ -22,11 +22,7 @@ public function processNode(Node $node, Scope $scope): array
{
$method = $node->getMethodReflection();

if ($method->isPrivate() || $method->isPublic()) {
return [];
}

if (!$method->isAbstract()->yes()) {
if ($method->isPublic()) {
return [];
}

Expand All @@ -41,7 +37,7 @@ public function processNode(Node $node, Scope $scope): array

return [
RuleErrorBuilder::message(sprintf(
'Protected method %s::%s() cannot be abstract.',
'Method %s::%s() cannot use non-public visibility in interface.',
$method->getDeclaringClass()->getDisplayName(),
$method->getName(),
))->nonIgnorable()->build(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected function getRule(): Rule

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/abstract-methods.php'], [
$this->analyse([__DIR__ . '/data/abstract-private-method.php'], [
[
'Private method AbstractMethods\HelloWorld::sayPrivate() cannot be abstract.',
12,
Expand Down
27 changes: 0 additions & 27 deletions tests/PHPStan/Rules/Methods/AbstractProtectedMethodRuleTest.php

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php declare(strict_types = 1);

namespace PHPStan\Rules\Methods;

use PHPStan\Rules\Rule;
use PHPStan\Testing\RuleTestCase;

/** @extends RuleTestCase<MethodVisibitiliyInInterfaceRule> */
class MethodVisibitiliyInInterfaceRuleTest extends RuleTestCase
{

protected function getRule(): Rule
{
return new MethodVisibitiliyInInterfaceRule();
}

public function testRule(): void
{
$this->analyse([__DIR__ . '/data/visibility-in-interace.php'], [
[
'Method VisibilityInInterface\FooInterface::sayPrivate() cannot use non-public visibility in interface.',
7,
],
[
'Method VisibilityInInterface\FooInterface::sayProtected() cannot use non-public visibility in interface.',
8,
],
]);
}

}
30 changes: 30 additions & 0 deletions tests/PHPStan/Rules/Methods/data/visibility-in-interace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace VisibilityInInterface;

interface FooInterface
{
private function sayPrivate() : void;
protected function sayProtected() : void;
public function sayPublic() : void;

function noVisibility() : void;
}

trait FooTrait
{
private function sayPrivate() : void {}
protected function sayProtected() : void {}
public function sayPublic() : void {}

function noVisibility() : void {}
}

abstract class AbstractFoo
{
abstract private function sayPrivate() : void;
abstract protected function sayProtected() : void;
abstract public function sayPublic() : void;

abstract function noVisibility() : void;
}

0 comments on commit 2e3b89a

Please sign in to comment.