Passed
Push — master ( 782f23...8a769b )
by Patrick
02:09
created

RuleUtils::get_number()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 14
rs 10
1
<?php
2
3
/**
4
 * Rule evaluation utilities
5
 *
6
 * @license GPL
7
 * @author Patrick Proy
8
 * @package trapdirector
9
 * @subpackage Processing
10
 *
11
 */
12
trait RuleUtils
13
{
14
15
    /**
16
     * Get full number
17
     * @param string $rule Rule as string
18
     * @param int item current eval position
0 ignored issues
show
Bug introduced by
The type item was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
19
     * @return array<int,string>
20
     */
21
    private function get_number(string $rule,int &$item)
22
    {
23
        $item2=$item+1;
24
        while (
25
            ($item2!=strlen($rule))
26
            && (preg_match('/[\-0-9\.]/',$rule[$item2])))
27
        {
28
            $item2++ ;
29
        }
30
        $val=substr($rule,$item,$item2-$item);
31
        $item=$item2;
32
        //echo "number ".$val."\n";
33
        
34
        return array(0,$val);
35
    }
36
37
    /**
38
     * Get a string (between ") 
39
     * @param string $rule Rule as string
40
     * @param int item current eval position
41
     * @return array<int,string>
42
     */
43
    private function get_string(string $rule,int &$item)
44
    {
45
        $item++;
46
        $item2=$this->eval_getNext($rule,$item,'"');
0 ignored issues
show
Bug introduced by
It seems like eval_getNext() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

46
        /** @scrutinizer ignore-call */ 
47
        $item2=$this->eval_getNext($rule,$item,'"');
Loading history...
47
        $val=substr($rule,$item,$item2-$item-1);
48
        $item=$item2;
49
        //echo "string : ".$val."\n";
50
        return array(1,$val);
51
        
52
    }
53
    
54
    /**
55
     * Parse elements inside () : jumps over "" and count parenthesis.
56
     * Ex : ( "test" != ")test" & (1==2) ) will return "test" != ")test" & (1==2)
57
     * @param string $rule : the current rule
58
     * @param int $item : actual position in rule
59
     * @throws Exception
60
     * @return string : everything inside parenthesis
61
     */
62
    private function parse_parenthesis(string $rule,int &$item) : string
63
    {
64
        $item++;
65
        $start=$item;
66
        $parenthesis_count=0;
67
        while (($item < strlen($rule)) // Not end of string AND
68
            && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded ()
69
        {
70
            if ($rule[$item] == '"' )
71
            { // pass through string
72
                $item++;
73
                $item=$this->eval_getNext($rule,$item,'"');
74
            }
75
            else{
76
                if ($rule[$item] == '(')
77
                {
78
                    $parenthesis_count++;
79
                }
80
                if ($rule[$item] == ')')
81
                {
82
                    $parenthesis_count--;
83
                }
84
                $item++;
85
            }
86
        }
87
        
88
        if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);}
89
        $val=substr($rule,$start,$item-$start);
90
        $item++;
91
        return $val;
92
    }
93
94
    /**
95
     * Get and eval a grouped condition - ex : (1==1)
96
     * @param string $rule
97
     * @param int $item
98
     * @return array<int,string>
99
     */
100
    private function get_group(string $rule,int &$item) : array
101
    {
102
        // gets eveything inside parenthesis
103
        $val=$this->parse_parenthesis($rule, $item);
104
        // Returns boolean with evaluation of all inside parenthesis
105
        $start=0;
106
        return array(2,$this->evaluation($val,$start));
0 ignored issues
show
Bug introduced by
It seems like evaluation() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

106
        return array(2,$this->/** @scrutinizer ignore-call */ evaluation($val,$start));
Loading history...
107
    }
108
    
109
    /**
110
     * @param string $rule
111
     * @param int $item
112
     * @throws Exception
113
     * @return array<int,string>
114
     */
115
    private function get_function(string $rule,int &$item) : array
116
    {
117
        // function is : __function(param1,param2...)
118
        $start=$item;
119
        while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '('
120
        {
121
            $item++;
122
        }
123
        if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);}
124
        
125
        // get parameters between parenthesis
126
        
127
        $this->parse_parenthesis($rule, $item);
128
        
129
        $val=substr($rule,$start,$item-$start);
130
        
131
        $this->logging->log('got function ' . $val,DEBUG);
132
        
133
        return array(2,$this->trapClass->pluginClass->evaluateFunctionString($val));
134
        
135
    }
136
    
137
}