This repository has been archived by the owner on Sep 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 77
/
DebugCode.php
54 lines (44 loc) · 1.48 KB
/
DebugCode.php
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
<?php
/**
* @author Patsura Dmitry https://github.com/ovr <talk@dmtry.me>
*/
namespace PHPSA\Analyzer\Pass\Expression\FunctionCall;
use phpDocumentor\Reflection\DocBlockFactory;
use PhpParser\Node\Expr\FuncCall;
use PHPSA\Context;
class DebugCode extends AbstractFunctionCallAnalyzer
{
const DESCRIPTION = 'Checks for use of debug code and suggests to remove it.';
protected $map = [
'var_dump' => 'var_dump',
'var_export' => 'var_export',
'debug_zval_dump' => 'debug_zval_dump',
'dd' => 'dd', // Laravel debug function
'dump' => 'dump' // Symfony debug function
];
/** @var DocBlockFactory */
protected $docBlockFactory;
public function __construct()
{
$this->docBlockFactory = DocBlockFactory::createInstance();
}
public function pass(FuncCall $funcCall, Context $context)
{
$functionName = $this->resolveFunctionName($funcCall, $context);
if (!$functionName || !isset($this->map[$functionName])) {
return false;
}
if ($funcCall->getDocComment()) {
$phpdoc = $this->docBlockFactory->create($funcCall->getDocComment()->getText());
if ($phpdoc->hasTag('expected')) {
return false;
}
}
$context->notice(
'debug.code',
sprintf('Function %s() is a debug function, please don`t use it in production.', $functionName),
$funcCall
);
return true;
}
}