Total Complexity | 57 |
Total Lines | 229 |
Duplicated Lines | 0 % |
Changes | 2 | ||
Bugs | 0 | Features | 0 |
Complex classes like Rule often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Rule, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
7 | class Rule |
||
8 | { |
||
9 | use \RuleUtils; |
||
10 | |||
11 | /** @var Logging $logging logging class*/ |
||
12 | protected $logging; |
||
13 | |||
14 | /** @var Trap $trapClass */ |
||
15 | protected $trapClass; |
||
16 | |||
17 | |||
18 | /** |
||
19 | * Setup Rule Class |
||
20 | * @param Trap $trapClass : To get logging class & plugin class |
||
21 | */ |
||
22 | function __construct($trapClass) |
||
23 | { |
||
24 | $this->trapClass=$trapClass; |
||
25 | $this->logging=$trapClass->logging; |
||
26 | } |
||
27 | |||
28 | |||
29 | protected function eval_getElement($rule,&$item) |
||
54 | |||
55 | } |
||
56 | |||
57 | protected function eval_getOper($rule,&$item) |
||
81 | } |
||
82 | } |
||
83 | |||
84 | private function do_compare($val1,$val2,$comp,$negate) |
||
101 | } |
||
102 | |||
103 | /** Evaluation : makes token and evaluate. |
||
104 | * Public function for expressions testing |
||
105 | * accepts : < > = <= >= != (typec = 0) |
||
106 | * operators : & | (typec=1) |
||
107 | * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
||
108 | * comparison int vs strings will return null (error) |
||
109 | * return : bool or null on error |
||
110 | */ |
||
111 | public function evaluation($rule,&$item) |
||
163 | } |
||
164 | } |
||
165 | |||
166 | /** |
||
167 | * Get '*' or '**' and transform in [0-9]+ or .* in return string |
||
168 | * @param string $oid OID in normal or regexp format. '*' will be escaped ('\*') |
||
169 | * @return string correct regexp format |
||
170 | */ |
||
171 | public function regexp_eval(string &$oid) |
||
172 | { |
||
173 | // ** replaced by .* |
||
174 | $oidR=preg_replace('/\*\*/', '.*', $oid); |
||
175 | // * replaced by [0-9]+ |
||
176 | $oidR=preg_replace('/\*/', '[0-9]+', $oidR); |
||
177 | |||
178 | // replace * with \* in oid for preg_replace |
||
179 | $oid=preg_replace('/\*/', '\*', $oid); |
||
180 | |||
181 | $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG ); |
||
182 | |||
183 | return $oidR; |
||
184 | } |
||
185 | |||
186 | |||
187 | /** Evaluation rule (uses eval_* functions recursively) |
||
188 | * @param string $rule : rule ( _OID(.1.3.6.1.4.1.8072.2.3.2.1)=_OID(.1.3.6.1.2.1.1.3.0) ) |
||
189 | * @param array $oidList : OIDs values to sustitute. |
||
190 | * @return bool : true : rule match, false : rule don't match , throw exception on error. |
||
191 | */ |
||
192 | public function eval_rule($rule,$oidList) |
||
236 | } |
||
237 | |||
238 | } |