Passed
Push — master ( eb8fe1...f9b36a )
by Maxim
03:07
created

InjectedStringParser::addSlashes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 1
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is a part of "Axessors" library.
4
 *
5
 * @author <[email protected]>
6
 * @license GPL
7
 */
8
9
namespace NoOne4rever\Axessors;
10
11
/**
12
 * Class InjectedStringParser.
13
 * 
14
 * Processes *injected* callbacks and conditions.
15
 * 
16
 * @package NoOne4rever\Axessors
17
 */
18
class InjectedStringParser
19
{
20
    private $expression;
21
    
22
    public function __construct(string $expression)
23
    {
24
        $this->expression = $expression;
25
    }
26
27
    /**
28
     * Resolves class names in *injected* callbacks and conditions.
29
     *
30
     * @param string $namespace namespace
31
     * @return string expression with resolved class names
32
     */
33
    public function resolveClassNames(string $namespace): string
34
    {
35
        $expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) {
36
            return str_replace(':', ':\\', $matches[0]);
37
        }, $this->expression);
38
        $expression = preg_replace('/(?<!:):(?=([a-zA-Z_][a-zA-Z0-9_]*))/', "$namespace\\", $expression);
39
        $expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) {
40
            return str_replace(':\\', ':', $matches[0]);
41
        }, $expression);
42
        return $expression;
43
    }
44
    
45
    public function addSlashes(string $charlist): string 
46
    {
47
        return preg_replace_callback(
48
            '/`([^`]|\\\\`)+((?<!\\\\)`)/',
49
            function (array $matches) use($charlist): string {
50
                return addcslashes($matches[0], $charlist);
51
            },
52
            $this->expression
53
        );
54
    }
55
}