-
Notifications
You must be signed in to change notification settings - Fork 476
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
114 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,55 @@ | ||
<?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 AbstractPrivateMethodRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return InClassMethodNode::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
$method = $node->getMethodReflection(); | ||
|
||
if (!$method->isPrivate()) { | ||
return []; | ||
} | ||
|
||
if (!$method->isAbstract()->yes()) { | ||
return []; | ||
} | ||
|
||
if ($scope->isInTrait()) { | ||
return []; | ||
} | ||
|
||
$classReflection = $scope->getClassReflection(); | ||
if ($classReflection === null) { | ||
return []; | ||
} | ||
|
||
if (!$classReflection->isAbstract() && !$classReflection->isInterface()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Private method %s::%s() cannot be abstract.', | ||
$method->getDeclaringClass()->getDisplayName(), | ||
$method->getName(), | ||
))->nonIgnorable()->build(), | ||
]; | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
tests/PHPStan/Rules/Methods/AbstractPrivateMethodRuleTest.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<AbstractPrivateMethodRule> */ | ||
class AbstractPrivateMethodRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new AbstractPrivateMethodRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/abstract-private-method.php'], [ | ||
[ | ||
'Private method PrivateAbstractMethod\HelloWorld::sayPrivate() cannot be abstract.', | ||
12, | ||
], | ||
[ | ||
'Private method PrivateAbstractMethod\fooInterface::sayPrivate() cannot be abstract.', | ||
24, | ||
], | ||
]); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
tests/PHPStan/Rules/Methods/data/abstract-private-method.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,27 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PrivateAbstractMethod; | ||
|
||
abstract class HelloWorld | ||
{ | ||
public function doFoo(): void { | ||
$this->sayHello(); | ||
$this->sayWorld(); | ||
} | ||
|
||
abstract private function sayPrivate() : void; | ||
abstract protected function sayProtected() : void; | ||
abstract public function sayPublic() : void; | ||
} | ||
|
||
trait fooTrait{ | ||
abstract private function sayPrivate() : void; | ||
abstract protected function sayProtected() : void; | ||
abstract public function sayPublic() : void; | ||
} | ||
|
||
interface fooInterface { | ||
abstract private function sayPrivate() : void; | ||
abstract protected function sayProtected() : void; | ||
abstract public function sayPublic() : void; | ||
} |