Completed
Push — master ( ecc5eb...8d6d87 )
by Lee
05:28 queued 01:25
created

Util::hasEval()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 1
crap 1
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
declare(strict_types=1);
4
5
namespace Casbin\Util;
6
7
use Casbin\Exceptions\CasbinException;
8
9
/**
10
 * Class Util.
11
 *
12
 * @author [email protected]
0 ignored issues
show
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
13
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
14
class Util
15
{
16
    const REGEXP = '/\beval\((?P<rule>[^),]*)\)/';
17
18
    /**
19
     * escapes the dots in the assertion, because the expression evaluation doesn't support such variable names.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
20
     *
21
     * @param string $s
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
22
     *
23
     * @return string
24
     */
25 210
    public static function escapeAssertion(string $s): string
26
    {
27 210
        if (0 === strpos($s, 'r.')) {
28 105
            $s = substr_replace($s, 'r_', 0, 2);
29
        }
30 210
        if (0 === strpos($s, 'p.')) {
31 3
            $s = substr_replace($s, 'p_', 0, 2);
32
        }
33
34
        $s = preg_replace_callback("~(\|| |=|\)|\(|&|<|>|,|\+|-|!|\*|\/)(r|p)(\.)~", function ($m) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
35 210
            return $m[1].$m[2].'_';
36 210
        }, $s);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
37
38 210
        if (null === $s) {
39
            throw new CasbinException(sprintf('Unable to escape assertion "%s"', $s));
0 ignored issues
show
Bug introduced by
$s of type void is incompatible with the type string expected by parameter $args of sprintf(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

39
            throw new CasbinException(sprintf('Unable to escape assertion "%s"', /** @scrutinizer ignore-type */ $s));
Loading history...
40
        }
41
42 210
        return $s;
43
    }
44
45
    /**
46
     * removes the comments starting with # in the text.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
47
     *
48
     * @param string $s
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
49
     *
50
     * @return string
51
     */
52 207
    public static function removeComments(string $s): string
53
    {
54 207
        $pos = strpos($s, '#');
55
56 207
        return false === $pos ? $s : trim(substr($s, 0, $pos));
57
    }
58
59
    /**
60
     * gets a printable string for a string array.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
61
     *
62
     * @param array $s
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
63
     *
64
     * @return string
65
     */
66 6
    public static function arrayToString(array $s): string
67
    {
68 6
        return implode(', ', $s);
69
    }
70
71
    /**
72
     * removes any duplicated elements in a string array.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
73
     *
74
     * @param array $s
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
75
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
76 12
    public static function arrayRemoveDuplicates(array &$s): void
77
    {
78 12
        $s = array_keys(array_flip($s));
79 12
    }
80
81
    /**
82
     * determine whether matcher contains function eval.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
83
     *
84
     * @param string $s
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
85
     *
86
     * @return bool
87
     */
88 126
    public static function hasEval(string $s): bool
89
    {
90 126
        return (bool) preg_match(self::REGEXP, $s);
91
    }
92
93
    /**
94
     * replace function eval with the value of its parameters.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
95
     *
96
     * @param string $s
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
97
     * @param string $rule
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
98
     *
99
     * @return string
100
     */
101 6
    public static function replaceEval(string $s, string $rule): string
102
    {
103 6
        return preg_replace(self::REGEXP, '('.$rule.')', $s);
104
    }
105
106
    /**
107
     * returns the parameters of function eval.
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
108
     *
109
     * @param string $s
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
110
     *
111
     * @return array
112
     */
113 6
    public static function getEvalValue(string $s): array
114
    {
115 6
        preg_match_all(self::REGEXP, $s, $matches);
116
117 6
        return $matches['rule'];
118
    }
119
}
120