| Conditions | 12 |
| Paths | 151 |
| Total Lines | 94 |
| Code Lines | 54 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | <?php |
||
| 45 | public function trapdetailAction() |
||
| 46 | { |
||
| 47 | |||
| 48 | $this->checkReadPermission(); |
||
| 49 | // set up tab |
||
| 50 | $this->getTabs()->add('get',array( |
||
| 51 | 'active' => true, |
||
| 52 | 'label' => $this->translate('Detailed status'), |
||
| 53 | 'url' => Url::fromRequest() |
||
| 54 | )); |
||
| 55 | // get id |
||
| 56 | $trapid=$this->params->get('id'); |
||
| 57 | $this->view->trapid=$trapid; |
||
| 58 | $queryArray=$this->getModuleConfig()->trapDetailQuery(); |
||
| 59 | |||
| 60 | $dbConn = $this->getUIDatabase()->getDbConn(); |
||
| 61 | if ($dbConn === null) throw new \ErrorException('uncatched db error'); |
||
| 62 | |||
| 63 | // URL to add a handler |
||
| 64 | $this->view->addHandlerUrl=Url::fromPath( |
||
| 65 | $this->getModuleConfig()->urlPath() . '/handler/add', |
||
| 66 | array('fromid' => $trapid)); |
||
| 67 | // *************** Get main data |
||
| 68 | // extract columns and titles; |
||
| 69 | $elmts=NULL; |
||
| 70 | foreach ($queryArray as $key => $val) { |
||
| 71 | $elmts[$key]=$val[1]; |
||
| 72 | } |
||
| 73 | |||
| 74 | // Do DB query for trap. |
||
| 75 | try |
||
| 76 | { |
||
| 77 | $query = $dbConn->select() |
||
| 78 | ->from($this->moduleConfig->getTrapTableName(),$elmts) |
||
| 79 | ->where('id=?',$trapid); |
||
| 80 | $trapDetail=$dbConn->fetchRow($query); |
||
| 81 | if ( $trapDetail == null) throw new Exception('No traps was found with id = '.$trapid); |
||
| 82 | } |
||
| 83 | catch (Exception $e) |
||
| 84 | { |
||
| 85 | $this->displayExitError('Trap detail',$e->getMessage()); |
||
| 86 | return; |
||
| 87 | } |
||
| 88 | |||
| 89 | // Store result in array (with Titles). |
||
| 90 | foreach ($queryArray as $key => $val) { |
||
| 91 | if ($key == 'timestamp') { |
||
| 92 | $cval=strftime('%c',$trapDetail->$key); |
||
| 93 | } else { |
||
| 94 | $cval=$trapDetail->$key; |
||
| 95 | } |
||
| 96 | array_push($queryArray[$key],$cval); |
||
| 97 | } |
||
| 98 | $this->view->rowset = $queryArray; |
||
| 99 | |||
| 100 | // ************** Check for additionnal data |
||
| 101 | |||
| 102 | // extract columns and titles; |
||
| 103 | $queryArrayData=$this->getModuleConfig()->trapDataDetailQuery(); |
||
| 104 | $data_elmts=NULL; |
||
| 105 | foreach ($queryArrayData as $key => $val) { |
||
| 106 | $data_elmts[$key]=$val[1]; |
||
| 107 | } |
||
| 108 | try |
||
| 109 | { |
||
| 110 | $query = $dbConn->select() |
||
| 111 | ->from($this->moduleConfig->getTrapDataTableName(),$data_elmts) |
||
| 112 | ->where('trap_id=?',$trapid); |
||
| 113 | $trapDetail=$dbConn->fetchAll($query); |
||
| 114 | } |
||
| 115 | catch (Exception $e) |
||
| 116 | { |
||
| 117 | $this->displayExitError('Trap detail',$e->getMessage()); |
||
| 118 | } |
||
| 119 | // TODO : code this in a better & simpler way |
||
| 120 | if ($trapDetail == null ) |
||
| 121 | { |
||
| 122 | $this->view->data=false; |
||
| 123 | } |
||
| 124 | else |
||
| 125 | { |
||
| 126 | $this->view->data=true; |
||
| 127 | // Store result in array. |
||
| 128 | $trapval=array(); |
||
| 129 | foreach ($trapDetail as $key => $val) |
||
| 130 | { |
||
| 131 | $trapval[$key]=array(); |
||
| 132 | foreach (array_keys($queryArrayData) as $vkey ) |
||
| 133 | { |
||
| 134 | array_push($trapval[$key],$val->$vkey); |
||
| 135 | } |
||
| 136 | } |
||
| 137 | $this->view->data_val=$trapval; |
||
| 138 | $this->view->data_title=$queryArrayData; |
||
| 139 | } |
||
| 231 | } |