Completed
Push — master ( ec8a9b...257aff )
by Maxim
05:03 queued 02:03
created

InjectedStringSuit   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 88.24%

Importance

Changes 0
Metric Value
dl 0
loc 47
ccs 15
cts 17
cp 0.8824
rs 10
c 0
b 0
f 0
wmc 3

3 Methods

Rating   Name   Duplication   Size   Complexity  
A addSlashes() 0 8 1
A resolveClassNames() 0 10 1
A __construct() 0 3 1
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 InjectedStringSuit
19
{
20
    /** @var string expression */
21
    private $expression;
22
23
    /**
24
     * InjectedStringSuit constructor.
25
     *
26
     * @param string $expression expression
27
     */
28 1
    public function __construct(string $expression)
29
    {
30 1
        $this->expression = $expression;
31 1
    }
32
33
    /**
34
     * Resolves class names in *injected* callbacks and conditions.
35
     *
36
     * @param string $namespace namespace
37
     * @return string expression with resolved class names
38
     */
39
    public function resolveClassNames(string $namespace): string
40
    {
41 1
        $expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) {
42
            return str_replace(':', ':\\', $matches[0]);
43 1
        }, $this->expression);
44 1
        $expression = preg_replace('/(?<!:):(?=([a-zA-Z_][a-zA-Z0-9_]*))/', "$namespace\\", $expression);
45 1
        $expression = preg_replace_callback('/"[^"]"|\'[^\']\'/', function (array $matches) {
46
            return str_replace(':\\', ':', $matches[0]);
47 1
        }, $expression);
48 1
        return $expression;
49
    }
50
51
    /**
52
     * Adds slashes to string.
53
     * 
54
     * @param string $charlist symbols add slashes to
55
     * @return string string with slashes
56
     */
57 1
    public function addSlashes(string $charlist): string 
58
    {
59 1
        return preg_replace_callback(
60 1
            '/`([^`]|\\\\`)+((?<!\\\\)`)/',
61 1
            function (array $matches) use($charlist): string {
62 1
                return addcslashes($matches[0], $charlist);
63 1
            },
64
            $this->expression
65
        );
66
    }
67
}