-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add AccessGlobalVariableWithinContextRule (#9)
* Add AccessGlobalVariableWithinContextRule * Each TestAsset in a subfolder to avoid namespace conflicts * Added AccessGlobalVariableWithinContextRule confi for Yii * Updated REAMDE.md * How to test this?
- Loading branch information
Showing
19 changed files
with
298 additions
and
67 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
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,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SlamPhpStan; | ||
|
||
use PhpParser\Node; | ||
use PhpParser\Node\Expr\Variable; | ||
use PHPStan\Analyser\Scope; | ||
use PHPStan\Broker\Broker; | ||
use PHPStan\Rules\Rule; | ||
|
||
final class AccessGlobalVariableWithinContextRule implements Rule | ||
{ | ||
/** | ||
* @var Broker | ||
*/ | ||
private $broker; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $contextBaseClassOrInterface; | ||
|
||
private $globals = [ | ||
'GLOBALS' => true, | ||
'_SERVER' => true, | ||
'_GET' => true, | ||
'_POST' => true, | ||
'_FILES' => true, | ||
'_REQUEST' => true, | ||
'_SESSION' => true, | ||
'_ENV' => true, | ||
'_COOKIE' => true, | ||
'argc' => true, | ||
'argv' => true, | ||
]; | ||
|
||
public function __construct(Broker $broker, string $contextBaseClassOrInterface) | ||
{ | ||
$this->broker = $broker; | ||
$this->contextBaseClassOrInterface = $contextBaseClassOrInterface; | ||
} | ||
|
||
public function getNodeType(): string | ||
{ | ||
return Variable::class; | ||
} | ||
|
||
/** | ||
* @param \PhpParser\Node\Expr\Variable $node | ||
* @param \PHPStan\Analyser\Scope $scope | ||
* | ||
* @return string[] errors | ||
*/ | ||
public function processNode(Node $node, Scope $scope): array | ||
{ | ||
if (! \is_string($node->name)) { | ||
return []; | ||
} | ||
|
||
if (! isset($this->globals[$node->name])) { | ||
return []; | ||
} | ||
|
||
if (! $scope->isInClass()) { | ||
return []; | ||
} | ||
|
||
$classReflection = $scope->getClassReflection(); | ||
if (! $classReflection->isSubclassOf($this->contextBaseClassOrInterface)) { | ||
return []; | ||
} | ||
|
||
$modelBaseClassOrInterface = $this->broker->getClass($this->contextBaseClassOrInterface); | ||
|
||
return [\sprintf('Class %s %s %s and uses $%s: accessing globals in this context is considered an anti-pattern', | ||
$classReflection->getDisplayName(), | ||
$modelBaseClassOrInterface->isInterface() ? 'implements' : 'extends', | ||
$this->contextBaseClassOrInterface, | ||
$node->name | ||
)]; | ||
} | ||
} |
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,66 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SlamPhpStan\Tests; | ||
|
||
use PHPStan\Rules\Rule; | ||
use PHPStan\Testing\RuleTestCase; | ||
use SlamPhpStan\AccessGlobalVariableWithinContextRule; | ||
|
||
/** | ||
* @covers \SlamPhpStan\AccessGlobalVariableWithinContextRule | ||
*/ | ||
final class AccessGlobalVariableWithinContextRuleTest extends RuleTestCase | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $contextBaseClassOrInterface; | ||
|
||
public function __construct($name = null, array $data = [], $dataName = '') | ||
{ | ||
$this->contextBaseClassOrInterface = TestAsset\AccessGlobalVariableWithinContextRule\YiiAlikeActiveRecordInterface::class; | ||
|
||
parent::__construct($name, $data, $dataName); | ||
} | ||
|
||
protected function getRule(): Rule | ||
{ | ||
$broker = $this->createBroker(); | ||
|
||
return new AccessGlobalVariableWithinContextRule($broker, $this->contextBaseClassOrInterface); | ||
} | ||
|
||
public function testRule() | ||
{ | ||
$this->analyse( | ||
[ | ||
__DIR__ . '/TestAsset/AccessGlobalVariableWithinContextRule/fixture.php', | ||
], | ||
[ | ||
[ | ||
\sprintf('Class %s implements %s and uses $_POST: accessing globals in this context is considered an anti-pattern', | ||
TestAsset\AccessGlobalVariableWithinContextRule\ModelAccessingGlobalVariable::class, | ||
$this->contextBaseClassOrInterface | ||
), | ||
8, | ||
], | ||
[ | ||
\sprintf('Class %s implements %s and uses $GLOBALS: accessing globals in this context is considered an anti-pattern', | ||
TestAsset\AccessGlobalVariableWithinContextRule\ModelAccessingGlobalVariable::class, | ||
$this->contextBaseClassOrInterface | ||
), | ||
9, | ||
], | ||
[ | ||
\sprintf('Class %s implements %s and uses $argc: accessing globals in this context is considered an anti-pattern', | ||
TestAsset\AccessGlobalVariableWithinContextRule\ModelAccessingGlobalVariable::class, | ||
$this->contextBaseClassOrInterface | ||
), | ||
10, | ||
], | ||
] | ||
); | ||
} | ||
} |
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
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
67 changes: 67 additions & 0 deletions
67
tests/TestAsset/AccessGlobalVariableWithinContextRule/fixture.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,67 @@ | ||
<?php | ||
|
||
namespace SlamPhpStan\Tests\TestAsset\AccessGlobalVariableWithinContextRule; | ||
|
||
class ModelAccessingGlobalVariable implements YiiAlikeActiveRecordInterface { | ||
public function foo() { | ||
// $bar0 = $_POST; | ||
$bar1 = $_POST; | ||
$bar2 = $GLOBALS; | ||
$argc = []; | ||
|
||
$baz1 = $POST ?? null; | ||
$baz2 = $argcount ?? null; | ||
|
||
$var = 'foo'; | ||
$$var = 1; | ||
${$var} = 2; | ||
} | ||
} | ||
|
||
class AnyOtherClass { | ||
public function foo() { | ||
// $bar0 = $_POST; | ||
$bar1 = $_POST; | ||
$bar2 = $GLOBALS; | ||
$argc = []; | ||
|
||
$baz1 = $POST ?? null; | ||
$baz2 = $argcount ?? null; | ||
|
||
$var = 'foo'; | ||
$$var = 1; | ||
${$var} = 2; | ||
} | ||
} | ||
|
||
trait YiiAppSingletonCallRuleTrait { | ||
public function foo() { | ||
// $bar0 = $_POST; | ||
$bar1 = $_POST; | ||
$bar2 = $GLOBALS; | ||
$argc = []; | ||
|
||
$baz1 = $POST ?? null; | ||
$baz2 = $argcount ?? null; | ||
|
||
$var = 'foo'; | ||
$$var = 1; | ||
${$var} = 2; | ||
} | ||
} | ||
|
||
function YiiAppSingletonCallRuleFunction() { | ||
// $bar0 = $_POST; | ||
$bar1 = $_POST; | ||
$bar2 = $GLOBALS; | ||
$argc = []; | ||
|
||
$baz1 = $POST ?? null; | ||
$baz2 = $argcount ?? null; | ||
|
||
$var = 'foo'; | ||
$$var = 1; | ||
${$var} = 2; | ||
} | ||
|
||
interface YiiAlikeActiveRecordInterface {} |
2 changes: 1 addition & 1 deletion
2
...sStaticPropertyWithinModelContextRule.php → ...ropertyWithinModelContextRule/fixture.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
2 changes: 1 addition & 1 deletion
2
tests/TestAsset/ClassNotation.php → tests/TestAsset/ClassNotation/fixture.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
Oops, something went wrong.