-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement AbstractProtectedMethodRule
- Loading branch information
Showing
4 changed files
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Methods; | ||
|
||
use PhpParser\Node; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Node\InClassMethodNode; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
use function sprintf; | ||
|
||
/** @implements Rule<InClassMethodNode> */ | ||
class MethodVisibitiliyInInterfaceRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassMethodNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$method = $node->getMethodReflection(); | ||
|
||
if ($method->isPublic()) { | ||
return []; | ||
} | ||
|
||
$classReflection = $scope->getClassReflection(); | ||
if ($classReflection === null) { | ||
return []; | ||
} | ||
|
||
if (!$classReflection->isInterface()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Method %s::%s() cannot use non-public visibility in interface.', | ||
$method->getDeclaringClass()->getDisplayName(), | ||
$method->getName(), | ||
))->nonIgnorable()->build(), | ||
]; | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
tests/PHPStan/Rules/Methods/MethodVisibitiliyInInterfaceRuleTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
30
tests/PHPStan/Rules/Methods/data/visibility-in-interace.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |