Completed
Push — master ( ed93a7...cfa785 )
by Дмитрий
04:06
created

RegularExpressions::pass()   B

Complexity

Conditions 6
Paths 4

Size

Total Lines 21
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 23.7156

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 14
c 1
b 0
f 0
nc 4
nop 2
dl 0
loc 21
ccs 4
cts 19
cp 0.2105
crap 23.7156
rs 8.7624
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\Analyzer\Helper\ResolveExpressionTrait;
11
use PHPSA\Check;
12
use PHPSA\Context;
13
14
class RegularExpressions implements PassFunctionCallInterface
15
{
16
    use ResolveExpressionTrait;
17
18
    static public $map = [
19
        'preg_filter' => 0,
20
        'preg_grep' => 0,
21
        'preg_match_all' => 0,
22
        'preg_match' => 0,
23
        'preg_quote' => 0,
24
//        'preg_replace_callback_array' => 0,
25
        'preg_replace_callback' => 0,
26
        'preg_replace' => 0,
27
        'preg_split' => 0,
28
    ];
29
30
    /**
31
     * @param FuncCall $funcCall
32
     * @param Context $context
33
     * @return mixed
34
     */
35 5
    public function pass(FuncCall $funcCall, Context $context)
36
    {
37 5
        $functionName = $this->resolveFunctionName($funcCall, $context);
38 5
        if ($functionName && isset(self::$map[$functionName])) {
39
            $pattern = $context->getExpressionCompiler()->compile($funcCall->args[0]);
40
            if ($pattern->isString() && $pattern->isCorrectValue()) {
41
                $guard = \RegexGuard\Factory::getGuard();
42
                if (!$guard->isRegexValid($pattern->getValue())) {
43
                    $context->notice(
44
                        'regex.invalid',
45
                        sprintf(
46
                            'Regular expression %s is not valid',
47
                            $pattern->getValue()
48
                        ),
49
                        $funcCall->args[0],
50
                        Check::CHECK_ALPHA
51
                    );
52
                }
53
            }
54
        }
55 5
    }
56
}
57