| Conditions | 14 |
| Paths | 66 |
| Total Lines | 74 |
| 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 |
||
| 131 | function precharger_traduction_objet($type, $id_objet, $id_rubrique = 0, $lier_trad = 0, $champ_titre = 'titre') { |
||
| 132 | $table = table_objet_sql($type); |
||
| 133 | $_id_objet = id_table_objet($table); |
||
| 134 | |||
| 135 | // Recuperer les donnees de l'objet original |
||
| 136 | $row = sql_fetsel("*", $table, "$_id_objet=".intval($lier_trad)); |
||
| 137 | if ($row) { |
||
| 138 | include_spip('inc/filtres'); |
||
| 139 | $row[$champ_titre] = filtrer_entites(objet_T($type, 'info_nouvelle_traduction')) . ' ' . $row[$champ_titre]; |
||
| 140 | } else { |
||
| 141 | $row = array(); |
||
| 142 | } |
||
| 143 | |||
| 144 | // on met l'objet dans une rubrique si l'objet le peut |
||
| 145 | $desc = lister_tables_objets_sql($table); |
||
| 146 | $is_rubrique = isset($desc['field']['id_rubrique']); |
||
| 147 | |||
| 148 | if ($is_rubrique) { |
||
| 149 | $langues_dispo = explode(',', $GLOBALS['meta']['langues_multilingue']); |
||
| 150 | // si le redacteur utilise une autre langue que celle de la source, on suppose que c'est pour traduire dans sa langue |
||
| 151 | if (in_array($GLOBALS['spip_lang'], $langues_dispo) and $GLOBALS['spip_lang'] !== $row['lang']) { |
||
| 152 | $row['lang'] = $GLOBALS['spip_lang']; |
||
| 153 | } |
||
| 154 | // sinon si il y a seulement 2 langues dispos, on bascule sur l'"autre" |
||
| 155 | elseif (count($langues_dispo) == 2) { |
||
| 156 | $autre_langue = array_diff($langues_dispo, [$row['lang']]); |
||
| 157 | if (count($autre_langue) == 1) { |
||
| 158 | $row['lang'] = reset($autre_langue); |
||
| 159 | } |
||
| 160 | } |
||
| 161 | else { |
||
| 162 | $row['lang'] = 'en'; |
||
| 163 | } |
||
| 164 | |||
| 165 | if ($id_rubrique) { |
||
| 166 | $row['id_rubrique'] = $id_rubrique; |
||
| 167 | |||
| 168 | return $row; |
||
| 169 | } |
||
| 170 | $id_rubrique = $row['id_rubrique']; |
||
| 171 | |||
| 172 | |||
| 173 | // Regler la langue, si possible, sur celle du redacteur |
||
| 174 | // Cela implique souvent de choisir une rubrique ou un secteur |
||
| 175 | if (in_array($GLOBALS['spip_lang'], $langues_dispo)) { |
||
| 176 | |||
| 177 | // Si le menu de langues est autorise sur l'objet, |
||
| 178 | // on peut changer la langue quelle que soit la rubrique |
||
| 179 | // donc on reste dans la meme rubrique |
||
| 180 | if (in_array($table, explode(',', $GLOBALS['meta']['multi_objets']))) { |
||
| 181 | $row['id_rubrique'] = $row['id_rubrique']; # explicite :-) |
||
| 182 | |||
| 183 | // Sinon, chercher la rubrique la plus adaptee pour |
||
| 184 | // accueillir l'objet dans la langue du traducteur |
||
| 185 | } elseif ($is_rubrique and $GLOBALS['meta']['multi_rubriques'] == 'oui') { |
||
| 186 | if ($GLOBALS['meta']['multi_secteurs'] == 'oui') { |
||
| 187 | $id_parent = 0; |
||
| 188 | } else { |
||
| 189 | // on cherche une rubrique soeur dans la bonne langue |
||
| 190 | $row_rub = sql_fetsel("id_parent", "spip_rubriques", "id_rubrique=".intval($id_rubrique)); |
||
| 191 | $id_parent = $row_rub['id_parent']; |
||
| 192 | } |
||
| 193 | |||
| 194 | $row_rub = sql_fetsel("id_rubrique", "spip_rubriques", |
||
| 195 | "lang='" . $GLOBALS['spip_lang'] . "' AND id_parent=".intval($id_parent)); |
||
| 196 | if ($row_rub) { |
||
| 197 | $row['id_rubrique'] = $row_rub['id_rubrique']; |
||
| 198 | } |
||
| 199 | } |
||
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | return $row; |
||
| 204 | } |
||
| 205 |
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.