| Conditions | 20 |
| Paths | 1785 |
| Total Lines | 77 |
| Code Lines | 56 |
| 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 action_spip_image_ajouter_dist($arg,$sousaction2,$source,$return=false) { |
||
| 55 | global $formats_logos; |
||
| 56 | |||
| 57 | include_spip('inc/documents'); |
||
| 58 | if (!$sousaction2) { |
||
| 59 | if (!$_FILES) $_FILES = $GLOBALS['HTTP_POST_FILES']; |
||
| 60 | $source = (is_array($_FILES) ? array_pop($_FILES) : ""); |
||
| 61 | } |
||
| 62 | $erreur = ""; |
||
| 63 | if (!$source) |
||
| 64 | spip_log("spip_image_ajouter : source inconnue"); |
||
| 65 | else { |
||
| 66 | $f =_DIR_LOGOS . $arg . '.tmp'; |
||
| 67 | |||
| 68 | if (!is_array($source)) |
||
| 69 | // fichier dans upload/ |
||
| 70 | $source = @copy(determine_upload() . $source, $f); |
||
| 71 | else { |
||
| 72 | // Intercepter une erreur a l'envoi |
||
| 73 | if ($erreur = check_upload_error($source['error'],"",$return)) |
||
| 74 | $source =""; |
||
| 75 | else |
||
| 76 | // analyse le type de l'image (on ne fait pas confiance au nom de |
||
| 77 | // fichier envoye par le browser : pour les Macs c'est plus sur) |
||
| 78 | |||
| 79 | $source = deplacer_fichier_upload($source['tmp_name'], $f); |
||
| 80 | } |
||
| 81 | if (!$source) |
||
| 82 | spip_log("pb de copie pour $f"); |
||
| 83 | } |
||
| 84 | if ($source AND $f) { |
||
| 85 | $size = @getimagesize($f); |
||
|
|
|||
| 86 | $type = !$size ? '': ($size[2] > 3 ? '' : $formats_logos[$size[2]-1]); |
||
| 87 | if ($type) { |
||
| 88 | $poids = filesize($f); |
||
| 89 | |||
| 90 | if (_LOGO_MAX_SIZE > 0 |
||
| 91 | AND $poids > _LOGO_MAX_SIZE*1024) { |
||
| 92 | spip_unlink ($f); |
||
| 93 | $erreur = _T('info_logo_max_poids', |
||
| 94 | array('maxi' => taille_en_octets(_LOGO_MAX_SIZE*1024), |
||
| 95 | 'actuel' => taille_en_octets($poids))); |
||
| 96 | } |
||
| 97 | |||
| 98 | elseif (_LOGO_MAX_WIDTH * _LOGO_MAX_HEIGHT |
||
| 99 | AND ($size[0] > _LOGO_MAX_WIDTH |
||
| 100 | OR $size[1] > _LOGO_MAX_HEIGHT)) { |
||
| 101 | spip_unlink ($f); |
||
| 102 | $erreur = _T('info_logo_max_poids', |
||
| 103 | array( |
||
| 104 | 'maxi' => |
||
| 105 | _T('info_largeur_vignette', |
||
| 106 | array('largeur_vignette' => _LOGO_MAX_WIDTH, |
||
| 107 | 'hauteur_vignette' => _LOGO_MAX_HEIGHT)), |
||
| 108 | 'actuel' => |
||
| 109 | _T('info_largeur_vignette', |
||
| 110 | array('largeur_vignette' => $size[0], |
||
| 111 | 'hauteur_vignette' => $size[1])) |
||
| 112 | )); |
||
| 113 | } |
||
| 114 | else |
||
| 115 | @rename ($f, _DIR_LOGOS . $arg . ".$type"); |
||
| 116 | } |
||
| 117 | else { |
||
| 118 | spip_unlink ($f); |
||
| 119 | $erreur = _T('info_logo_format_interdit', |
||
| 120 | array('formats' => join(', ', $formats_logos))); |
||
| 121 | } |
||
| 122 | |||
| 123 | } |
||
| 124 | if ($erreur){ |
||
| 125 | if ($return) |
||
| 126 | return $erreur; |
||
| 127 | else |
||
| 128 | check_upload_error(6,$erreur); |
||
| 129 | } |
||
| 130 | } |
||
| 131 | ?> |
||
| 132 |
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: