| Conditions | 20 |
| Paths | 4608 |
| Total Lines | 100 |
| 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 |
||
| 102 | function inc_exporter_csv_dist($titre, $resource, $options = []) { |
||
| 103 | |||
| 104 | // support ancienne syntaxe |
||
| 105 | // inc_exporter_csv_dist($titre, $resource, $delim = ', ', $entetes = null, $envoyer = true) |
||
| 106 | if (is_string($options)) { |
||
| 107 | $args = func_get_args(); |
||
| 108 | $options = []; |
||
| 109 | foreach ([2 => 'delim', 3 => 'entetes', 4 => 'envoyer'] as $k => $option) { |
||
| 110 | if (!empty($args[$k])) { |
||
| 111 | $options[$option] = $args[$k]; |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | $default_options = [ |
||
| 117 | 'delim' => ', ', |
||
| 118 | 'entetes' => null, |
||
| 119 | 'envoyer' => true, |
||
| 120 | 'charset' => null, |
||
| 121 | 'callback' => null, |
||
| 122 | ]; |
||
| 123 | $options = array_merge($default_options, $options); |
||
| 124 | |||
| 125 | $filename = preg_replace(',[^-_\w]+,', '_', translitteration(textebrut(typo($titre)))); |
||
| 126 | |||
| 127 | if ($options['delim'] == 'TAB') { |
||
| 128 | $options['delim'] = "\t"; |
||
| 129 | } |
||
| 130 | if (!in_array($options['delim'], array(',', ';', "\t"))) { |
||
| 131 | $options['delim'] = ','; |
||
| 132 | } |
||
| 133 | |||
| 134 | $charset = $GLOBALS['meta']['charset']; |
||
| 135 | $importer_charset = null; |
||
| 136 | if ($options['delim'] == ',') { |
||
| 137 | $extension = 'csv'; |
||
| 138 | } else { |
||
| 139 | $extension = 'xls'; |
||
| 140 | # Excel n'accepte pas l'utf-8 ni les entites html... on transcode tout ce qu'on peut |
||
| 141 | $charset = 'iso-8859-1'; |
||
| 142 | } |
||
| 143 | // mais si une option charset est explicite, elle a la priorite |
||
| 144 | if (!empty($options['charset'])) { |
||
| 145 | $charset = $options['charset']; |
||
| 146 | } |
||
| 147 | |||
| 148 | $importer_charset = (($charset === $GLOBALS['meta']['charset']) ? null : $charset); |
||
| 149 | |||
| 150 | $filename = "$filename.$extension"; |
||
| 151 | |||
| 152 | $output = ''; |
||
| 153 | $nb = 0; |
||
| 154 | if (!empty($options['entetes']) and is_array($options['entetes'])) { |
||
| 155 | $output = exporter_csv_ligne_numerotee($nb, $options['entetes'], $options['delim'], $importer_charset, $options['callback']); |
||
| 156 | } |
||
| 157 | // les donnees commencent toujours a la ligne 1, qu'il y ait ou non des entetes |
||
| 158 | $nb++; |
||
| 159 | |||
| 160 | if ($options['envoyer']) { |
||
| 161 | $disposition = ($options['envoyer'] === 'attachment' ? 'attachment' : 'inline'); |
||
| 162 | header("Content-Type: text/comma-separated-values; charset=$charset"); |
||
| 163 | header("Content-Disposition: $disposition; filename=$filename"); |
||
| 164 | |||
| 165 | // Vider tous les tampons |
||
| 166 | $level = @ob_get_level(); |
||
| 167 | while ($level--) { |
||
| 168 | @ob_end_flush(); |
||
| 169 | } |
||
| 170 | } |
||
| 171 | |||
| 172 | // si envoyer=='attachment' on passe par un fichier temporaire |
||
| 173 | // sinon on ecrit directement sur stdout |
||
| 174 | if ($options['envoyer'] and $options['envoyer'] !== 'attachment') { |
||
| 175 | $fichier = "php://output"; |
||
| 176 | } |
||
| 177 | else { |
||
| 178 | $fichier = sous_repertoire(_DIR_CACHE, 'export') . $filename; |
||
| 179 | } |
||
| 180 | |||
| 181 | $fp = fopen($fichier, 'w'); |
||
| 182 | $length = fwrite($fp, $output); |
||
| 183 | |||
| 184 | while ($row = is_array($resource) ? array_shift($resource) : sql_fetch($resource)) { |
||
| 185 | $output = exporter_csv_ligne_numerotee($nb, $row, $options['delim'], $importer_charset, $options['callback']); |
||
| 186 | $length += fwrite($fp, $output); |
||
| 187 | $nb++; |
||
| 188 | } |
||
| 189 | fclose($fp); |
||
| 190 | |||
| 191 | if ($options['envoyer']) { |
||
| 192 | if ($options['envoyer'] === 'attachment') { |
||
| 193 | header("Content-Length: $length"); |
||
| 194 | readfile($fichier); |
||
| 195 | } |
||
| 196 | // si on a envoye inline, c'est deja tout bon |
||
| 197 | exit; |
||
| 198 | } |
||
| 199 | |||
| 200 | return $fichier; |
||
| 201 | } |
||
| 202 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive. In addition it looks for parameters that have the generic type
arrayand suggests a stricter type likearray<String>.Most often this is a case of a parameter that can be null in addition to its declared types.