| Total Complexity | 87 |
| Total Lines | 629 |
| Duplicated Lines | 0 % |
| Changes | 6 | ||
| Bugs | 1 | Features | 0 |
Complex classes like Mib 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 Mib, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 10 | class Mib |
||
| 11 | { |
||
| 12 | |||
| 13 | protected $logging; //< logging class |
||
| 14 | protected $trapsDB; //< Database class |
||
| 15 | |||
| 16 | public $snmptranslate; |
||
| 17 | public $snmptranslateDirs; |
||
| 18 | |||
| 19 | private $dbOidAll; //< All oid in database; |
||
| 20 | private $dbOidIndex; //< Index of oid in dbOidAll |
||
| 21 | private $objectsAll; //< output lines of snmptranslate list |
||
| 22 | private $trapObjectsIndex; //< array of traps objects (as OID) |
||
| 23 | |||
| 24 | private $oidDesc=array(); //< $oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL |
||
| 25 | |||
| 26 | // Timing vars for update |
||
| 27 | private $timing=array(); |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Setup Mib Class |
||
| 31 | * @param Logging $logClass : where to log |
||
| 32 | * @param Database $dbClass : Database |
||
| 33 | */ |
||
| 34 | function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
||
| 40 | |||
| 41 | } |
||
| 42 | |||
| 43 | |||
| 44 | /** |
||
| 45 | * Update or add an OID to database uses $this->dbOidIndex for mem cache |
||
| 46 | * and $this->oidDesc doe data |
||
| 47 | * @return number : 0=unchanged, 1 = changed, 2=created |
||
| 48 | */ |
||
| 49 | public function update_oid() |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * get all objects for a trap. |
||
| 160 | * @param integer $trapId |
||
| 161 | * @return array : array of cached objects |
||
| 162 | */ |
||
| 163 | private function cache_db_objects($trapId) |
||
| 164 | { |
||
| 165 | $dbObjects=array(); // cache of objects for trap in db |
||
| 166 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 167 | // Get all objects |
||
| 168 | $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
||
| 169 | $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
||
| 170 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 171 | $this->logging->log('No result in query : ' . $sql,1,''); |
||
| 172 | } |
||
| 173 | $dbObjectsRaw=$ret_code->fetchAll(); |
||
| 174 | |||
| 175 | foreach ($dbObjectsRaw as $val) |
||
| 176 | { |
||
| 177 | $dbObjects[$val['object_id']]=1; |
||
| 178 | } |
||
| 179 | return $dbObjects; |
||
| 180 | } |
||
| 181 | |||
| 182 | /** |
||
| 183 | * Get object details & mib , returns snmptranslate output |
||
| 184 | * @param string $object : object name |
||
| 185 | * @param string $trapmib : mib of trap |
||
| 186 | * @return NULL|array : null if not found, or output of snmptranslate |
||
| 187 | */ |
||
| 188 | private function get_object_details($object,$trapmib) |
||
| 189 | { |
||
| 190 | $match=$snmptrans=array(); |
||
| 191 | $retVal=0; |
||
| 192 | $this->oidDesc['mib']=$trapmib; |
||
| 193 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
||
| 194 | ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
||
| 195 | if ($retVal!=0) |
||
| 196 | { |
||
| 197 | // Maybe not trap mib, search with IR |
||
| 198 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
||
| 199 | ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
||
| 200 | if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
||
| 201 | { // Not found -> continue with warning |
||
| 202 | $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
||
| 203 | return null; |
||
| 204 | } |
||
| 205 | $this->oidDesc['mib']=$match[1]; |
||
| 206 | |||
| 207 | // Do the snmptranslate again. |
||
| 208 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
||
| 209 | ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal); |
||
| 210 | if ($retVal!=0) { |
||
| 211 | $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,''); |
||
| 212 | return null; |
||
| 213 | } |
||
| 214 | |||
| 215 | } |
||
| 216 | return $snmptrans; |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Parse snmptranslate output and set $this->oidDesc with elements |
||
| 221 | * @param array $snmptrans : multi line output of snmptrans |
||
| 222 | */ |
||
| 223 | private function parse_object($snmptrans) |
||
| 278 | } |
||
| 279 | } |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * create or update (with check_existing = true) objects of trap |
||
| 284 | * @param string $trapOID : trap oid |
||
| 285 | * @param string $trapmib : mib of trap |
||
| 286 | * @param array $objects : array of objects name (without MIB) |
||
| 287 | * @param bool $check_existing : check instead of create |
||
| 288 | */ |
||
| 289 | public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
||
| 340 | // TODO : remove link trap - objects that wasn't marked. |
||
| 341 | } |
||
| 342 | |||
| 343 | } |
||
| 344 | |||
| 345 | private function reset_oidDesc() |
||
| 346 | { |
||
| 347 | $this->oidDesc['oid']=null; |
||
| 348 | $this->oidDesc['name']=null; |
||
| 349 | $this->oidDesc['type']=null; |
||
| 350 | $this->oidDesc['mib']=null; |
||
| 351 | $this->oidDesc['textconv']=null; |
||
| 352 | $this->oidDesc['dispHint'] =null; |
||
| 353 | $this->oidDesc['syntax']=null; |
||
| 354 | $this->oidDesc['type_enum']=null; |
||
| 355 | $this->oidDesc['description']=null; |
||
| 356 | } |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Fills $this->objectsAll with all mibs from snmptranslate |
||
| 360 | * @return integer : number of elements |
||
| 361 | */ |
||
| 362 | private function load_mibs_snmptranslate() |
||
| 363 | { |
||
| 364 | $retVal=0; |
||
| 365 | // Get all mib objects from all mibs |
||
| 366 | $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
||
| 367 | $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
||
| 368 | unset($this->objectsAll); |
||
| 369 | exec($snmpCommand,$this->objectsAll,$retVal); |
||
| 370 | if ($retVal!=0) |
||
| 371 | { |
||
| 372 | $this->logging->log('error executing snmptranslate',ERROR,''); |
||
| 373 | } |
||
| 374 | // Count elements to show progress |
||
| 375 | $numElements=count($this->objectsAll); |
||
| 376 | $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
||
| 377 | return $numElements; |
||
| 378 | } |
||
| 379 | |||
| 380 | /** |
||
| 381 | * load all mib objects db in dbOidAll (raw) and index in dbOidIndex |
||
| 382 | */ |
||
| 383 | private function load_mibs_from_db() |
||
| 384 | { |
||
| 385 | // Get all mibs from databse to have a memory index |
||
| 386 | |||
| 387 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 388 | |||
| 389 | $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
||
| 390 | $this->logging->log('SQL query : '.$sql,DEBUG ); |
||
| 391 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 392 | $this->logging->log('No result in query : ' . $sql,ERROR,''); |
||
| 393 | } |
||
| 394 | $this->dbOidAll=$ret_code->fetchAll(); |
||
| 395 | $this->dbOidIndex=array(); |
||
| 396 | // Create the index for db; |
||
| 397 | foreach($this->dbOidAll as $key=>$val) |
||
| 398 | { |
||
| 399 | $this->dbOidIndex[$val['oid']]['key']=$key; |
||
| 400 | $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
||
| 401 | } |
||
| 402 | } |
||
| 403 | |||
| 404 | /** |
||
| 405 | * Reset all update timers & count to zero |
||
| 406 | */ |
||
| 407 | private function reset_update_timers() |
||
| 408 | { |
||
| 409 | $this->timing['base_parse_time']=0; |
||
| 410 | $this->timing['base_check_time']=0; |
||
| 411 | $this->timing['type0_check_time']=0; |
||
| 412 | $this->timing['nottrap_time']=0; |
||
| 413 | $this->timing['update_time']=0; |
||
| 414 | $this->timing['objects_time']=0; |
||
| 415 | $this->timing['base_parse_num']=0; |
||
| 416 | $this->timing['base_check_num']=0; |
||
| 417 | $this->timing['type0_check_num']=0; |
||
| 418 | $this->timing['nottrap_num']=0; |
||
| 419 | $this->timing['update_num']=0; |
||
| 420 | $this->timing['objects_num']=0; |
||
| 421 | $this->timing['num_traps']=0; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Detect if $this->objectsAll[$curElement] is a trap |
||
| 426 | * @param integer $curElement |
||
| 427 | * @param bool $onlyTraps : set to false to get all and not only traps. |
||
| 428 | * @return boolean : false if it's a trap , true if not |
||
| 429 | */ |
||
| 430 | private function detect_trap($curElement,$onlyTraps) |
||
| 485 | } |
||
| 486 | |||
| 487 | |||
| 488 | private function get_trap_mib_description() |
||
| 523 | } |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Cache mib in database |
||
| 527 | * @param boolean $display_progress : Display progress on standard output |
||
| 528 | * @param boolean $check_change : Force check of trap params & objects |
||
| 529 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
||
| 530 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
||
| 531 | */ |
||
| 532 | public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
||
| 639 | } |
||
| 640 | } |
||
| 641 | |||
| 642 | |||
| 643 | } |
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.