| Conditions | 32 |
| Paths | > 20000 |
| Total Lines | 77 |
| 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 | { |
||
| 114 | global $db, $conf; |
||
| 115 | |||
| 116 | $obj_ret = array(); |
||
| 117 | |||
| 118 | $socid = DolibarrApiAccess::$user->societe_id ? DolibarrApiAccess::$user->societe_id : ''; |
||
| 119 | |||
| 120 | $restictonsocid = 0; // Set to 1 if there is a field socid in table of object |
||
| 121 | |||
| 122 | // If the internal user must only see his customers, force searching by him |
||
| 123 | if ($restictonsocid && ! DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) $search_sale = DolibarrApiAccess::$user->id; |
||
| 124 | |||
| 125 | $sql = "SELECT t.rowid"; |
||
| 126 | if ($restictonsocid && (!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) |
||
|
|
|||
| 127 | $sql.= " FROM ".MAIN_DB_PREFIX."myobject_mytable as t"; |
||
| 128 | |||
| 129 | if ($restictonsocid && (!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 |
||
| 130 | $sql.= " WHERE 1 = 1"; |
||
| 131 | |||
| 132 | // Example of use $mode |
||
| 133 | //if ($mode == 1) $sql.= " AND s.client IN (1, 3)"; |
||
| 134 | //if ($mode == 2) $sql.= " AND s.client IN (2, 3)"; |
||
| 135 | |||
| 136 | $tmpobject = new MyObject($db); |
||
| 137 | if ($tmpobject->ismultientitymanaged) $sql.= ' AND t.entity IN ('.getEntity('myobject').')'; |
||
| 138 | if ($restictonsocid && (!DolibarrApiAccess::$user->rights->societe->client->voir && !$socid) || $search_sale > 0) $sql.= " AND t.fk_soc = sc.fk_soc"; |
||
| 139 | if ($restictonsocid && $socid) $sql.= " AND t.fk_soc = ".$socid; |
||
| 140 | if ($restictonsocid && $search_sale > 0) $sql.= " AND t.rowid = sc.fk_soc"; // Join for the needed table to filter by sale |
||
| 141 | // Insert sale filter |
||
| 142 | if ($restictonsocid && $search_sale > 0) |
||
| 143 | { |
||
| 144 | $sql .= " AND sc.fk_user = ".$search_sale; |
||
| 145 | } |
||
| 146 | if ($sqlfilters) |
||
| 147 | { |
||
| 148 | if (! DolibarrApi::_checkFilters($sqlfilters)) |
||
| 149 | { |
||
| 150 | throw new RestException(503, 'Error when validating parameter sqlfilters '.$sqlfilters); |
||
| 151 | } |
||
| 152 | $regexstring='\(([^:\'\(\)]+:[^:\'\(\)]+:[^:\(\)]+)\)'; |
||
| 153 | $sql.=" AND (".preg_replace_callback('/'.$regexstring.'/', 'DolibarrApi::_forge_criteria_callback', $sqlfilters).")"; |
||
| 154 | } |
||
| 155 | |||
| 156 | $sql.= $db->order($sortfield, $sortorder); |
||
| 157 | if ($limit) { |
||
| 158 | if ($page < 0) |
||
| 159 | { |
||
| 160 | $page = 0; |
||
| 161 | } |
||
| 162 | $offset = $limit * $page; |
||
| 163 | |||
| 164 | $sql.= $db->plimit($limit + 1, $offset); |
||
| 165 | } |
||
| 166 | |||
| 167 | $result = $db->query($sql); |
||
| 168 | if ($result) |
||
| 169 | { |
||
| 170 | $num = $db->num_rows($result); |
||
| 171 | while ($i < $num) |
||
| 172 | { |
||
| 173 | $obj = $db->fetch_object($result); |
||
| 174 | $myobject_static = new MyObject($db); |
||
| 175 | if($myobject_static->fetch($obj->rowid)) { |
||
| 176 | $obj_ret[] = $this->_cleanObjectDatas($myobject_static); |
||
| 177 | } |
||
| 178 | $i++; |
||
| 179 | } |
||
| 180 | } |
||
| 181 | else { |
||
| 182 | throw new RestException(503, 'Error when retrieve myobject list'); |
||
| 183 | } |
||
| 184 | if( ! count($obj_ret)) { |
||
| 185 | throw new RestException(404, 'No myobject found'); |
||
| 186 | } |
||
| 187 | return $obj_ret; |
||
| 188 | } |
||
| 189 | |||
| 325 |
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: