| Conditions | 24 |
| Paths | 11008 |
| Total Lines | 74 |
| Code Lines | 42 |
| 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 |
||
| 112 | function index($sortfield = "t.rowid", $sortorder = 'ASC', $limit = 100, $page = 0, $sqlfilters = '') { |
||
| 113 | global $db, $conf; |
||
| 114 | |||
| 115 | $obj_ret = array(); |
||
| 116 | |||
| 117 | $socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : ''; |
||
| 118 | |||
| 119 | // If the internal user must only see his customers, force searching by him |
||
| 120 | if (! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id; |
||
| 121 | |||
| 122 | $sql = "SELECT s.rowid"; |
||
| 123 | if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql .= ", sc.fk_soc, sc.fk_user"; // We need these fields in order to filter by sale (including the case where the user can only see his prospects) |
||
|
|
|||
| 124 | $sql.= " FROM ".MAIN_DB_PREFIX."myobject as s"; |
||
| 125 | |||
| 126 | if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc"; // We need this table joined to the select in order to filter by sale |
||
| 127 | $sql.= ", ".MAIN_DB_PREFIX."c_stcomm as st"; |
||
| 128 | $sql.= " WHERE s.fk_stcomm = st.id"; |
||
| 129 | |||
| 130 | // Example of use $mode |
||
| 131 | //if ($mode == 1) $sql.= " AND s.client IN (1, 3)"; |
||
| 132 | //if ($mode == 2) $sql.= " AND s.client IN (2, 3)"; |
||
| 133 | |||
| 134 | $sql.= ' AND s.entity IN ('.getEntity('myobject').')'; |
||
| 135 | if ((!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND s.fk_soc = sc.fk_soc"; |
||
| 136 | if ($socid) $sql.= " AND s.fk_soc = ".$socid; |
||
| 137 | if ($search_sale > 0) $sql.= " AND s.rowid = sc.fk_soc"; // Join for the needed table to filter by sale |
||
| 138 | // Insert sale filter |
||
| 139 | if ($search_sale > 0) |
||
| 140 | { |
||
| 141 | $sql .= " AND sc.fk_user = ".$search_sale; |
||
| 142 | } |
||
| 143 | if ($sqlfilters) |
||
| 144 | { |
||
| 145 | if (! DolibarrApi::_checkFilters($sqlfilters)) |
||
| 146 | { |
||
| 147 | throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); |
||
| 148 | } |
||
| 149 | $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; |
||
| 150 | $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; |
||
| 151 | } |
||
| 152 | |||
| 153 | $sql.= $db->order($sortfield, $sortorder); |
||
| 154 | if ($limit) { |
||
| 155 | if ($page < 0) |
||
| 156 | { |
||
| 157 | $page = 0; |
||
| 158 | } |
||
| 159 | $offset = $limit * $page; |
||
| 160 | |||
| 161 | $sql.= $db->plimit($limit + 1, $offset); |
||
| 162 | } |
||
| 163 | |||
| 164 | $result = $db->query($sql); |
||
| 165 | if ($result) |
||
| 166 | { |
||
| 167 | $num = $db->num_rows($result); |
||
| 168 | while ($i < $num) |
||
| 169 | { |
||
| 170 | $obj = $db->fetch_object($result); |
||
| 171 | $myobject_static = new MyObject($db); |
||
| 172 | if($myobject_static->fetch($obj->rowid)) { |
||
| 173 | $obj_ret[] = parent::_cleanObjectDatas($myobject_static); |
||
| 174 | } |
||
| 175 | $i++; |
||
| 176 | } |
||
| 177 | } |
||
| 178 | else { |
||
| 179 | throw new RestException(503, 'Error when retrieve myobject list'); |
||
| 180 | } |
||
| 181 | if( ! count($obj_ret)) { |
||
| 182 | throw new RestException(404, 'No myobject found'); |
||
| 183 | } |
||
| 184 | return $obj_ret; |
||
| 185 | } |
||
| 186 | |||
| 301 |
If you define a variable conditionally, it can happen that it is not defined for all execution paths.
Let’s take a look at an example:
In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.
Available Fixes
Check for existence of the variable explicitly:
Define a default value for the variable:
Add a value for the missing path: