Complex classes like TabelleController 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 TabelleController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class TabelleController extends FiCoreController |
||
| 14 | { |
||
| 15 | |||
| 16 | public function aggiornaAction(Request $request) |
||
| 17 | { |
||
| 18 | if ($request->get('oper') == 'edit') { |
||
| 19 | $gestionepermessi = $this->get("ficorebundle.gestionepermessi"); |
||
| 20 | $operatore = $gestionepermessi->utentecorrente(); |
||
| 21 | |||
| 22 | $id = $request->get('id'); |
||
| 23 | |||
| 24 | $em = $this->getDoctrine()->getManager(); |
||
| 25 | $tabelle = $em->getRepository('FiCoreBundle:Tabelle')->find($id); |
||
| 26 | $tabelle->setOperatoriId($operatore['id']); |
||
| 27 | $nometabella = $this->getRequestValue($request, 'nometabella'); |
||
| 28 | if ($nometabella) { |
||
| 29 | $tabelle->setNometabella($nometabella); |
||
| 30 | } |
||
| 31 | $nomecampo = $this->getRequestValue($request, 'nomecampo'); |
||
| 32 | if ($nomecampo) { |
||
| 33 | $tabelle->setNomecampo($nomecampo); |
||
| 34 | } |
||
| 35 | $mostraindex = $this->getRequestValue($request, 'mostraindex'); |
||
| 36 | $tabelle->setMostraindex($mostraindex); |
||
| 37 | $ordineindex = $this->getRequestValue($request, 'ordineindex'); |
||
| 38 | $tabelle->setOrdineindex($ordineindex); |
||
| 39 | $etichettaindex = $this->getRequestValue($request, 'etichettaindex'); |
||
| 40 | $tabelle->setEtichettaindex($etichettaindex); |
||
| 41 | |||
| 42 | $larghezzaindex = $this->getRequestValue($request, 'larghezzaindex'); |
||
| 43 | $tabelle->setLarghezzaindex($larghezzaindex); |
||
| 44 | |||
| 45 | $mostrastampa = $this->getRequestValue($request, 'mostrastampa'); |
||
| 46 | $tabelle->setMostrastampa($mostrastampa); |
||
| 47 | |||
| 48 | $ordinestampa = $this->getRequestValue($request, 'ordinestampa'); |
||
| 49 | $tabelle->setOrdinestampa($ordinestampa); |
||
| 50 | |||
| 51 | $etichettastampa = $this->getRequestValue($request, 'etichettastampa'); |
||
| 52 | $tabelle->setEtichettastampa($etichettastampa); |
||
| 53 | |||
| 54 | $larghezzastampa = $this->getRequestValue($request, 'larghezzastampa'); |
||
| 55 | $tabelle->setLarghezzastampa($larghezzastampa); |
||
| 56 | $em->persist($tabelle); |
||
|
|
|||
| 57 | $em->flush(); |
||
| 58 | } |
||
| 59 | |||
| 60 | /* operatori_id int(11) |
||
| 61 | nometabella varchar(45) |
||
| 62 | nomecampo varchar(45) |
||
| 63 | mostraindex tinyint(1) |
||
| 64 | ordineindex int(11) |
||
| 65 | larghezzaindex int(11) |
||
| 66 | etichettaindex varchar(255) |
||
| 67 | mostrastampa tinyint(1) |
||
| 68 | ordinestampa int(11) |
||
| 69 | larghezzastampa int(11) |
||
| 70 | etichettastampa varchar(255) |
||
| 71 | * |
||
| 72 | */ |
||
| 73 | |||
| 74 | return new Response('OK'); |
||
| 75 | } |
||
| 76 | |||
| 77 | private function getRequestValue($request, $attribute) |
||
| 85 | |||
| 86 | 1 | public function configuraAction(Request $request, $nometabella) |
|
| 87 | { |
||
| 88 | 1 | $this->setup($request); |
|
| 89 | 1 | $gestionepermessi = $this->get("ficorebundle.gestionepermessi"); |
|
| 90 | 1 | $operatore = $gestionepermessi->utentecorrente(); |
|
| 91 | 1 | $this->generaDB(array('tabella' => $nometabella), $request); |
|
| 92 | 1 | $this->generaDB(array('tabella' => $nometabella, 'operatore' => $operatore['id']), $request); |
|
| 93 | |||
| 94 | 1 | $namespace = $this->getNamespace(); |
|
| 95 | 1 | $bundle = $this->getBundle(); |
|
| 96 | 1 | $controller = $this->getController(); |
|
| 97 | 1 | $container = $this->container; |
|
| 98 | |||
| 99 | 1 | $nomebundle = $namespace . $bundle . 'Bundle'; |
|
| 100 | |||
| 101 | 1 | $em = $this->getDoctrine()->getManager(); |
|
| 102 | 1 | $entities = $em->getRepository($nomebundle . ':' . $controller)->findAll(); |
|
| 103 | |||
| 104 | $dettaglij = array( |
||
| 105 | 1 | 'nomecampo' => array( |
|
| 106 | array('nomecampo' => 'nomecampo', 'lunghezza' => '150', 'descrizione' => 'Campo', 'tipo' => 'text', 'editable' => false),), |
||
| 107 | 'mostraindex' => array( |
||
| 108 | array('nomecampo' => 'mostraindex', 'lunghezza' => '100', 'descrizione' => 'Vedi in griglia', 'tipo' => 'boolean'), |
||
| 109 | ), |
||
| 110 | 'ordineindex' => array( |
||
| 111 | array('nomecampo' => 'ordineindex', 'lunghezza' => '100', 'descrizione' => 'Ordine in griglia', 'tipo' => 'text'), |
||
| 112 | ), |
||
| 113 | 'etichettaindex' => array( |
||
| 114 | array('nomecampo' => 'etichettaindex', 'lunghezza' => '150', 'descrizione' => 'Label in griglia', 'tipo' => 'text'), |
||
| 115 | ), |
||
| 116 | 'larghezzaindex' => array( |
||
| 117 | array('nomecampo' => 'larghezzaindex', 'lunghezza' => '100', 'descrizione' => 'Largh. in griglia', 'tipo' => 'text'), |
||
| 118 | ), |
||
| 119 | 'mostrastampa' => array( |
||
| 120 | array('nomecampo' => 'mostrastampa', 'lunghezza' => '100', 'descrizione' => 'Vedi in stampa', 'tipo' => 'boolean'), |
||
| 121 | ), |
||
| 122 | 'ordinestampa' => array( |
||
| 123 | array('nomecampo' => 'ordinestampa', 'lunghezza' => '100', 'descrizione' => 'Ordine in stampa', 'tipo' => 'text'), |
||
| 124 | ), |
||
| 125 | 'etichettastampa' => array( |
||
| 126 | array('nomecampo' => 'etichettastampa', 'lunghezza' => '150', 'descrizione' => 'Label in stampa', 'tipo' => 'text'), |
||
| 127 | ), |
||
| 128 | 'larghezzastampa' => array( |
||
| 129 | array('nomecampo' => 'larghezzastampa', 'lunghezza' => '100', 'descrizione' => 'Largh. in stampa', 'tipo' => 'text'), |
||
| 130 | ), |
||
| 131 | ); |
||
| 132 | |||
| 133 | $paricevuti = array( |
||
| 134 | 1 | 'doctrine' => $em, |
|
| 135 | 1 | 'nomebundle' => $nomebundle, |
|
| 136 | 1 | 'nometabella' => $controller, |
|
| 137 | 1 | 'dettaglij' => $dettaglij, |
|
| 138 | 1 | 'container' => $container, |
|
| 139 | ); |
||
| 140 | |||
| 141 | 1 | $paricevuti['escludere'] = array('nometabella', 'operatori_id'); |
|
| 142 | |||
| 143 | 1 | $testata = Griglia::testataPerGriglia($paricevuti); |
|
| 144 | |||
| 145 | 1 | $testata['titolo'] = "Configurazione colonne per tabella $nometabella"; |
|
| 146 | 1 | $testata['multisearch'] = 0; |
|
| 147 | 1 | $testata['showdel'] = 0; |
|
| 148 | 1 | $testata['showadd'] = 0; |
|
| 149 | 1 | $testata['showedit'] = 0; |
|
| 150 | 1 | $testata['showprint'] = 0; |
|
| 151 | 1 | $testata['editinline'] = 1; |
|
| 152 | 1 | $testata['nomelist'] = '#listconfigura'; |
|
| 153 | 1 | $testata['nomepager'] = '#pagerconfigura'; |
|
| 154 | 1 | $testata['tastochiudi'] = 1; |
|
| 155 | 1 | $testata['div'] = '#dettaglioconf'; |
|
| 156 | 1 | $testata['chiamante'] = $nometabella; |
|
| 157 | 1 | $testata['percorsogriglia'] = $nometabella . '/grigliapopup'; |
|
| 158 | 1 | $testata['altezzagriglia'] = '300'; |
|
| 159 | 1 | $testata['larghezzagriglia'] = '900'; |
|
| 160 | |||
| 161 | 1 | $testata['permessiedit'] = 1; |
|
| 162 | 1 | $testata['permessidelete'] = 1; |
|
| 163 | 1 | $testata['permessicreate'] = 1; |
|
| 164 | 1 | $testata['permessiread'] = 1; |
|
| 165 | $twigparm = array( |
||
| 166 | 1 | 'entities' => $entities, |
|
| 167 | 1 | 'nomecontroller' => $controller, |
|
| 168 | 1 | 'testata' => json_encode($testata), |
|
| 169 | 1 | 'chiamante' => $nometabella, |
|
| 170 | ); |
||
| 171 | |||
| 172 | 1 | return $this->render($nomebundle . ':' . $controller . ':configura.html.twig', $twigparm); |
|
| 173 | } |
||
| 174 | |||
| 175 | 1 | public function generaDB($parametri, Request $request) |
|
| 176 | { |
||
| 177 | 1 | $this->setup($request); |
|
| 178 | 1 | if (!isset($parametri['tabella'])) { |
|
| 179 | return false; |
||
| 180 | } |
||
| 181 | |||
| 182 | 1 | $namespace = $this->getNamespace(); |
|
| 183 | 1 | $bundle = $this->getBundle(); |
|
| 184 | |||
| 185 | 1 | $nomebundle = $namespace . $bundle . 'Bundle'; |
|
| 186 | |||
| 187 | 1 | $nometabella = $parametri['tabella']; |
|
| 188 | 1 | $em = $this->getDoctrine()->getManager(); |
|
| 189 | |||
| 190 | 1 | $bundles = $this->get('kernel')->getBundles(); |
|
| 191 | 1 | foreach ($bundles as $bundle) { |
|
| 192 | 1 | $className = get_class($bundle); |
|
| 193 | 1 | $entityClass = substr($className, 0, strrpos($className, '\\')); |
|
| 194 | 1 | $tableClassName = '\\' . $entityClass . '\\Entity\\' . $nometabella; |
|
| 195 | 1 | if (!class_exists($tableClassName)) { |
|
| 196 | 1 | $tableClassName = ''; |
|
| 197 | 1 | continue; |
|
| 198 | } else { |
||
| 199 | 1 | break; |
|
| 200 | } |
||
| 201 | } |
||
| 202 | |||
| 203 | 1 | if (!$tableClassName) { |
|
| 204 | throw new \Exception('Entity per la tabella ' . $nometabella . ' non trovata', '-1'); |
||
| 205 | } |
||
| 206 | |||
| 207 | 1 | $bundleClass = str_replace('\\', '', $entityClass); |
|
| 208 | |||
| 209 | 1 | $c = $em->getClassMetadata($bundleClass . ':' . $nometabella); |
|
| 210 | |||
| 211 | 1 | $colonne = $c->getColumnNames(); |
|
| 212 | 1 | $this->scriviDB($colonne, $nometabella, $nomebundle, $parametri); |
|
| 213 | 1 | } |
|
| 214 | |||
| 215 | 1 | private function scriviDB($colonne, $nometabella, $nomebundle, $parametri) |
|
| 216 | { |
||
| 217 | 1 | foreach ($colonne as $colonna) { |
|
| 218 | $vettorericerca = array( |
||
| 219 | 1 | 'nometabella' => $nometabella, |
|
| 220 | 1 | 'nomecampo' => $colonna, |
|
| 221 | ); |
||
| 222 | |||
| 223 | 1 | if (isset($parametri['operatore'])) { |
|
| 224 | 1 | $vettorericerca['operatori_id'] = $parametri['operatore']; |
|
| 225 | } |
||
| 226 | |||
| 227 | 1 | $trovato = $this->getDoctrine()->getRepository($nomebundle . ':tabelle')->findBy($vettorericerca, array()); |
|
| 228 | |||
| 229 | 1 | if (!$trovato) { |
|
| 230 | 1 | $crea = new tabelle(); |
|
| 231 | 1 | $crea->setNometabella($nometabella); |
|
| 232 | 1 | $crea->setNomecampo($colonna); |
|
| 233 | |||
| 234 | 1 | if (isset($parametri['operatore'])) { |
|
| 235 | 1 | $arraycreaoperatore = array('id' => $parametri['operatore']); |
|
| 236 | 1 | $creaoperatore = $this->getDoctrine()->getRepository($nomebundle . ':operatori')->findOneBy($arraycreaoperatore, array()); |
|
| 237 | 1 | $crea->setOperatori($creaoperatore); |
|
| 238 | |||
| 239 | 1 | unset($vettorericerca['operatori_id']); |
|
| 240 | 1 | $vettorericerca['operatori_id'] = null; |
|
| 241 | 1 | $ritrovato = $this->getDoctrine()->getRepository($nomebundle . ':tabelle')->findOneBy($vettorericerca, array()); |
|
| 242 | |||
| 243 | 1 | if ($ritrovato) { |
|
| 244 | 1 | $crea->setMostrastampa($ritrovato->hasMostrastampa() ? true : false); |
|
| 245 | 1 | $crea->setMostraindex($ritrovato->hasMostraindex() ? true : false); |
|
| 246 | } |
||
| 247 | } else { |
||
| 248 | 1 | $crea->setMostrastampa(true); |
|
| 249 | 1 | $crea->setMostraindex(true); |
|
| 250 | } |
||
| 251 | |||
| 252 | 1 | $ma = $this->getDoctrine()->getManager(); |
|
| 253 | 1 | $ma->persist($crea); |
|
| 254 | 1 | $ma->flush(); |
|
| 255 | } |
||
| 256 | } |
||
| 257 | 1 | } |
|
| 258 | |||
| 259 | public function grigliapopupAction(Request $request, $chiamante) |
||
| 287 | |||
| 288 | public function grigliaAction(Request $request) |
||
| 295 | |||
| 296 | 1 | protected function setParametriGriglia($prepar = array()) |
|
| 297 | { |
||
| 298 | 1 | $this->setup($prepar['request']); |
|
| 299 | 1 | $namespace = $this->getNamespace(); |
|
| 328 | |||
| 329 | public function listacampitabellaAction(Request $request) |
||
| 376 | |||
| 377 | private function listacampitabelladettagli($escludiid, $colonne, $nomebundle, $controller) |
||
| 432 | } |
||
| 433 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.