-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDisallowDebugStaticCallRule.php
More file actions
65 lines (58 loc) · 1.68 KB
/
DisallowDebugStaticCallRule.php
File metadata and controls
65 lines (58 loc) · 1.68 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
declare(strict_types=1);
namespace CakeDC\PHPStan\Rule\Debug;
use Cake\Error\Debugger;
use PhpParser\Node;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
class DisallowDebugStaticCallRule implements Rule
{
/**
* @var array<string, array<int, string>>
*/
private array $disallowed = [
//Methods must be lowercased
Debugger::class => ['dump', 'printvar'],
'DebugKit\DebugSql' => ['sql', 'sqld'],
];
/**
* @inheritDoc
*/
public function getNodeType(): string
{
return StaticCall::class;
}
/**
* @inheritDoc
*/
public function processNode(Node $node, Scope $scope): array
{
assert($node instanceof StaticCall);
if (!$node->class instanceof Name || !$node->name instanceof Identifier) {
return [];
}
$className = (string)$node->class;
if (!isset($this->disallowed[$className])) {
return [];
}
$methodUsed = (string)$node->name;
$method = strtolower($methodUsed);
if (!in_array($method, $this->disallowed[$className], true)) {
return [];
}
return [
RuleErrorBuilder::message(sprintf(
'Use of debug method "%s::%s" is not allowed. %s',
$className,
$methodUsed,
'The use in shipped code is discouraged because they can leak sensitive information or clutter output.',
))
->identifier('cake.debug.debugStaticCallUse')
->build(),
];
}
}