| Total Complexity | 86 |
| Total Lines | 674 |
| Duplicated Lines | 0 % |
| Changes | 9 | ||
| Bugs | 3 | 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 |
||
| 11 | class Mib |
||
| 12 | { |
||
| 13 | |||
| 14 | protected $logging; //< logging class |
||
| 15 | protected $trapsDB; //< Database class |
||
| 16 | |||
| 17 | public $snmptranslate; |
||
| 18 | public $snmptranslateDirs; |
||
| 19 | |||
| 20 | private $dbOidAll; //< All oid in database; |
||
| 21 | private $dbOidIndex; //< Index of oid in dbOidAll |
||
| 22 | private $objectsAll; //< output lines of snmptranslate list |
||
| 23 | private $trapObjectsIndex; //< array of traps objects (as OID) |
||
| 24 | |||
| 25 | private $oidDesc=array(); //< $oid,$mib,$name,$type,$textConv,$dispHint,$syntax,$type_enum,$description=NULL |
||
| 26 | |||
| 27 | // Timing vars for update |
||
| 28 | private $timing=array(); |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Setup Mib Class |
||
| 32 | * @param Logging $logClass : where to log |
||
| 33 | * @param Database $dbClass : Database |
||
| 34 | */ |
||
| 35 | function __construct($logClass,$dbClass,$snmptrans,$snmptransdir) |
||
| 41 | |||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Update object in DB with object in dbOidIndex if name/mib/type has changed. |
||
| 46 | * @return number : 0=unchanged, 1 = changed, 2=created |
||
| 47 | */ |
||
| 48 | private function update_oid_update() |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Create object in DB with object in dbOidIndex |
||
| 97 | * @return number : 0=unchanged, 1 = changed, 2=created |
||
| 98 | */ |
||
| 99 | private function update_oid_create() |
||
| 100 | { |
||
| 101 | // Insert data |
||
| 102 | |||
| 103 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 104 | $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache '. |
||
| 105 | '(oid, name, type , mib, textual_convention, display_hint '. |
||
| 106 | ', syntax, type_enum , description ) ' . |
||
| 107 | 'values (:oid, :name , :type ,:mib ,:tc , :display_hint'. |
||
| 108 | ', :syntax, :type_enum, :description )'; |
||
| 109 | |||
| 110 | if ($this->trapsDB->trapDBType == 'pgsql') $sql .= 'RETURNING id'; |
||
| 111 | |||
| 112 | $sqlQuery=$db_conn->prepare($sql); |
||
| 113 | |||
| 114 | $sqlParam=array( |
||
| 115 | ':oid' => $this->oidDesc['oid'], |
||
| 116 | ':name' => $this->oidDesc['name'], |
||
| 117 | ':type' => $this->oidDesc['type'], |
||
| 118 | ':mib' => $this->oidDesc['mib'], |
||
| 119 | ':tc' => $this->oidDesc['textconv']??'null', |
||
| 120 | ':display_hint' => $this->oidDesc['dispHint']??'null', |
||
| 121 | ':syntax' => $this->oidDesc['syntax']??'null', |
||
| 122 | ':type_enum' => $this->oidDesc['type_enum']??'null', |
||
| 123 | ':description' => $this->oidDesc['description']??'null' |
||
| 124 | ); |
||
| 125 | |||
| 126 | if ($sqlQuery->execute($sqlParam) === false) { |
||
| 127 | $this->logging->log('Error in query : ' . $sql,1,''); |
||
| 128 | } |
||
| 129 | |||
| 130 | switch ($this->trapsDB->trapDBType) |
||
| 131 | { |
||
| 132 | case 'pgsql': |
||
| 133 | // Get last id to insert oid/values in secondary table |
||
| 134 | if (($inserted_id_ret=$sqlQuery->fetch(PDO::FETCH_ASSOC)) === false) { |
||
| 135 | $this->logging->log('Error getting id - pgsql - ',1,''); |
||
| 136 | } |
||
| 137 | if (! isset($inserted_id_ret['id'])) { |
||
| 138 | $this->logging->log('Error getting id - pgsql - empty.',ERROR); |
||
| 139 | return 0; |
||
| 140 | } |
||
| 141 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id_ret['id']; |
||
| 142 | break; |
||
| 143 | case 'mysql': |
||
| 144 | // Get last id to insert oid/values in secondary table |
||
| 145 | $sql='SELECT LAST_INSERT_ID();'; |
||
| 146 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 147 | $this->logging->log('Erreur getting id - mysql - ',ERROR); |
||
| 148 | return 0; |
||
| 149 | } |
||
| 150 | |||
| 151 | $inserted_id=$ret_code->fetch(PDO::FETCH_ASSOC)['LAST_INSERT_ID()']; |
||
| 152 | if ($inserted_id==false) throw new Exception("Weird SQL error : last_insert_id returned false : open issue"); |
||
| 153 | $this->dbOidIndex[$this->oidDesc['oid']]['id']=$inserted_id; |
||
| 154 | break; |
||
| 155 | default: |
||
| 156 | $this->logging->log('Error SQL type Unknown : '.$this->trapsDB->trapDBType,ERROR); |
||
| 157 | return 0; |
||
| 158 | } |
||
| 159 | |||
| 160 | // Set as newly created. |
||
| 161 | $this->dbOidIndex[$this->oidDesc['oid']]['key']=-1; |
||
| 162 | return 2; |
||
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Update or add an OID to database uses $this->dbOidIndex for mem cache |
||
| 167 | * and $this->oidDesc doe data |
||
| 168 | * @return number : 0=unchanged, 1 = changed, 2=created |
||
| 169 | */ |
||
| 170 | public function update_oid() |
||
| 182 | |||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * get all objects for a trap. |
||
| 187 | * @param integer $trapId |
||
| 188 | * @return array : array of cached objects |
||
| 189 | */ |
||
| 190 | private function cache_db_objects($trapId) |
||
| 191 | { |
||
| 192 | $dbObjects=array(); // cache of objects for trap in db |
||
| 193 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 194 | // Get all objects |
||
| 195 | $sql='SELECT * FROM '.$this->trapsDB->dbPrefix.'mib_cache_trap_object where trap_id='.$trapId.';'; |
||
| 196 | $this->logging->log('SQL query get all traps: '.$sql,DEBUG ); |
||
| 197 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 198 | $this->logging->log('No result in query : ' . $sql,1,''); |
||
| 199 | } |
||
| 200 | $dbObjectsRaw=$ret_code->fetchAll(); |
||
| 201 | |||
| 202 | foreach ($dbObjectsRaw as $val) |
||
| 203 | { |
||
| 204 | $dbObjects[$val['object_id']]=1; |
||
| 205 | } |
||
| 206 | return $dbObjects; |
||
| 207 | } |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Get object details & mib , returns snmptranslate output |
||
| 211 | * @param string $object : object name |
||
| 212 | * @param string $trapmib : mib of trap |
||
| 213 | * @return NULL|array : null if not found, or output of snmptranslate |
||
| 214 | */ |
||
| 215 | private function get_object_details($object,$trapmib) |
||
| 216 | { |
||
| 217 | $match=$snmptrans=array(); |
||
| 218 | $retVal=0; |
||
| 219 | $this->oidDesc['mib']=$trapmib; |
||
| 220 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
||
| 221 | ' -On -Td '.$this->oidDesc['mib'].'::'.$object . ' 2>/dev/null',$snmptrans,$retVal); |
||
| 222 | if ($retVal!=0) |
||
| 223 | { |
||
| 224 | // Maybe not trap mib, search with IR |
||
| 225 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
||
| 226 | ' -IR '.$object . ' 2>/dev/null',$snmptrans,$retVal); |
||
| 227 | if ($retVal != 0 || !preg_match('/(.*)::(.*)/',$snmptrans[0],$match)) |
||
| 228 | { // Not found -> continue with warning |
||
| 229 | $this->logging->log('Error finding trap object : '.$trapmib.'::'.$object,2,''); |
||
| 230 | return null; |
||
| 231 | } |
||
| 232 | $this->oidDesc['mib']=$match[1]; |
||
| 233 | |||
| 234 | // Do the snmptranslate again. |
||
| 235 | exec($this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs. |
||
| 236 | ' -On -Td '.$this->oidDesc['mib'].'::'.$object,$snmptrans,$retVal); |
||
| 237 | if ($retVal!=0) { |
||
| 238 | $this->logging->log('Error finding trap object : '.$this->oidDesc['mib'].'::'.$object,2,''); |
||
| 239 | return null; |
||
| 240 | } |
||
| 241 | |||
| 242 | } |
||
| 243 | return $snmptrans; |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Parse snmptranslate output and set $this->oidDesc with elements |
||
| 248 | * @param array $snmptrans : multi line output of snmptrans |
||
| 249 | */ |
||
| 250 | private function parse_object($snmptrans) |
||
| 305 | } |
||
| 306 | } |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * create or update (with check_existing = true) objects of trap |
||
| 311 | * @param string $trapOID : trap oid |
||
| 312 | * @param string $trapmib : mib of trap |
||
| 313 | * @param array $objects : array of objects name (without MIB) |
||
| 314 | * @param bool $check_existing : check instead of create |
||
| 315 | */ |
||
| 316 | public function trap_objects($trapOID,$trapmib,$objects,$check_existing) |
||
| 317 | { |
||
| 318 | $trapId = $this->dbOidIndex[$trapOID]['id']; // Get id of trap |
||
| 319 | |||
| 320 | if ($check_existing === true) |
||
| 321 | { |
||
| 322 | $dbObjects=$this->cache_db_objects($trapId); |
||
| 323 | } |
||
| 324 | |||
| 325 | foreach ($objects as $object) |
||
| 326 | { |
||
| 327 | |||
| 328 | $this->reset_oidDesc(); |
||
| 329 | |||
| 330 | $snmptrans=$this->get_object_details($object, $trapmib); // Get object mib & details |
||
|
1 ignored issue
–
show
|
|||
| 331 | if ($snmptrans === null) continue; // object not found |
||
| 332 | |||
| 333 | $this->parse_object($snmptrans); |
||
| 334 | |||
| 335 | $this->oidDesc['name'] = $object; |
||
| 336 | |||
| 337 | $this->logging->log("Adding object ".$this->oidDesc['name']." : ".$this->oidDesc['oid']." / ".$this->oidDesc['syntax']." / ".$this->oidDesc['type_enum']." / ".$this->oidDesc['dispHint']." / ".$this->oidDesc['textconv'],DEBUG ); |
||
| 338 | |||
| 339 | // Update |
||
| 340 | $this->update_oid(); |
||
| 341 | |||
| 342 | if (isset($dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']])) |
||
| 343 | { // if link exists, continue |
||
| 344 | $dbObjects[$this->dbOidIndex[$this->oidDesc['oid']]['id']]=2; |
||
| 345 | continue; |
||
| 346 | } |
||
| 347 | if ($check_existing === true) |
||
| 348 | { |
||
| 349 | // TODO : check link trap - objects exists, mark them. |
||
| 350 | } |
||
| 351 | // Associate in object table |
||
| 352 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 353 | $sql='INSERT INTO '.$this->trapsDB->dbPrefix.'mib_cache_trap_object (trap_id,object_id) '. |
||
| 354 | 'values (:trap_id, :object_id)'; |
||
| 355 | $sqlQuery=$db_conn->prepare($sql); |
||
| 356 | $sqlParam=array( |
||
| 357 | ':trap_id' => $trapId, |
||
| 358 | ':object_id' => $this->dbOidIndex[$this->oidDesc['oid']]['id'], |
||
| 359 | ); |
||
| 360 | |||
| 361 | if ($sqlQuery->execute($sqlParam) === false) { |
||
| 362 | $this->logging->log('Error adding trap object : ' . $sql . ' / ' . $trapId . '/'. $this->dbOidIndex[$this->oidDesc['oid']]['id'] ,1,''); |
||
| 363 | } |
||
| 364 | } |
||
| 365 | if ($check_existing === true) |
||
| 366 | { |
||
| 367 | // TODO : remove link trap - objects that wasn't marked. |
||
| 368 | } |
||
| 369 | |||
| 370 | } |
||
| 371 | |||
| 372 | private function reset_oidDesc() |
||
| 373 | { |
||
| 374 | $this->oidDesc['oid']=null; |
||
| 375 | $this->oidDesc['name']=null; |
||
| 376 | $this->oidDesc['type']=null; |
||
| 377 | $this->oidDesc['mib']=null; |
||
| 378 | $this->oidDesc['textconv']=null; |
||
| 379 | $this->oidDesc['dispHint'] =null; |
||
| 380 | $this->oidDesc['syntax']=null; |
||
| 381 | $this->oidDesc['type_enum']=null; |
||
| 382 | $this->oidDesc['description']=null; |
||
| 383 | } |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Fills $this->objectsAll with all mibs from snmptranslate |
||
| 387 | * @return integer : number of elements |
||
| 388 | */ |
||
| 389 | private function load_mibs_snmptranslate() |
||
| 390 | { |
||
| 391 | $retVal=0; |
||
| 392 | // Get all mib objects from all mibs |
||
| 393 | $snmpCommand=$this->snmptranslate . ' -m ALL -M +'.$this->snmptranslateDirs.' -On -Tto 2>/dev/null'; |
||
| 394 | $this->logging->log('Getting all traps : '.$snmpCommand,DEBUG ); |
||
| 395 | unset($this->objectsAll); |
||
| 396 | exec($snmpCommand,$this->objectsAll,$retVal); |
||
| 397 | if ($retVal!=0) |
||
| 398 | { |
||
| 399 | $this->logging->log('error executing snmptranslate',ERROR,''); |
||
| 400 | } |
||
| 401 | // Count elements to show progress |
||
| 402 | $numElements=count($this->objectsAll); |
||
| 403 | $this->logging->log('Total snmp objects returned by snmptranslate : '.$numElements,INFO ); |
||
| 404 | return $numElements; |
||
| 405 | } |
||
| 406 | |||
| 407 | /** |
||
| 408 | * load all mib objects db in dbOidAll (raw) and index in dbOidIndex |
||
| 409 | */ |
||
| 410 | private function load_mibs_from_db() |
||
| 411 | { |
||
| 412 | // Get all mibs from databse to have a memory index |
||
| 413 | |||
| 414 | $db_conn=$this->trapsDB->db_connect_trap(); |
||
| 415 | |||
| 416 | $sql='SELECT * from '.$this->trapsDB->dbPrefix.'mib_cache;'; |
||
| 417 | $this->logging->log('SQL query : '.$sql,DEBUG ); |
||
| 418 | if (($ret_code=$db_conn->query($sql)) === false) { |
||
| 419 | $this->logging->log('No result in query : ' . $sql,ERROR,''); |
||
| 420 | } |
||
| 421 | $this->dbOidAll=$ret_code->fetchAll(); |
||
| 422 | $this->dbOidIndex=array(); |
||
| 423 | // Create the index for db; |
||
| 424 | foreach($this->dbOidAll as $key=>$val) |
||
| 425 | { |
||
| 426 | $this->dbOidIndex[$val['oid']]['key']=$key; |
||
| 427 | $this->dbOidIndex[$val['oid']]['id']=$val['id']; |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Reset all update timers & count to zero |
||
| 433 | */ |
||
| 434 | private function reset_update_timers() |
||
| 435 | { |
||
| 436 | $this->timing['base_parse_time']=0; |
||
| 437 | $this->timing['base_check_time']=0; |
||
| 438 | $this->timing['type0_check_time']=0; |
||
| 439 | $this->timing['nottrap_time']=0; |
||
| 440 | $this->timing['update_time']=0; |
||
| 441 | $this->timing['objects_time']=0; |
||
| 442 | $this->timing['base_parse_num']=0; |
||
| 443 | $this->timing['base_check_num']=0; |
||
| 444 | $this->timing['type0_check_num']=0; |
||
| 445 | $this->timing['nottrap_num']=0; |
||
| 446 | $this->timing['update_num']=0; |
||
| 447 | $this->timing['objects_num']=0; |
||
| 448 | $this->timing['num_traps']=0; |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Detect if $this->objectsAll[$curElement] is a trap |
||
| 453 | * @param integer $curElement |
||
| 454 | * @param bool $onlyTraps : set to false to get all and not only traps. |
||
| 455 | * @return boolean : false if it's a trap , true if not |
||
| 456 | */ |
||
| 457 | private function detect_trap($curElement,$onlyTraps) |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * get_trap_mib_description |
||
| 516 | * @return array|null : array of snmptranslate output or null on error |
||
| 517 | **/ |
||
| 518 | private function get_trap_mib_description() |
||
| 555 | } |
||
| 556 | |||
| 557 | /** |
||
| 558 | * Get trap objects |
||
| 559 | * @param array $snmptrans : output of snmptranslate for TrapModuleConfig |
||
| 560 | * @return array|null : array of objects or null if not found |
||
| 561 | **/ |
||
| 562 | private function get_trap_objects($snmptrans) |
||
| 563 | { |
||
| 564 | $objectName=null; |
||
| 565 | $match=array(); |
||
| 566 | foreach ($snmptrans as $line) |
||
| 567 | { |
||
| 568 | if (preg_match('/OBJECTS.*\{([^\}]+)\}/',$line,$match)) |
||
| 569 | { |
||
| 570 | $objectName=$match[1]; |
||
| 571 | } |
||
| 572 | } |
||
| 573 | if ($objectName == null) |
||
| 574 | { |
||
| 575 | $this->logging->log('No objects for ' . $this->oidDesc['oid'],DEBUG); |
||
| 576 | $this->timing['objects_time'] += microtime(true) - $this->timing['base_time']; |
||
| 577 | return null; |
||
| 578 | } |
||
| 579 | |||
| 580 | $trapObjects=array(); |
||
| 581 | while (preg_match('/ *([^ ,]+) *,* */',$objectName,$match)) |
||
| 582 | { |
||
| 583 | array_push($trapObjects,$match[1]); |
||
| 584 | $objectName=preg_replace('/'.$match[0].'/','',$objectName); |
||
| 585 | } |
||
| 586 | return $trapObjects; |
||
| 587 | } |
||
| 588 | |||
| 589 | /** |
||
| 590 | * Cache mib in database |
||
| 591 | * @param boolean $display_progress : Display progress on standard output |
||
| 592 | * @param boolean $check_change : Force check of trap params & objects |
||
| 593 | * @param boolean $onlyTraps : only cache traps and objects (true) or all (false) |
||
| 594 | * @param string $startOID : only cache under startOID (NOT IMPLEMENTED) |
||
| 595 | */ |
||
| 596 | public function update_mib_database($display_progress=false,$check_change=false,$onlyTraps=true,$startOID='.1') |
||
| 685 | } |
||
| 686 | } |
||
| 687 | |||
| 688 | |||
| 689 | } |
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.