Complex classes like FiUtilita often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use FiUtilita, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class FiUtilita |
||
| 15 | { |
||
| 16 | |||
| 17 | public function percentualiConfrontoStringheVettore($parametri = array()) |
||
| 23 | |||
| 24 | 1 | public function percentualiConfrontoStringhe($parametri = array()) |
|
| 30 | |||
| 31 | public function sommaMinuti($parametri = array()) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @param array $parametri |
||
| 54 | * @param string $parametri["tipo"] |
||
| 55 | * |
||
| 56 | * @return Array("segnouno"=>"xx", "segnodue"=>"yy") dove segnodue non obbligatorio |
||
| 57 | */ |
||
| 58 | public function operatoreQuery($parametri = array()) |
||
| 59 | { |
||
| 60 | $risposta = array(); |
||
| 61 | |||
| 62 | if (isset($parametri['tipo'])) { |
||
| 63 | $tipocampo = $parametri['tipo']; |
||
| 64 | } else { |
||
| 65 | return array('segnouno' => '='); |
||
| 66 | } |
||
| 67 | |||
| 68 | switch ($tipocampo) { |
||
| 69 | case 'date': |
||
| 70 | case 'integer': |
||
| 71 | case 'double': |
||
| 72 | $operatore = '>='; |
||
| 73 | $operatoredue = '<='; |
||
| 74 | break; |
||
| 75 | case 'string': |
||
| 76 | case 'text': |
||
| 77 | $operatore = 'LIKE'; |
||
| 78 | break; |
||
| 79 | default: |
||
| 80 | $operatore = '='; |
||
| 81 | break; |
||
| 82 | } |
||
| 83 | |||
| 84 | $risposta['segnouno'] = $operatore; |
||
| 85 | if (isset($operatoredue)) { |
||
| 86 | $risposta['segnodue'] = $operatoredue; |
||
| 87 | } |
||
| 88 | |||
| 89 | return $risposta; |
||
| 90 | } |
||
| 91 | |||
| 92 | 2 | public static function data2db($giorno, $invertito = false, $senzalinea = false) |
|
| 118 | |||
| 119 | 1 | private static function getDataFormattata($aaaa, $mm, $gg, $senzalinea) |
|
| 127 | |||
| 128 | public static function db2data($giorno, $senzalinea = false) |
||
| 129 | { |
||
| 130 | if (substr($giorno, 2, 1) == '/') { |
||
| 131 | return $giorno; |
||
| 132 | } |
||
| 133 | |||
| 134 | if ($senzalinea) { |
||
| 135 | $formattata = self::senzalinea($giorno); |
||
| 136 | } else { |
||
| 137 | $barra = strpos($giorno, '-'); |
||
| 138 | $aaaa = substr($giorno, 0, $barra); |
||
| 139 | $restante = substr($giorno, $barra + 1); |
||
| 140 | $barra = strpos($restante, '-'); |
||
| 141 | $mm = substr($restante, 0, $barra); |
||
| 142 | $gg = substr($restante, $barra + 1); |
||
| 143 | |||
| 144 | $formattata = (strlen($gg) == 0 ? '' : "$gg/$mm/$aaaa"); |
||
| 145 | } |
||
| 146 | |||
| 147 | return $formattata; |
||
| 148 | } |
||
| 149 | |||
| 150 | private static function senzalinea($giorno) |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param array $parametri |
||
| 163 | * @param string $parametri["nomecodice"] default = "codice" |
||
| 164 | * @param string $parametri["nomedescrizione"] default = "descrizione" |
||
| 165 | * @param array $parametri["elementi"] Array([0]=>("codice"=>1, "descrizione"=>"blaa"), [1]=>...) |
||
| 166 | * @param string $parametri["selezionato"] opzionale |
||
| 167 | * |
||
| 168 | * @return string |
||
| 169 | */ |
||
| 170 | 1 | public function proSelect($parametri = array()) |
|
| 171 | { |
||
| 172 | 1 | $stringaproselect = ''; |
|
| 173 | 1 | if (!isset($parametri['elementi'])) { |
|
| 174 | return false; |
||
| 175 | } |
||
| 176 | |||
| 177 | //parametri obbligatori |
||
| 178 | 1 | $elementi = $parametri['elementi']; |
|
| 179 | 1 | $attributi = $this->getProSelectAttribute($parametri); |
|
| 180 | 1 | $selezionato = $attributi['selezionato']; |
|
| 181 | 1 | $nomecodice = $attributi['nomecodice']; |
|
| 182 | 1 | $nomedescrizione = $attributi['nomedescrizione']; |
|
| 183 | |||
| 184 | 1 | foreach ($elementi as $elemento) { |
|
| 185 | 1 | $elementonomecodice = $elemento[$nomecodice]; |
|
| 186 | 1 | $elementonomedescrizione = $elemento[$nomedescrizione]; |
|
| 187 | 1 | $elementoselezionato = ($elementonomecodice === $selezionato ? " selected='yes'" : ''); |
|
| 188 | 1 | $stringaproselect .= '<option value="' . $elementonomecodice . '"' . $elementoselezionato . '>' . $elementonomedescrizione . '</option>'; |
|
| 189 | } |
||
| 190 | |||
| 191 | 1 | return $stringaproselect; |
|
| 192 | } |
||
| 193 | |||
| 194 | 1 | public function getProSelectAttribute($parametri) |
|
| 203 | |||
| 204 | /** |
||
| 205 | * @param $parametri["vettore"] |
||
| 206 | * @param $parametri["chiave"] |
||
| 207 | * @param $parametri["valore"] |
||
| 208 | * |
||
| 209 | * @return $vettorenuovo |
||
| 210 | */ |
||
| 211 | public function cancellaDaVettore($parametri = array()) |
||
| 244 | } |
||
| 245 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.