| Conditions | 31 |
| Paths | 4111 |
| Total Lines | 140 |
| Code Lines | 85 |
| Lines | 42 |
| Ratio | 30 % |
| 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 |
||
| 238 | function urls_propres_dist($i, $entite, $args='', $ancre='') { |
||
| 239 | |||
| 240 | if (is_numeric($i)) |
||
| 241 | return _generer_url_propre($entite, $i, $args, $ancre); |
||
| 242 | |||
| 243 | $url = $i; |
||
| 244 | $id_objet = $type = 0; |
||
| 245 | $url_redirect = null; |
||
| 246 | // recuperer les &debut_xx; |
||
| 247 | if (is_array($args)) |
||
| 248 | $contexte = $args; |
||
| 249 | else |
||
| 250 | parse_str($args,$contexte); |
||
| 251 | |||
| 252 | |||
| 253 | // Migration depuis anciennes URLs ? |
||
| 254 | // traiter les injections domain.tld/spip.php/n/importe/quoi/rubrique23 |
||
| 255 | View Code Duplication | if ($GLOBALS['profondeur_url']<=0 |
|
| 256 | AND $_SERVER['REQUEST_METHOD'] != 'POST') { |
||
| 257 | include_spip('inc/urls'); |
||
| 258 | $r = nettoyer_url_page($i, $contexte); |
||
| 259 | if ($r) { |
||
| 260 | list($contexte, $type,,, $suite) = $r; |
||
| 261 | $_id = id_table_objet($type); |
||
| 262 | $id_objet = $contexte[$_id]; |
||
| 263 | $url_propre = generer_url_entite($id_objet, $type); |
||
| 264 | if (strlen($url_propre) |
||
| 265 | AND !strstr($url,$url_propre)) { |
||
| 266 | list(,$hash) = explode('#', $url_propre); |
||
| 267 | $args = array(); |
||
| 268 | foreach(array_filter(explode('&', $suite)) as $fragment) { |
||
| 269 | if ($fragment != "$_id=$id_objet") |
||
| 270 | $args[] = $fragment; |
||
| 271 | } |
||
| 272 | $url_redirect = generer_url_entite($id_objet, $type, join('&',array_filter($args)), $hash); |
||
| 273 | |||
| 274 | return array($contexte, $type, $url_redirect, $type); |
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | /* Fin compatibilite anciennes urls */ |
||
| 279 | // Chercher les valeurs d'environnement qui indiquent l'url-propre |
||
| 280 | View Code Duplication | if (isset($_SERVER['REDIRECT_url_propre'])) |
|
| 281 | $url_propre = $_SERVER['REDIRECT_url_propre']; |
||
| 282 | elseif (isset($_ENV['url_propre'])) |
||
| 283 | $url_propre = $_ENV['url_propre']; |
||
| 284 | else { |
||
| 285 | // ne prendre que le segment d'url qui correspond, en fonction de la profondeur calculee |
||
| 286 | $url = ltrim($url,'/'); |
||
| 287 | $url = explode('/',$url); |
||
| 288 | while (count($url)>$GLOBALS['profondeur_url']+1) |
||
| 289 | array_shift($url); |
||
| 290 | $url = implode('/',$url); |
||
| 291 | $url_propre = preg_replace(',[?].*,', '', $url); |
||
| 292 | } |
||
| 293 | |||
| 294 | // Mode Query-String ? |
||
| 295 | $is_qs = false; |
||
| 296 | if (!$url_propre |
||
| 297 | AND preg_match(',[?]([^=/?&]+)(&.*)?$,', $url, $r)) { |
||
| 298 | $url_propre = $r[1]; |
||
| 299 | $is_qs = true; |
||
| 300 | } |
||
| 301 | |||
| 302 | if (!$url_propre) return; // qu'est-ce qu'il veut ??? |
||
| 303 | |||
| 304 | // gerer le cas de retour depuis des urls arbos |
||
| 305 | // mais si url arbo ne trouve pas, on veut une 404 par securite |
||
| 306 | if ($GLOBALS['profondeur_url']>0){ |
||
| 307 | $urls_anciennes = charger_fonction('arbo','urls'); |
||
| 308 | return $urls_anciennes($url_propre, $entite, $contexte); |
||
| 309 | } |
||
| 310 | |||
| 311 | include_spip('base/abstract_sql'); // chercher dans la table des URLS |
||
| 312 | |||
| 313 | // Compatibilite avec propres2 |
||
| 314 | $url_propre = preg_replace(',\.html$,i', '', $url_propre); |
||
| 315 | |||
| 316 | // Revenir en utf-8 si encodage type %D8%A7 (farsi) |
||
| 317 | $url_propre = rawurldecode($url_propre); |
||
| 318 | |||
| 319 | // Compatibilite avec les anciens marqueurs d'URL propres |
||
| 320 | // Tester l'entree telle quelle (avec 'url_libre' des sites ont pu avoir des entrees avec marqueurs dans la table spip_urls) |
||
| 321 | if (!$row = sql_fetsel('id_objet, type, date', 'spip_urls', 'url='.sql_quote($url_propre))) { |
||
| 322 | // Sinon enlever les marqueurs eventuels |
||
| 323 | $url_propre2 = retirer_marqueurs_url_propre($url_propre); |
||
| 324 | |||
| 325 | $row = sql_fetsel('id_objet, type, date', 'spip_urls', 'url='.sql_quote($url_propre2)); |
||
| 326 | } |
||
| 327 | |||
| 328 | if ($row) { |
||
| 329 | $type = $row['type']; |
||
| 330 | $col_id = id_table_objet($type); |
||
| 331 | $contexte[$col_id] = $row['id_objet']; |
||
| 332 | $entite = $row['type']; |
||
| 333 | |||
| 334 | // Si l'url est vieux, donner le nouveau |
||
| 335 | if ($recent = sql_fetsel('url, date', 'spip_urls', |
||
| 336 | 'type='.sql_quote($row['type']).' AND id_objet='.sql_quote($row['id_objet']) |
||
| 337 | .' AND date>'.sql_quote($row['date']), '', 'date DESC', 1)) { |
||
| 338 | // Mode compatibilite pour conserver la distinction -Rubrique- |
||
| 339 | View Code Duplication | if (_MARQUEUR_URL) { |
|
| 340 | $marqueur = unserialize(_MARQUEUR_URL); |
||
| 341 | $marqueur1 = $marqueur[$type.'1']; // debut '+-' |
||
| 342 | $marqueur2 = $marqueur[$type.'2']; // fin '-+' |
||
| 343 | } else |
||
| 344 | $marqueur1 = $marqueur2 = ''; |
||
| 345 | $url_redirect = $marqueur1 . $recent['url'] . $marqueur2; |
||
| 346 | } |
||
| 347 | } |
||
| 348 | |||
| 349 | if ($entite=='' OR $entite=='type_urls' /* compat .htaccess 2.0 */) { |
||
| 350 | if ($type) { |
||
| 351 | $entite = ($type == 'syndic') ? 'site' : $type; |
||
| 352 | } else { |
||
| 353 | // Si ca ressemble a une URL d'objet, ce n'est pas la home |
||
| 354 | // et on provoque un 404 |
||
| 355 | if (preg_match(',^.*/[^\.]+(\.html)?$,', $url)) { |
||
| 356 | $entite = '404'; |
||
| 357 | $contexte['erreur'] = ''; |
||
| 358 | |||
| 359 | // l'url n'existe pas... |
||
| 360 | // on ne sait plus dire de quel type d'objet il s'agit |
||
| 361 | // sauf si on a le marqueur. et la c'est un peu sale... |
||
| 362 | if (_MARQUEUR_URL) { |
||
| 363 | $fmarqueur = @array_flip(unserialize(_MARQUEUR_URL)); |
||
| 364 | preg_match(',^([+][-]|[-+@_]),', $url_propre, $regs); |
||
| 365 | $objet = $regs ? substr($fmarqueur[$regs[1]],0,n-1) : 'article'; |
||
| 366 | $contexte['erreur'] = _T( |
||
| 367 | ($objet=='rubrique' OR $objet=='breve') |
||
| 368 | ? 'public:aucune_'.$objet |
||
| 369 | : 'public:aucun_'.$objet |
||
| 370 | ); |
||
| 371 | } |
||
| 372 | } |
||
| 373 | } |
||
| 374 | } |
||
| 375 | |||
| 376 | return array($contexte, $entite, $url_redirect, $is_qs?$entite:null); |
||
| 377 | } |
||
| 378 | |||
| 380 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.