-
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 property accessed through static::
- Loading branch information
1 parent
bad2607
commit d8e8953
Showing
6 changed files
with
150 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
63 changes: 63 additions & 0 deletions
63
src/Rules/Properties/AccessPrivatePropertyThroughStaticRule.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,63 @@ | ||
<?php declare(strict_types = 1); | ||
|
||
namespace PHPStan\Rules\Properties; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Name; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Rules\Rule; | ||
use PHPStan\Rules\RuleErrorBuilder; | ||
|
||
/** | ||
* @implements Rule<Node\Expr\StaticPropertyFetch> | ||
*/ | ||
class AccessPrivatePropertyThroughStaticRule implements Rule | ||
{ | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Node\Expr\StaticPropertyFetch::class; | ||
} | ||
|
||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (!$node->name instanceof Node\VarLikeIdentifier) { | ||
return []; | ||
} | ||
if (!$node->class instanceof Name) { | ||
return []; | ||
} | ||
|
||
$propertyName = $node->name->name; | ||
$className = $node->class; | ||
if ($className->toLowerString() !== 'static') { | ||
return []; | ||
} | ||
|
||
$classType = $scope->resolveTypeByName($className); | ||
if (!$classType->hasProperty($propertyName)->yes()) { | ||
return []; | ||
} | ||
|
||
$property = $classType->getProperty($propertyName, $scope); | ||
if (!$property->isPrivate()) { | ||
return []; | ||
} | ||
if (!$property->isStatic()) { | ||
return []; | ||
} | ||
|
||
if ($scope->isInClass() && $scope->getClassReflection()->isFinal()) { | ||
return []; | ||
} | ||
|
||
return [ | ||
RuleErrorBuilder::message(sprintf( | ||
'Unsafe access to private property %s::$%s through static::.', | ||
$property->getDeclaringClass()->getDisplayName(), | ||
$propertyName | ||
))->build(), | ||
]; | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
tests/PHPStan/Rules/Properties/AccessPrivatePropertyThroughStaticRuleTest.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 PHPStan\Rules\Properties; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
|
||
/** @extends RuleTestCase<AccessPrivatePropertyThroughStaticRule> */ | ||
class AccessPrivatePropertyThroughStaticRuleTest extends RuleTestCase | ||
{ | ||
|
||
protected function getRule(): Rule | ||
{ | ||
return new AccessPrivatePropertyThroughStaticRule(); | ||
} | ||
|
||
public function testRule(): void | ||
{ | ||
$this->analyse([__DIR__ . '/data/access-private-property-static.php'], [ | ||
[ | ||
'Unsafe access to private property AccessPrivatePropertyThroughStatic\Foo::$foo through static::.', | ||
13, | ||
], | ||
]); | ||
} | ||
|
||
} |
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
33 changes: 33 additions & 0 deletions
33
tests/PHPStan/Rules/Properties/data/access-private-property-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,33 @@ | ||
<?php | ||
|
||
namespace AccessPrivatePropertyThroughStatic; | ||
|
||
class Foo | ||
{ | ||
|
||
private static $foo; | ||
private $bar; | ||
|
||
public function doBar() | ||
{ | ||
static::$foo; | ||
static::$bar; // reported by different rule | ||
static::$nonexistent; // reported by different rule | ||
} | ||
|
||
} | ||
|
||
final class Bar | ||
{ | ||
|
||
private static $foo; | ||
private $bar; | ||
|
||
public function doBar() | ||
{ | ||
static::$foo; | ||
static::$bar; | ||
static::$nonexistent; | ||
} | ||
|
||
} |
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