| Conditions | 18 |
| Paths | 345 |
| Total Lines | 142 |
| Code Lines | 92 |
| 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 |
||
| 54 | function loadBox($max=5) |
||
| 55 | { |
||
| 56 | global $user, $langs, $db, $conf, $hookmanager; |
||
| 57 | |||
| 58 | $this->max=$max; |
||
| 59 | |||
| 60 | include_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; |
||
| 61 | $productstatic=new Product($db); |
||
| 62 | |||
| 63 | $this->info_box_head = array('text' => $langs->trans("BoxTitleLastProducts",$max)); |
||
| 64 | |||
| 65 | if ($user->rights->produit->lire || $user->rights->service->lire) |
||
| 66 | { |
||
| 67 | $sql = "SELECT p.rowid, p.label, p.ref, p.price, p.price_base_type, p.price_ttc, p.fk_product_type, p.tms, p.tosell, p.tobuy, p.fk_price_expression, p.entity"; |
||
| 68 | $sql.= " FROM ".MAIN_DB_PREFIX."product as p"; |
||
| 69 | $sql.= ' WHERE p.entity IN ('.getEntity($productstatic->element, 1).')'; |
||
| 70 | if (empty($user->rights->produit->lire)) $sql.=' AND p.fk_product_type != 0'; |
||
| 71 | if (empty($user->rights->service->lire)) $sql.=' AND p.fk_product_type != 1'; |
||
| 72 | // Add where from hooks |
||
| 73 | if (is_object($hookmanager)) |
||
| 74 | { |
||
| 75 | $parameters=array(); |
||
| 76 | $reshook=$hookmanager->executeHooks('printFieldListWhere',$parameters); // Note that $action and $object may have been modified by hook |
||
| 77 | $sql.=$hookmanager->resPrint; |
||
| 78 | } |
||
| 79 | $sql.= $db->order('p.datec', 'DESC'); |
||
| 80 | $sql.= $db->plimit($max, 0); |
||
| 81 | |||
| 82 | $result = $db->query($sql); |
||
| 83 | if ($result) |
||
| 84 | { |
||
| 85 | $num = $db->num_rows($result); |
||
| 86 | $line = 0; |
||
| 87 | while ($line < $num) |
||
| 88 | { |
||
| 89 | $objp = $db->fetch_object($result); |
||
| 90 | $datem=$db->jdate($objp->tms); |
||
| 91 | |||
| 92 | // Multilangs |
||
| 93 | if (! empty($conf->global->MAIN_MULTILANGS)) // si l'option est active |
||
| 94 | { |
||
| 95 | $sqld = "SELECT label"; |
||
| 96 | $sqld.= " FROM ".MAIN_DB_PREFIX."product_lang"; |
||
| 97 | $sqld.= " WHERE fk_product=".$objp->rowid; |
||
| 98 | $sqld.= " AND lang='". $langs->getDefaultLang() ."'"; |
||
| 99 | $sqld.= " LIMIT 1"; |
||
| 100 | |||
| 101 | $resultd = $db->query($sqld); |
||
| 102 | if ($resultd) |
||
| 103 | { |
||
| 104 | $objtp = $db->fetch_object($resultd); |
||
| 105 | if (isset($objtp->label) && $objtp->label != '') |
||
| 106 | $objp->label = $objtp->label; |
||
| 107 | } |
||
| 108 | } |
||
| 109 | $productstatic->id = $objp->rowid; |
||
| 110 | $productstatic->ref = $objp->ref; |
||
| 111 | $productstatic->type = $objp->fk_product_type; |
||
| 112 | $productstatic->label = $objp->label; |
||
| 113 | $productstatic->entity = $objp->entity; |
||
| 114 | |||
| 115 | $this->info_box_contents[$line][] = array( |
||
| 116 | 'td' => '', |
||
| 117 | 'text' => $productstatic->getNomUrl(1), |
||
| 118 | 'asis' => 1, |
||
| 119 | ); |
||
| 120 | |||
| 121 | $this->info_box_contents[$line][] = array( |
||
| 122 | 'td' => 'class="tdoverflowmax100 maxwidth100onsmartphone"', |
||
| 123 | 'text' => $objp->label, |
||
| 124 | ); |
||
| 125 | |||
| 126 | if (empty($conf->dynamicprices->enabled) || empty($objp->fk_price_expression)) { |
||
| 127 | $price_base_type=$langs->trans($objp->price_base_type); |
||
| 128 | $price=($objp->price_base_type == 'HT')?price($objp->price):$price=price($objp->price_ttc); |
||
| 129 | } |
||
| 130 | else //Parse the dynamic price |
||
| 131 | { |
||
| 132 | $productstatic->fetch($objp->rowid, '', '', 1); |
||
| 133 | $priceparser = new PriceParser($this->db); |
||
| 134 | $price_result = $priceparser->parseProduct($productstatic); |
||
| 135 | if ($price_result >= 0) { |
||
| 136 | if ($objp->price_base_type == 'HT') |
||
| 137 | { |
||
| 138 | $price_base_type=$langs->trans("HT"); |
||
| 139 | } |
||
| 140 | else |
||
| 141 | { |
||
| 142 | $price_result = $price_result * (1 + ($productstatic->tva_tx / 100)); |
||
| 143 | $price_base_type=$langs->trans("TTC"); |
||
| 144 | } |
||
| 145 | $price=price($price_result); |
||
| 146 | } |
||
| 147 | } |
||
| 148 | $this->info_box_contents[$line][] = array( |
||
| 149 | 'td' => 'class="right"', |
||
| 150 | 'text' => $price, |
||
|
|
|||
| 151 | ); |
||
| 152 | |||
| 153 | $this->info_box_contents[$line][] = array( |
||
| 154 | 'td' => 'class="nowrap"', |
||
| 155 | 'text' => $price_base_type, |
||
| 156 | ); |
||
| 157 | |||
| 158 | $this->info_box_contents[$line][] = array( |
||
| 159 | 'td' => 'class="right"', |
||
| 160 | 'text' => dol_print_date($datem,'day'), |
||
| 161 | ); |
||
| 162 | |||
| 163 | $this->info_box_contents[$line][] = array( |
||
| 164 | 'td' => 'align="right" width="18"', |
||
| 165 | 'text' => $productstatic->LibStatut($objp->tosell,3,0), |
||
| 166 | ); |
||
| 167 | |||
| 168 | $this->info_box_contents[$line][] = array( |
||
| 169 | 'td' => 'align="right" width="18"', |
||
| 170 | 'text' => $productstatic->LibStatut($objp->tobuy,3,1), |
||
| 171 | ); |
||
| 172 | |||
| 173 | $line++; |
||
| 174 | } |
||
| 175 | if ($num==0) |
||
| 176 | $this->info_box_contents[$line][0] = array( |
||
| 177 | 'td' => 'align="center"', |
||
| 178 | 'text'=>$langs->trans("NoRecordedProducts"), |
||
| 179 | ); |
||
| 180 | |||
| 181 | $db->free($result); |
||
| 182 | } else { |
||
| 183 | $this->info_box_contents[0][0] = array( |
||
| 184 | 'td' => '', |
||
| 185 | 'maxlength'=>500, |
||
| 186 | 'text' => ($db->error().' sql='.$sql), |
||
| 187 | ); |
||
| 188 | } |
||
| 189 | } else { |
||
| 190 | $this->info_box_contents[0][0] = array( |
||
| 191 | 'td' => '', |
||
| 192 | 'text' => $langs->trans("ReadPermissionNotAllowed"), |
||
| 193 | ); |
||
| 194 | } |
||
| 195 | } |
||
| 196 | |||
| 212 |
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: