| Total Complexity | 86 |
| Total Lines | 375 |
| 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 | |||
| 10 | protected $logging; //< logging class |
||
| 11 | |||
| 12 | /** |
||
| 13 | * Setup Rule Class |
||
| 14 | * @param Logging $logClass : where to log |
||
| 15 | */ |
||
| 16 | function __construct($logClass) |
||
| 17 | { |
||
| 18 | $this->logging=$logClass; |
||
| 19 | |||
| 20 | } |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Get full number |
||
| 24 | * @return array<number,string> |
||
| 25 | */ |
||
| 26 | private function get_number($rule,&$item) |
||
| 27 | { |
||
| 28 | $item2=$item+1; |
||
| 29 | while ( |
||
| 30 | ($item2!=strlen($rule)) |
||
| 31 | && (preg_match('/[\-0-9\.]/',$rule[$item2]))) |
||
| 32 | { |
||
| 33 | $item2++ ; |
||
| 34 | } |
||
| 35 | $val=substr($rule,$item,$item2-$item); |
||
| 36 | $item=$item2; |
||
| 37 | //echo "number ".$val."\n"; |
||
| 38 | |||
| 39 | return array(0,$val); |
||
| 40 | } |
||
| 41 | |||
| 42 | private function get_string($rule,&$item) |
||
| 50 | |||
| 51 | } |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Parse elements inside () : jumps over "" and count parenthesis. |
||
| 55 | * Ex : ( "test" != ")test" & (1==2) ) will return "test" != ")test" & (1==2) |
||
| 56 | * @param string $rule : the current rule |
||
| 57 | * @param int $item : actual position in rule |
||
| 58 | * @throws Exception |
||
| 59 | * @return string : everything inside parenthesis |
||
| 60 | */ |
||
| 61 | private function parse_parenthesis(string $rule,int &$item) : string |
||
| 62 | { |
||
| 63 | $item++; |
||
| 64 | $start=$item; |
||
| 65 | $parenthesis_count=0; |
||
| 66 | while (($item < strlen($rule)) // Not end of string AND |
||
| 67 | && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
||
| 68 | { |
||
| 69 | if ($rule[$item] == '"' ) |
||
| 70 | { // pass through string |
||
| 71 | $item++; |
||
| 72 | $item=$this->eval_getNext($rule,$item,'"'); |
||
| 73 | } |
||
| 74 | else{ |
||
| 75 | if ($rule[$item] == '(') |
||
| 76 | { |
||
| 77 | $parenthesis_count++; |
||
| 78 | } |
||
| 79 | if ($rule[$item] == ')') |
||
| 80 | { |
||
| 81 | $parenthesis_count--; |
||
| 82 | } |
||
| 83 | $item++; |
||
| 84 | } |
||
| 85 | } |
||
| 86 | |||
| 87 | if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
||
| 88 | $val=substr($rule,$start,$item-$start); |
||
| 89 | $item++; |
||
| 90 | return $val; |
||
| 91 | } |
||
| 92 | |||
| 93 | |||
| 94 | /** |
||
| 95 | * Get and eval a grouped condition - ex : (1==1) |
||
| 96 | * @param string $rule |
||
| 97 | * @param int $item |
||
| 98 | * @return array |
||
| 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)); |
||
| 107 | } |
||
| 108 | |||
| 109 | private function get_function(string $rule,int &$item) : array |
||
| 110 | { |
||
| 111 | // function is : __function(param1,param2...) |
||
| 112 | $start=$item; |
||
| 113 | while (($item < strlen($rule)) && ($rule[$item] != '(' )) // Not end of string AND not opening '(' |
||
| 114 | { |
||
| 115 | $item++; |
||
| 116 | } |
||
| 117 | if ($item==strlen($rule)) {throw new Exception("no opening () for function in ".$rule ." at " .$item);} |
||
| 118 | |||
| 119 | // get parameters between parenthesis |
||
| 120 | |||
| 121 | $params=$this->parse_parenthesis($rule, $item); |
||
|
|
|||
| 122 | |||
| 123 | $val=substr($rule,$start,$item-$start); |
||
| 124 | |||
| 125 | $this->logging->log('got function ' . $val . ' returning true for now',DEBUG); |
||
| 126 | |||
| 127 | return array(2,true); |
||
| 128 | |||
| 129 | } |
||
| 130 | |||
| 131 | protected function eval_getElement($rule,&$item) |
||
| 132 | { |
||
| 133 | if ($item >= strlen($rule)) |
||
| 134 | { |
||
| 135 | throw new Exception("Early end of string ".$rule ." at " .$item ); |
||
| 136 | } |
||
| 137 | while ($rule[$item]==' ') $item++; |
||
| 138 | if (preg_match('/[\-0-9\.]/',$rule[$item])) |
||
| 139 | { // number |
||
| 140 | return $this->get_number($rule, $item); |
||
| 141 | } |
||
| 142 | if ($rule[$item] == '"') |
||
| 143 | { // string |
||
| 144 | return $this->get_string($rule, $item); |
||
| 145 | } |
||
| 146 | |||
| 147 | if ($rule[$item] == '(') |
||
| 148 | { // grouping |
||
| 149 | return $this->get_group($rule, $item); |
||
| 150 | } |
||
| 151 | if ($rule[$item] == '_') |
||
| 152 | { // function |
||
| 153 | return $this->get_function($rule, $item); |
||
| 154 | } |
||
| 155 | throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
||
| 156 | |||
| 157 | } |
||
| 158 | |||
| 159 | protected function eval_getNext($rule,$item,$tok) |
||
| 160 | { |
||
| 161 | while ( |
||
| 162 | ($rule[$item] != $tok ) |
||
| 163 | && ($item < strlen($rule))) |
||
| 164 | { |
||
| 165 | $item++; |
||
| 166 | } |
||
| 167 | if ($item==strlen($rule)) { |
||
| 168 | throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
||
| 169 | } |
||
| 170 | return $item+1; |
||
| 171 | } |
||
| 172 | |||
| 173 | protected function eval_getOper($rule,&$item) |
||
| 197 | } |
||
| 198 | } |
||
| 199 | |||
| 200 | private function check_negate_first($rule,&$item) |
||
| 201 | { |
||
| 202 | if ( $rule[$item] == '!') // If '!' found, negate next expression. |
||
| 203 | { |
||
| 204 | $item++; |
||
| 205 | return true; |
||
| 206 | } |
||
| 207 | else |
||
| 208 | { |
||
| 209 | return false; |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | private function do_compare($val1,$val2,$comp,$negate) |
||
| 214 | { |
||
| 215 | switch ($comp){ |
||
| 216 | case '<': $retVal= ($val1 < $val2); break; |
||
| 217 | case '<=': $retVal= ($val1 <= $val2); break; |
||
| 218 | case '>': $retVal= ($val1 > $val2); break; |
||
| 219 | case '>=': $retVal= ($val1 >= $val2); break; |
||
| 220 | case '=': $retVal= ($val1 == $val2); break; |
||
| 221 | case '!=': $retVal= ($val1 != $val2); break; |
||
| 222 | case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
||
| 223 | case '|': $retVal= ($val1 || $val2); break; |
||
| 224 | case '&': $retVal= ($val1 && $val2); break; |
||
| 225 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
||
| 226 | } |
||
| 227 | if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
||
| 228 | |||
| 229 | return $retVal; |
||
| 230 | } |
||
| 231 | |||
| 232 | /** Evaluation : makes token and evaluate. |
||
| 233 | * Public function for expressions testing |
||
| 234 | * accepts : < > = <= >= != (typec = 0) |
||
| 235 | * operators : & | (typec=1) |
||
| 236 | * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
||
| 237 | * comparison int vs strings will return null (error) |
||
| 238 | * return : bool or null on error |
||
| 239 | */ |
||
| 240 | public function evaluation($rule,&$item) |
||
| 292 | } |
||
| 293 | } |
||
| 294 | |||
| 295 | // Remove all whitespaces (when not quoted) |
||
| 296 | public function eval_cleanup($rule) |
||
| 297 | { |
||
| 298 | $item=0; |
||
| 299 | $rule2=''; |
||
| 300 | while ($item < strlen($rule)) |
||
| 301 | { |
||
| 302 | if ($rule[$item]==' ') { $item++; continue; } |
||
| 303 | if ($rule[$item]=='"') |
||
| 304 | { |
||
| 305 | $rule2.=$rule[$item]; |
||
| 306 | $item++; |
||
| 307 | while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
||
| 308 | { |
||
| 309 | $rule2.=$rule[$item]; |
||
| 310 | $item++; |
||
| 311 | } |
||
| 312 | if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
||
| 313 | $rule2.=$rule[$item]; |
||
| 314 | $item++; |
||
| 315 | continue; |
||
| 316 | } |
||
| 317 | |||
| 318 | $rule2.=$rule[$item]; |
||
| 319 | $item++; |
||
| 320 | } |
||
| 321 | |||
| 322 | return $rule2; |
||
| 323 | } |
||
| 324 | |||
| 325 | /** Evaluation rule (uses eval_* functions recursively) |
||
| 326 | * @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) ) |
||
| 327 | * @param array $oidList : OIDs values to sustitute. |
||
| 328 | * @return bool : true : rule match, false : rule don't match , throw exception on error. |
||
| 329 | */ |
||
| 330 | |||
| 331 | public function eval_rule($rule,$oidList) |
||
| 382 | } |
||
| 383 | |||
| 384 | } |