Completed
Push — master ( 60f9bd...b3f6ba )
by Дмитрий
06:29 queued 03:07
created

RegularExpressions   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 100%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 41
ccs 19
cts 19
cp 1
rs 10
wmc 6
lcom 1
cbo 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B pass() 0 21 6
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 10
    public function pass(FuncCall $funcCall, Context $context)
33
    {
34 10
        $functionName = $this->resolveFunctionName($funcCall, $context);
35 10
        if ($functionName && isset(self::$map[$functionName])) {
36 1
            $pattern = $context->getExpressionCompiler()->compile($funcCall->args[0]);
37 1
            if ($pattern->isString() && $pattern->isCorrectValue()) {
38 1
                $guard = \RegexGuard\Factory::getGuard();
39 1
                if (!$guard->isRegexValid($pattern->getValue())) {
40 1
                    $context->notice(
41 1
                        'regex.invalid',
42 1
                        sprintf(
43 1
                            'Regular expression %s is not valid',
44 1
                            $pattern->getValue()
45 1
                        ),
46 1
                        $funcCall->args[0],
47
                        Check::CHECK_ALPHA
48 1
                    );
49 1
                }
50 1
            }
51 1
        }
52 10
    }
53
}
54