| Total Complexity | 80 |
| Total Lines | 328 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| 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 |
||
| 11 | class Rule |
||
| 12 | { |
||
| 13 | |||
| 14 | protected $logging; //< logging class |
||
| 15 | |||
| 16 | /** |
||
| 17 | * Setup Rule Class |
||
| 18 | * @param Logging $logClass : where to log |
||
| 19 | */ |
||
| 20 | function __construct($logClass) |
||
| 21 | { |
||
| 22 | $this->logging=$logClass; |
||
| 23 | |||
| 24 | } |
||
| 25 | |||
| 26 | /** |
||
| 27 | * Get full number |
||
| 28 | * @return array<number,string> |
||
| 29 | */ |
||
| 30 | private function get_number($rule,&$item) |
||
| 31 | { |
||
| 32 | $item2=$item+1; |
||
| 33 | while ( |
||
| 34 | ($item2!=strlen($rule)) |
||
| 35 | && (preg_match('/[0-9\.]/',$rule[$item2]))) |
||
| 36 | { |
||
| 37 | $item2++ ; |
||
| 38 | } |
||
| 39 | $val=substr($rule,$item,$item2-$item); |
||
| 40 | $item=$item2; |
||
| 41 | //echo "number ".$val."\n"; |
||
| 42 | |||
| 43 | return array(0,$val); |
||
| 44 | } |
||
| 45 | |||
| 46 | private function get_string($rule,&$item) |
||
| 47 | { |
||
| 48 | $item++; |
||
| 49 | $item2=$this->eval_getNext($rule,$item,'"'); |
||
| 50 | $val=substr($rule,$item,$item2-$item-1); |
||
| 51 | $item=$item2; |
||
| 52 | //echo "string : ".$val."\n"; |
||
| 53 | return array(1,$val); |
||
| 54 | |||
| 55 | } |
||
| 56 | |||
| 57 | private function get_group($rule,&$item) |
||
| 58 | { |
||
| 59 | $item++; |
||
| 60 | $start=$item; |
||
| 61 | $parenthesis_count=0; |
||
| 62 | while (($item < strlen($rule)) // Not end of string AND |
||
| 63 | && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
||
| 64 | { |
||
| 65 | if ($rule[$item] == '"' ) |
||
| 66 | { // pass through string |
||
| 67 | $item++; |
||
| 68 | $item=$this->eval_getNext($rule,$item,'"'); |
||
| 69 | } |
||
| 70 | else{ |
||
| 71 | if ($rule[$item] == '(') |
||
| 72 | { |
||
| 73 | $parenthesis_count++; |
||
| 74 | } |
||
| 75 | if ($rule[$item] == ')') |
||
| 76 | { |
||
| 77 | $parenthesis_count--; |
||
| 78 | } |
||
| 79 | $item++; |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
||
| 84 | $val=substr($rule,$start,$item-$start); |
||
| 85 | $item++; |
||
| 86 | $start=0; |
||
| 87 | //echo "group : ".$val."\n"; |
||
| 88 | // returns evaluation of group as type 2 (boolean) |
||
| 89 | return array(2,$this->evaluation($val,$start)); |
||
| 90 | } |
||
| 91 | |||
| 92 | protected function eval_getElement($rule,&$item) |
||
| 93 | { |
||
| 94 | if ($item >= strlen($rule)) |
||
| 95 | { |
||
| 96 | throw new Exception("Early end of string ".$rule ." at " .$item ); |
||
| 97 | } |
||
| 98 | while ($rule[$item]==' ') $item++; |
||
| 99 | if (preg_match('/[0-9\.]/',$rule[$item])) |
||
| 100 | { // number |
||
| 101 | return $this->get_number($rule, $item); |
||
| 102 | } |
||
| 103 | if ($rule[$item] == '"') |
||
| 104 | { // string |
||
| 105 | return $this->get_string($rule, $item); |
||
| 106 | } |
||
| 107 | |||
| 108 | if ($rule[$item] == '(') |
||
| 109 | { // grouping |
||
| 110 | return $this->get_group($rule, $item); |
||
| 111 | } |
||
| 112 | throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
||
| 113 | |||
| 114 | } |
||
| 115 | |||
| 116 | protected function eval_getNext($rule,$item,$tok) |
||
| 117 | { |
||
| 118 | while ( |
||
| 119 | ($rule[$item] != $tok ) |
||
| 120 | && ($item < strlen($rule))) |
||
| 121 | { |
||
| 122 | $item++; |
||
| 123 | } |
||
| 124 | if ($item==strlen($rule)) { |
||
| 125 | throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
||
| 126 | } |
||
| 127 | return $item+1; |
||
| 128 | } |
||
| 129 | |||
| 130 | protected function eval_getOper($rule,&$item) |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | private function check_negate_first($rule,&$item) |
||
| 158 | { |
||
| 159 | if ( $rule[$item] == '!') // If '!' found, negate next expression. |
||
| 160 | { |
||
| 161 | $item++; |
||
| 162 | return true; |
||
| 163 | } |
||
| 164 | else |
||
| 165 | { |
||
| 166 | return false; |
||
| 167 | } |
||
| 168 | } |
||
| 169 | |||
| 170 | private function do_compare($val1,$val2,$comp,$negate) |
||
| 171 | { |
||
| 172 | switch ($comp){ |
||
| 173 | case '<': $retVal= ($val1 < $val2); break; |
||
| 174 | case '<=': $retVal= ($val1 <= $val2); break; |
||
| 175 | case '>': $retVal= ($val1 > $val2); break; |
||
| 176 | case '>=': $retVal= ($val1 >= $val2); break; |
||
| 177 | case '=': $retVal= ($val1 == $val2); break; |
||
| 178 | case '!=': $retVal= ($val1 != $val2); break; |
||
| 179 | case '~': $retVal= (preg_match('/'.preg_replace('/"/','',$val2).'/',$val1)); break; |
||
| 180 | case '|': $retVal= ($val1 || $val2); break; |
||
| 181 | case '&': $retVal= ($val1 && $val2); break; |
||
| 182 | default: throw new Exception("Error in expression - unknown comp : ".$comp); |
||
| 183 | } |
||
| 184 | if ($negate === true) $retVal = ! $retVal; // Inverse result if negate before expression |
||
| 185 | |||
| 186 | return $retVal; |
||
| 187 | } |
||
| 188 | |||
| 189 | /** Evaluation : makes token and evaluate. |
||
| 190 | * Public function for expressions testing |
||
| 191 | * accepts : < > = <= >= != (typec = 0) |
||
| 192 | * operators : & | (typec=1) |
||
| 193 | * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
||
| 194 | * comparison int vs strings will return null (error) |
||
| 195 | * return : bool or null on error |
||
| 196 | */ |
||
| 197 | public function evaluation($rule,&$item) |
||
| 249 | } |
||
| 250 | } |
||
| 251 | |||
| 252 | // Remove all whitespaces (when not quoted) |
||
| 253 | public function eval_cleanup($rule) |
||
| 254 | { |
||
| 255 | $item=0; |
||
| 256 | $rule2=''; |
||
| 257 | while ($item < strlen($rule)) |
||
| 258 | { |
||
| 259 | if ($rule[$item]==' ') { $item++; continue; } |
||
| 260 | if ($rule[$item]=='"') |
||
| 261 | { |
||
| 262 | $rule2.=$rule[$item]; |
||
| 263 | $item++; |
||
| 264 | while (($item < strlen($rule)) && ($rule[$item]!='"') ) |
||
| 265 | { |
||
| 266 | $rule2.=$rule[$item]; |
||
| 267 | $item++; |
||
| 268 | } |
||
| 269 | if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
||
| 270 | $rule2.=$rule[$item]; |
||
| 271 | $item++; |
||
| 272 | continue; |
||
| 273 | } |
||
| 274 | |||
| 275 | $rule2.=$rule[$item]; |
||
| 276 | $item++; |
||
| 277 | } |
||
| 278 | |||
| 279 | return $rule2; |
||
| 280 | } |
||
| 281 | |||
| 282 | /** Evaluation rule (uses eval_* functions recursively) |
||
| 283 | * @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) ) |
||
| 284 | * @param array $oidList : OIDs values to sustitute. |
||
| 285 | * @return bool : true : rule match, false : rule don't match , throw exception on error. |
||
| 286 | */ |
||
| 287 | |||
| 288 | public function eval_rule($rule,$oidList) |
||
| 339 | } |
||
| 340 | |||
| 341 | } |