| Total Complexity | 77 |
| Total Lines | 316 |
| 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 | private $item; //< current index of rule |
||
|
|
|||
| 17 | private $rule; //< The rule |
||
| 18 | |||
| 19 | /** |
||
| 20 | * Setup Rule Class |
||
| 21 | * @param Logging $logClass : where to log |
||
| 22 | */ |
||
| 23 | function __construct($logClass) |
||
| 24 | { |
||
| 25 | $this->logging=$logClass; |
||
| 26 | |||
| 27 | } |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Get full number |
||
| 31 | * @return array<number,string> |
||
| 32 | */ |
||
| 33 | private function get_number($rule,&$item) |
||
| 34 | { |
||
| 35 | $item2=$item+1; |
||
| 36 | while ( |
||
| 37 | ($item2!=strlen($rule)) |
||
| 38 | && (preg_match('/[0-9\.]/',$rule[$item2]))) |
||
| 39 | { |
||
| 40 | $item2++ ; |
||
| 41 | } |
||
| 42 | $val=substr($rule,$item,$item2-$item); |
||
| 43 | $item=$item2; |
||
| 44 | //echo "number ".$val."\n"; |
||
| 45 | |||
| 46 | return array(0,$val); |
||
| 47 | } |
||
| 48 | |||
| 49 | private function get_string($rule,&$item) |
||
| 50 | { |
||
| 51 | $item++; |
||
| 52 | $item2=$this->eval_getNext($rule,$item,'"'); |
||
| 53 | $val=substr($rule,$item,$item2-$item-1); |
||
| 54 | $item=$item2; |
||
| 55 | //echo "string : ".$val."\n"; |
||
| 56 | return array(1,$val); |
||
| 57 | |||
| 58 | } |
||
| 59 | |||
| 60 | private function get_group($rule,&$item) |
||
| 61 | { |
||
| 62 | $item++; |
||
| 63 | $start=$item; |
||
| 64 | $parenthesis_count=0; |
||
| 65 | while (($item < strlen($rule)) // Not end of string AND |
||
| 66 | && ( ($rule[$item] != ')' ) || $parenthesis_count > 0) ) // Closing ')' or embeded () |
||
| 67 | { |
||
| 68 | if ($rule[$item] == '"' ) |
||
| 69 | { // pass through string |
||
| 70 | $item++; |
||
| 71 | $item=$this->eval_getNext($rule,$item,'"'); |
||
| 72 | } |
||
| 73 | else{ |
||
| 74 | if ($rule[$item] == '(') |
||
| 75 | { |
||
| 76 | $parenthesis_count++; |
||
| 77 | } |
||
| 78 | if ($rule[$item] == ')') |
||
| 79 | { |
||
| 80 | $parenthesis_count--; |
||
| 81 | } |
||
| 82 | $item++; |
||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | if ($item==strlen($rule)) {throw new Exception("no closing () in ".$rule ." at " .$item);} |
||
| 87 | $val=substr($rule,$start,$item-$start); |
||
| 88 | $item++; |
||
| 89 | $start=0; |
||
| 90 | //echo "group : ".$val."\n"; |
||
| 91 | // returns evaluation of group as type 2 (boolean) |
||
| 92 | return array(2,$this->evaluation($val,$start)); |
||
| 93 | } |
||
| 94 | |||
| 95 | protected function eval_getElement($rule,&$item) |
||
| 96 | { |
||
| 97 | while ($rule[$item]==' ') $item++; |
||
| 98 | if (preg_match('/[0-9\.]/',$rule[$item])) |
||
| 99 | { // number |
||
| 100 | return $this->get_number($rule, $item); |
||
| 101 | } |
||
| 102 | if ($rule[$item] == '"') |
||
| 103 | { // string |
||
| 104 | return $this->get_string($rule, $item); |
||
| 105 | } |
||
| 106 | |||
| 107 | if ($rule[$item] == '(') |
||
| 108 | { // grouping |
||
| 109 | return $this->get_group($rule, $item); |
||
| 110 | } |
||
| 111 | throw new Exception("number/string not found in ".$rule ." at " .$item . ' : ' .$rule[$item]); |
||
| 112 | |||
| 113 | } |
||
| 114 | |||
| 115 | protected function eval_getNext($rule,$item,$tok) |
||
| 116 | { |
||
| 117 | while ( |
||
| 118 | ($rule[$item] != $tok ) |
||
| 119 | && ($item < strlen($rule))) |
||
| 120 | { |
||
| 121 | $item++; |
||
| 122 | } |
||
| 123 | if ($item==strlen($rule)) { |
||
| 124 | throw new Exception("closing '".$tok."' not found in ".$rule ." at " .$item); |
||
| 125 | } |
||
| 126 | return $item+1; |
||
| 127 | } |
||
| 128 | |||
| 129 | protected function eval_getOper($rule,&$item) |
||
| 153 | } |
||
| 154 | } |
||
| 155 | |||
| 156 | |||
| 157 | /** Evaluation : makes token and evaluate. |
||
| 158 | * Public function for expressions testing |
||
| 159 | * accepts : < > = <= >= != (typec = 0) |
||
| 160 | * operators : & | (typec=1) |
||
| 161 | * with : integers/float (type 0) or strings "" (type 1) or results (type 2) |
||
| 162 | * comparison int vs strings will return null (error) |
||
| 163 | * return : bool or null on error |
||
| 164 | */ |
||
| 165 | public function evaluation($rule,&$item) |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | // Remove all whitespaces (when not quoted) |
||
| 241 | public function eval_cleanup($rule) |
||
| 242 | { |
||
| 243 | $item=0; |
||
| 244 | $rule2=''; |
||
| 245 | while ($item < strlen($rule)) |
||
| 246 | { |
||
| 247 | if ($rule[$item]==' ') { $item++; continue; } |
||
| 248 | if ($rule[$item]=='"') |
||
| 249 | { |
||
| 250 | $rule2.=$rule[$item]; |
||
| 251 | $item++; |
||
| 252 | while (($rule[$item]!='"') && ($item < strlen($rule))) |
||
| 253 | { |
||
| 254 | $rule2.=$rule[$item]; |
||
| 255 | $item++; |
||
| 256 | } |
||
| 257 | if ($item == strlen ($rule)) throw new Exception("closing '\"' not found in ".$rule ." at " .$item); |
||
| 258 | $rule2.=$rule[$item]; |
||
| 259 | $item++; |
||
| 260 | continue; |
||
| 261 | } |
||
| 262 | |||
| 263 | $rule2.=$rule[$item]; |
||
| 264 | $item++; |
||
| 265 | } |
||
| 266 | |||
| 267 | return $rule2; |
||
| 268 | } |
||
| 269 | |||
| 270 | /** Evaluation rule (uses eval_* functions recursively) |
||
| 271 | * @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) ) |
||
| 272 | * @param array $oidList : OIDs values to sustitute. |
||
| 273 | * @return bool : true : rule match, false : rule don't match , throw exception on error. |
||
| 274 | */ |
||
| 275 | |||
| 276 | public function eval_rule($rule,$oidList) |
||
| 327 | } |
||
| 328 | |||
| 329 | } |