-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDump.php
More file actions
76 lines (64 loc) · 1.96 KB
/
Dump.php
File metadata and controls
76 lines (64 loc) · 1.96 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
73
74
75
76
<?php
namespace Drew\DebugStatementsFixers;
use PhpCsFixer\AbstractFunctionReferenceFixer;
use PhpCsFixer\FixerDefinition\CodeSample;
use PhpCsFixer\FixerDefinition\FixerDefinition;
use PhpCsFixer\FixerDefinition\FixerDefinitionInterface;
use PhpCsFixer\Tokenizer\Tokens;
/**
* @author Andrew Kovalyov <andrew.kovalyoff@gmail.com>
*/
final class Dump extends AbstractFunctionReferenceFixer
{
/**
* @var array
*/
private $functions = array('dump', 'var_dump', 'dd');
/**
* {@inheritdoc}
*/
public function isCandidate(Tokens $tokens): bool
{
return $tokens->isTokenKindFound(T_STRING);
}
/**
* {@inheritdoc}
*/
public function getName(): string
{
return 'RemoveDebugStatements/dump';
}
/**
* {@inheritdoc}
*/
public function getDefinition(): FixerDefinitionInterface
{
return new FixerDefinition(
'Removes dump/var_dump statements, which shouldn\'t be in production ever.',
array(new CodeSample("<?php\nvar_dump(false);\n")),
null,
'Risky when functions ' . implode(', ', $this->functions) . ' are redefined.'
);
}
/**
* {@inheritdoc}
*/
protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
{
foreach ($this->functions as $function) {
$currIndex = 0;
while (null !== $currIndex) {
$matches = $this->find($function, $tokens, $currIndex);
if (null === $matches) {
break;
}
$funcStart = $tokens->getPrevNonWhitespace($matches[0]);
if ($tokens[$funcStart]->isGivenKind(T_NEW) || $tokens[$funcStart]->isGivenKind(T_FUNCTION)) {
break;
}
$funcEnd = $tokens->getNextTokenOfKind($matches[1], array(';'));
$tokens->clearRange($funcStart + 1, $funcEnd);
}
}
}
}