DebugCode::pass()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 22

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 0
Metric Value
cc 5
nc 4
nop 2
dl 0
loc 22
ccs 0
cts 13
cp 0
crap 30
rs 9.2568
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
namespace PHPSA\Analyzer\Pass\Expression\FunctionCall;
7
8
use phpDocumentor\Reflection\DocBlockFactory;
9
use PhpParser\Node\Expr\FuncCall;
10
use PHPSA\Context;
11
12
class DebugCode extends AbstractFunctionCallAnalyzer
13
{
14
    const DESCRIPTION = 'Checks for use of debug code and suggests to remove it.';
15
16
    protected $map = [
17
        'var_dump' => 'var_dump',
18
        'var_export' => 'var_export',
19
        'debug_zval_dump' => 'debug_zval_dump',
20
        'dd' => 'dd', // Laravel debug function
21
        'dump' => 'dump' // Symfony debug function
22
    ];
23
24
    /** @var DocBlockFactory */
25
    protected $docBlockFactory;
26
27
    public function __construct()
28
    {
29
        $this->docBlockFactory = DocBlockFactory::createInstance();
30
    }
31
32
    public function pass(FuncCall $funcCall, Context $context)
33
    {
34
        $functionName = $this->resolveFunctionName($funcCall, $context);
35
        if (!$functionName || !isset($this->map[$functionName])) {
36
            return false;
37
        }
38
39
        if ($funcCall->getDocComment()) {
40
            $phpdoc = $this->docBlockFactory->create($funcCall->getDocComment()->getText());
41
            if ($phpdoc->hasTag('expected')) {
42
                return false;
43
            }
44
        }
45
46
        $context->notice(
47
            'debug.code',
48
            sprintf('Function %s() is a debug function, please don`t use it in production.', $functionName),
49
            $funcCall
50
        );
51
52
        return true;
53
    }
54
}
55