forked from phpstan/phpstan-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThisInGlobalStatementRule.php
More file actions
48 lines (40 loc) · 950 Bytes
/
ThisInGlobalStatementRule.php
File metadata and controls
48 lines (40 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
<?php declare(strict_types = 1);
namespace PHPStan\Rules\Variables;
use PhpParser\Node;
use PhpParser\Node\Expr\Variable;
use PHPStan\Analyser\Scope;
use PHPStan\DependencyInjection\RegisteredRule;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
use function is_string;
/**
* @implements Rule<Node\Stmt\Global_>
*/
#[RegisteredRule(level: 0)]
final class ThisInGlobalStatementRule implements Rule
{
public function getNodeType(): string
{
return Node\Stmt\Global_::class;
}
public function processNode(Node $node, Scope $scope): array
{
$errors = [];
foreach ($node->vars as $var) {
if (!$var instanceof Variable) {
continue;
}
if (!is_string($var->name)) {
continue;
}
if ($var->name !== 'this') {
continue;
}
$errors[] = RuleErrorBuilder::message('Cannot use $this as global variable.')
->identifier('global.this')
->nonIgnorable()
->build();
}
return $errors;
}
}