Completed
Pull Request — master (#190)
by
unknown
04:51
created

RegularExpressions::pass()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 42

Importance

Changes 0
Metric Value
cc 6
eloc 14
nc 4
nop 2
dl 0
loc 21
ccs 0
cts 19
cp 0
crap 42
rs 8.7624
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Patsura Dmitry https://github.com/ovr <[email protected]>
4
 */
5
6
7
namespace PHPSA\Analyzer\Pass\Expression\FunctionCall;
8
9
use PhpParser\Node\Expr\FuncCall;
10
use PHPSA\Check;
11
use PHPSA\Context;
12
13
class RegularExpressions extends AbstractFunctionCallAnalyzer
14
{
15
    static public $map = [
16
        'preg_filter' => 0,
17
        'preg_grep' => 0,
18
        'preg_match_all' => 0,
19
        'preg_match' => 0,
20
        'preg_quote' => 0,
21
//        'preg_replace_callback_array' => 0,
22
        'preg_replace_callback' => 0,
23
        'preg_replace' => 0,
24
        'preg_split' => 0,
25
    ];
26
27
    /**
28
     * @param FuncCall $funcCall
29
     * @param Context $context
30
     * @return mixed
31
     */
32
    public function pass(FuncCall $funcCall, Context $context)
33
    {
34
        $functionName = $this->resolveFunctionName($funcCall, $context);
35
        if ($functionName && isset(self::$map[$functionName])) {
36
            $pattern = $context->getExpressionCompiler()->compile($funcCall->args[0]);
37
            if ($pattern->isString() && $pattern->isCorrectValue()) {
38
                $guard = \RegexGuard\Factory::getGuard();
39
                if (!$guard->isRegexValid($pattern->getValue())) {
40
                    $context->notice(
41
                        'regex.invalid',
42
                        sprintf(
43
                            'Regular expression %s is not valid',
44
                            $pattern->getValue()
45
                        ),
46
                        $funcCall->args[0],
47
                        Check::CHECK_ALPHA
48
                    );
49
                }
50
            }
51
        }
52
    }
53
}
54