| Total Complexity | 70 |
| Total Lines | 287 |
| 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; |
||
|
1 ignored issue
–
show
|
|||
| 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) |
||
| 30 | { |
||
| 31 | if ($item >= strlen($rule)) |
||
| 32 | { |
||
| 33 | throw new Exception("Early end of string ".$rule ." at " .$item ); |
||
| 34 | } |
||
| 35 | while ($rule[$item]==' ') $item++; |
||
| 36 | if (preg_match('/[\-0-9\.]/',$rule[$item])) |
||
| 37 | { // number |
||
| 38 | return $this->get_number($rule, $item); |
||
| 39 | } |
||
| 40 | if ($rule[$item] == '"') |
||
| 41 | { // string |
||
| 42 | return $this->get_string($rule, $item); |
||
| 43 | } |
||
| 44 | |||
| 45 | if ($rule[$item] == '(') |
||
| 46 | { // grouping |
||
| 47 | return $this->get_group($rule, $item); |
||
| 48 | } |
||
| 49 | if ($rule[$item] == '_') |
||
| 50 | { // function |
||
| 51 | return $this->get_function($rule, $item); |
||
| 52 | } |
||
| 53 | throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
||
| 54 | |||
| 55 | } |
||
| 56 | |||
| 57 | protected function eval_getNext($rule,$item,$tok) |
||
| 58 | { |
||
| 59 | while ( |
||
| 60 | ($rule[$item] != $tok ) |
||
| 61 | && ($item < strlen($rule))) |
||
| 62 | { |
||
| 63 | $item++; |
||
| 64 | } |
||
| 65 | if ($item==strlen($rule)) { |
||
| 66 | throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
||
| 67 | } |
||
| 68 | return $item+1; |
||
| 69 | } |
||
| 70 | |||
| 71 | protected function eval_getOper($rule,&$item) |
||
| 95 | } |
||
| 96 | } |
||
| 97 | |||
| 98 | private function check_negate_first($rule,&$item) |
||
| 99 | { |
||
| 100 | if ( $rule[$item] == '!') // If '!' found, negate next expression. |
||
| 101 | { |
||
| 102 | $item++; |
||
| 103 | return true; |
||
| 104 | } |
||
| 105 | else |
||
| 106 | { |
||
| 107 | return false; |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | private function do_compare($val1,$val2,$comp,$negate) |
||
| 128 | } |
||
| 129 | |||
| 130 | /** Evaluation : makes token and evaluate. |
||
| 131 | * Public function for expressions testing |
||
| 132 | * accepts : < > = <= >= != (typec = 0) |
||
| 133 | * operators : & | (typec=1) |
||
| 134 | * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
||
| 135 | * comparison int vs strings will return null (error) |
||
| 136 | * return : bool or null on error |
||
| 137 | */ |
||
| 138 | public function evaluation($rule,&$item) |
||
| 190 | } |
||
| 191 | } |
||
| 192 | |||
| 193 | // Remove all whitespaces (when not quoted) |
||
| 194 | public function eval_cleanup($rule) |
||
| 195 | { |
||
| 196 | $item=0; |
||
| 197 | $rule2=''; |
||
| 198 | while ($item < strlen($rule)) |
||
| 199 | { |
||
| 200 | if ($rule[$item]==' ') { $item++; continue; } |
||
| 201 | if ($rule[$item]=='"') |
||
| 202 | { |
||
| 203 | $rule2.=$rule[$item]; |
||
| 204 | $item++; |
||
| 205 | while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
||
| 206 | { |
||
| 207 | $rule2.=$rule[$item]; |
||
| 208 | $item++; |
||
| 209 | } |
||
| 210 | if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
||
| 211 | $rule2.=$rule[$item]; |
||
| 212 | $item++; |
||
| 213 | continue; |
||
| 214 | } |
||
| 215 | |||
| 216 | $rule2.=$rule[$item]; |
||
| 217 | $item++; |
||
| 218 | } |
||
| 219 | |||
| 220 | return $rule2; |
||
| 221 | } |
||
| 222 | |||
| 223 | |||
| 224 | /** |
||
| 225 | * Get '*' or '**' and transform in [0-9]+ or .* in return string |
||
| 226 | * @param string $oid OID in normal or regexp format. '*' will be escaped ('\*') |
||
| 227 | * @return string correct regexp format |
||
| 228 | */ |
||
| 229 | public function regexp_eval(string &$oid) |
||
| 230 | { |
||
| 231 | // ** replaced by .* |
||
| 232 | $oidR=preg_replace('/\*\*/', '.*', $oid); |
||
| 233 | // * replaced by [0-9]+ |
||
| 234 | $oidR=preg_replace('/\*/', '[0-9]+', $oidR); |
||
| 235 | |||
| 236 | // replace * with \* in oid for preg_replace |
||
| 237 | $oid=preg_replace('/\*/', '\*', $oid); |
||
| 238 | |||
| 239 | $this->logging->log('Regexp eval : '.$oid.' / '.$oidR,DEBUG ); |
||
| 240 | |||
| 241 | return $oidR; |
||
| 242 | } |
||
| 243 | |||
| 244 | |||
| 245 | /** Evaluation rule (uses eval_* functions recursively) |
||
| 246 | * @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) ) |
||
| 247 | * @param array $oidList : OIDs values to sustitute. |
||
| 248 | * @return bool : true : rule match, false : rule don't match , throw exception on error. |
||
| 249 | */ |
||
| 250 | public function eval_rule($rule,$oidList) |
||
| 294 | } |
||
| 295 | |||
| 296 | } |