-
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 constant accessed through static::
- Loading branch information
1 parent
d8e8953
commit 270326a
Showing
7 changed files
with
142 additions
and
3 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
60 changes: 60 additions & 0 deletions
60
src/Rules/Classes/AccessPrivateConstantThroughStaticRule.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,60 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Name; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<Node\Expr\ClassConstFetch> | ||
*/ | ||
class AccessPrivateConstantThroughStaticRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\ClassConstFetch::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node->name instanceof Node\Identifier) { | ||
return []; | ||
} | ||
if (!$node->class instanceof Name) { | ||
return []; | ||
} | ||
|
||
$constantName = $node->name->name; | ||
$className = $node->class; | ||
if ($className->toLowerString() !== 'static') { | ||
return []; | ||
} | ||
|
||
$classType = $scope->resolveTypeByName($className); | ||
if (!$classType->hasConstant($constantName)->yes()) { | ||
return []; | ||
} | ||
|
||
$constant = $classType->getConstant($constantName); | ||
if (!$constant->isPrivate()) { | ||
return []; | ||
} | ||
|
||
if ($scope->isInClass() && $scope->getClassReflection()->isFinal()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Unsafe access to private constant %s::%s through static::.', | ||
$constant->getDeclaringClass()->getDisplayName(), | ||
$constantName | ||
))->build(), | ||
]; | ||
} | ||
|
||
} |
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
29 changes: 29 additions & 0 deletions
29
tests/PHPStan/Rules/Classes/AccessPrivateConstantThroughStaticRuleTest.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,29 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Classes; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** | ||
* @extends RuleTestCase<AccessPrivateConstantThroughStaticRule> | ||
*/ | ||
class AccessPrivateConstantThroughStaticRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new AccessPrivateConstantThroughStaticRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/access-private-constant-static.php'], [ | ||
[ | ||
'Unsafe access to private constant AccessPrivateConstantThroughStatic\Foo::FOO 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
29 changes: 29 additions & 0 deletions
29
tests/PHPStan/Rules/Classes/data/access-private-constant-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,29 @@ | ||
<?php | ||
|
||
namespace AccessPrivateConstantThroughStatic; | ||
|
||
class Foo | ||
{ | ||
|
||
private const FOO = 1; | ||
|
||
public function doFoo() | ||
{ | ||
static::FOO; | ||
static::BAR; // reported by a different rule | ||
} | ||
|
||
} | ||
|
||
final class Bar | ||
{ | ||
|
||
private const FOO = 1; | ||
|
||
public function doFoo() | ||
{ | ||
static::FOO; | ||
static::BAR; // reported by a different rule | ||
} | ||
|
||
} |
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