| Conditions | 10 |
| Paths | 76 |
| Total Lines | 46 |
| Code Lines | 27 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 |
||
| 53 | function index($sortfield = "code", $sortorder = 'ASC', $limit = 100, $page = 0, $type = '', $module = '', $sqlfilters = '') |
||
| 54 | { |
||
| 55 | $list = array(); |
||
| 56 | |||
| 57 | $sql = "SELECT id, code, type, libelle as label, module"; |
||
| 58 | $sql.= " FROM ".MAIN_DB_PREFIX."c_actioncomm as t"; |
||
| 59 | $sql.= " WHERE t.active = 1"; |
||
| 60 | if ($type) $sql.=" AND t.type LIKE '%" . $this->db->escape($type) . "%'"; |
||
| 61 | if ($module) $sql.=" AND t.module LIKE '%" . $this->db->escape($module) . "%'"; |
||
| 62 | // Add sql filters |
||
| 63 | if ($sqlfilters) |
||
| 64 | { |
||
| 65 | if (! DolibarrApi::_checkFilters($sqlfilters)) |
||
| 66 | { |
||
| 67 | throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); |
||
| 68 | } |
||
| 69 | $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; |
||
| 70 | $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; |
||
| 71 | } |
||
| 72 | |||
| 73 | |||
| 74 | $sql.= $this->db->order($sortfield, $sortorder); |
||
| 75 | |||
| 76 | if ($limit) { |
||
| 77 | if ($page < 0) { |
||
| 78 | $page = 0; |
||
| 79 | } |
||
| 80 | $offset = $limit * $page; |
||
| 81 | |||
| 82 | $sql .= $this->db->plimit($limit, $offset); |
||
| 83 | } |
||
| 84 | |||
| 85 | $result = $this->db->query($sql); |
||
| 86 | |||
| 87 | if ($result) { |
||
| 88 | $num = $this->db->num_rows($result); |
||
| 89 | $min = min($num, ($limit <= 0 ? $num : $limit)); |
||
| 90 | for ($i = 0; $i < $min; $i++) { |
||
| 91 | $list[] = $this->db->fetch_object($result); |
||
| 92 | } |
||
| 93 | } else { |
||
| 94 | throw new RestException(503, 'Error when retrieving list of events types : '.$this->db->lasterror()); |
||
| 95 | } |
||
| 96 | |||
| 97 | return $list; |
||
| 98 | } |
||
| 99 | |||
| 101 |