-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bleeding edge - private method called through static::
- Loading branch information
1 parent
716fe52
commit bad2607
Showing
8 changed files
with
154 additions
and
1 deletion.
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
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,61 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Methods; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\StaticCall; | ||
use PhpParser\Node\Name; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<StaticCall> | ||
*/ | ||
class CallPrivateMethodThroughStaticRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return StaticCall::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node->name instanceof Node\Identifier) { | ||
return []; | ||
} | ||
if (!$node->class instanceof Name) { | ||
return []; | ||
} | ||
|
||
$methodName = $node->name->name; | ||
$className = $node->class; | ||
if ($className->toLowerString() !== 'static') { | ||
return []; | ||
} | ||
|
||
$classType = $scope->resolveTypeByName($className); | ||
if (!$classType->hasMethod($methodName)->yes()) { | ||
return []; | ||
} | ||
|
||
$method = $classType->getMethod($methodName, $scope); | ||
if (!$method->isPrivate()) { | ||
return []; | ||
} | ||
|
||
if ($scope->isInClass() && $scope->getClassReflection()->isFinal()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Unsafe call to private method %s::%s() through static::.', | ||
$method->getDeclaringClass()->getDisplayName(), | ||
$method->getName() | ||
))->build(), | ||
]; | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
tests/PHPStan/Rules/Methods/CallPrivateMethodThroughStaticRuleTest.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,28 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Methods; | ||
|
||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<CallPrivateMethodThroughStaticRule> | ||
*/ | ||
class CallPrivateMethodThroughStaticRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): \PHPStan\Rules\Rule | ||
{ | ||
return new CallPrivateMethodThroughStaticRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/call-private-method-static.php'], [ | ||
[ | ||
'Unsafe call to private method CallPrivateMethodThroughStatic\Foo::doBar() through static::.', | ||
12, | ||
], | ||
]); | ||
} | ||
|
||
} |
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
37 changes: 37 additions & 0 deletions
37
tests/PHPStan/Rules/Methods/data/call-private-method-static.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,37 @@ | ||
<?php | ||
|
||
namespace CallPrivateMethodThroughStatic; | ||
|
||
class Foo | ||
{ | ||
|
||
public function doFoo() | ||
{ | ||
static::doFoo(); | ||
static::nonexistent(); | ||
static::doBar(); | ||
} | ||
|
||
private function doBar() | ||
{ | ||
|
||
} | ||
|
||
} | ||
|
||
final class Bar | ||
{ | ||
|
||
public function doFoo() | ||
{ | ||
static::doFoo(); | ||
static::nonexistent(); | ||
static::doBar(); | ||
} | ||
|
||
private function doBar() | ||
{ | ||
|
||
} | ||
|
||
} |
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