-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDisallowDebugFuncCallRule.php
More file actions
72 lines (65 loc) · 1.94 KB
/
DisallowDebugFuncCallRule.php
File metadata and controls
72 lines (65 loc) · 1.94 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
66
67
68
69
70
71
72
<?php
declare(strict_types=1);
namespace CakeDC\PHPStan\Rule\Debug;
use PhpParser\Node;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\FuncCall;
use PHPStan\Analyser\Scope;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleErrorBuilder;
class DisallowDebugFuncCallRule implements Rule
{
protected const BASIC = 'basic';
protected const RETURNABLE = 'returnable';
/**
* @var array<string, string>
*/
private array $disallowedFunctions = [
'dd' => self::BASIC,
'debug' => self::BASIC,
'debug_print_backtrace' => self::BASIC,
'debug_zval_dump' => self::BASIC,
'pr' => self::BASIC,
'print_r' => self::RETURNABLE,
'stacktrace' => self::BASIC,
'var_dump' => self::BASIC,
'var_export' => self::RETURNABLE,
];
/**
* @inheritDoc
*/
public function getNodeType(): string
{
return FuncCall::class;
}
/**
* @inheritDoc
*/
public function processNode(Node $node, Scope $scope): array
{
assert($node instanceof FuncCall);
if (!$node->name instanceof Node\Name) {
return [];
}
$usedName = $node->name->name;
$name = strtolower($usedName);
if (!isset($this->disallowedFunctions[$name])) {
return [];
}
if ($this->disallowedFunctions[$name] === self::RETURNABLE) {
$arg = $node->getArgs()[1]->value ?? null;
if ($arg instanceof ConstFetch && $arg->name->name === 'true') {
return [];
}
}
return [
RuleErrorBuilder::message(sprintf(
'Use of debug function "%s" is not allowed. %s',
$usedName,
'The use in shipped code is discouraged because they can leak sensitive information or clutter output.',
))
->identifier('cake.debug.debugFuncCallUse')
->build(),
];
}
}