Complex classes like StampatabellaController 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 StampatabellaController, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
8 | class StampatabellaController extends FiCoreController |
||
9 | { |
||
10 | |||
11 | public function __construct($container = null) |
||
17 | |||
18 | 1 | public function stampa($parametri = array()) |
|
19 | { |
||
20 | 1 | $testata = $parametri['testata']; |
|
21 | 1 | $rispostaj = $parametri['griglia']; |
|
22 | 1 | $request = $parametri['request']; |
|
23 | 1 | $nomicolonne = $testata['nomicolonne']; |
|
24 | |||
25 | 1 | $modellicolonne = $testata['modellocolonne']; |
|
26 | 1 | $larghezzaform = 900; |
|
27 | |||
28 | 1 | $pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false); |
|
29 | |||
30 | //echo PDF_HEADER_LOGO; |
||
31 | 1 | $pdftitle = isset($testata['titolo']) && ($testata['titolo'] != '') ? $testata['titolo'] : 'Elenco ' . $request->get('nometabella'); |
|
32 | 1 | $pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, 'FiFree2', $pdftitle, array(0, 0, 0), array(0, 0, 0)); |
|
33 | 1 | $pdf->setFooterData(array(0, 0, 0), array(0, 0, 0)); |
|
34 | |||
35 | 1 | $pdf->setHeaderFont(array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN)); |
|
36 | 1 | $pdf->setFooterFont(array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA)); |
|
37 | |||
38 | 1 | $pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT); |
|
39 | 1 | $pdf->SetHeaderMargin(PDF_MARGIN_HEADER); |
|
40 | 1 | $pdf->SetFooterMargin(PDF_MARGIN_FOOTER); |
|
41 | 1 | $pdf->SetFillColor(220, 220, 220); |
|
42 | |||
43 | 1 | $pdf->AddPage('L'); |
|
44 | 1 | $h = 6; |
|
45 | 1 | $border = 1; |
|
46 | 1 | $ln = 0; |
|
47 | 1 | $align = 'L'; |
|
48 | 1 | $fill = 0; |
|
49 | |||
50 | 1 | $dimensions = $pdf->getPageDimensions(); |
|
51 | |||
52 | 1 | $this->stampaTestata($pdf, $nomicolonne, $modellicolonne, $larghezzaform, $h, $border, $align, $fill, $ln); |
|
53 | |||
54 | // Dati |
||
55 | 1 | $risposta = json_decode($rispostaj); |
|
56 | 1 | $righe = $risposta->rows; |
|
57 | 1 | $pdf->SetFont('helvetica', '', 9); |
|
58 | 1 | foreach ($righe as $riga) { |
|
59 | 1 | $fill = !$fill; |
|
60 | 1 | $vettorecelle = $riga->cell; |
|
61 | |||
62 | 1 | $arr_heights = array(); |
|
63 | // store current object |
||
64 | 1 | $pdf->startTransaction(); |
|
65 | 1 | foreach ($vettorecelle as $posizione => $valore) { |
|
66 | 1 | if (!is_object($valore)) { |
|
67 | 1 | if (isset($modellicolonne[$posizione])) { |
|
68 | 1 | $width = ((297 * $modellicolonne[$posizione]['width']) / $larghezzaform) / 2; |
|
69 | } else { |
||
70 | $width = ((297 * 100) / $larghezzaform) / 2; |
||
71 | } |
||
72 | // get the number of lines |
||
73 | 1 | $arr_heights[] = $pdf->MultiCell($width, 0, $valore, $border, $align, $fill, 0, '', '', true, 0, false, true, 0); |
|
74 | //$arr_heights[] = $pdf->getNumLines($valore, $width); |
||
|
|||
75 | } |
||
76 | } |
||
77 | // restore previous object |
||
78 | 1 | $pdf->rollbackTransaction(true); |
|
79 | //work out the number of lines required |
||
80 | 1 | $rowcount = max($arr_heights); |
|
81 | 1 | $startY = $pdf->GetY(); |
|
82 | 1 | if (($startY + $rowcount * $h) + $dimensions['bm'] > ($dimensions['hk'])) { |
|
83 | // page break |
||
84 | $pdf->AddPage('L'); |
||
85 | // stampa testata |
||
86 | $this->stampaTestata($pdf, $nomicolonne, $modellicolonne, $larghezzaform, $h, $border, $align, $fill, $ln); |
||
87 | } |
||
88 | //now draw it |
||
89 | 1 | foreach ($vettorecelle as $posizione => $valore) { |
|
90 | 1 | if (!is_object($valore)) { |
|
91 | 1 | if (isset($modellicolonne[$posizione])) { |
|
92 | 1 | $width = ((297 * $modellicolonne[$posizione]['width']) / $larghezzaform) / 2; |
|
93 | } else { |
||
94 | $width = ((297 * 100) / $larghezzaform) / 2; |
||
95 | } |
||
96 | 1 | $pdf->MultiCell($width, $rowcount * $h, $valore, $border, $align, $fill, $ln); |
|
97 | } |
||
98 | } |
||
99 | 1 | $pdf->Ln(); |
|
100 | } |
||
101 | |||
102 | 1 | $pdf->Cell(0, 10, GrigliaFiltriUtils::traduciFiltri(array('filtri' => $risposta->filtri)), 0, false, 'L', 0, '', 0, false, 'T', 'M'); |
|
103 | |||
104 | /* |
||
105 | I: send the file inline to the browser (default). The plug-in is used if available. |
||
106 | The name given by name is used when one selects the “Save as” option on the link generating the PDF. |
||
107 | D: send to the browser and force a file download with the name given by name. |
||
108 | F: save to a local server file with the name given by name. |
||
109 | S: return the document as a string (name is ignored). |
||
110 | FI: equivalent to F + I option |
||
111 | FD: equivalent to F + D option |
||
112 | E: return the document as base64 mime multi-part email attachment (RFC 2045) |
||
113 | */ |
||
114 | |||
115 | /* In caso il pdf stampato nel browser resti fisso a caricare la pagina, |
||
116 | impostare 'D' per forzare lo scarico del file, oppure |
||
117 | mettere exit al posto di return 0; questo opzione però non è accettata da gli strumenti di controllo del codice che non si |
||
118 | aspettano exit nel codice |
||
119 | */ |
||
120 | 1 | $pdf->Output($request->get('nometabella') . '.pdf', 'I'); |
|
121 | |||
122 | 1 | return 0; |
|
123 | } |
||
124 | |||
125 | public function esportaexcel($parametri = array()) |
||
183 | |||
184 | 1 | private function printHeaderXls($modellicolonne, $testata, $sheet) |
|
219 | |||
220 | private function getValueCell($tipocampo, $vettorecella) |
||
221 | { |
||
222 | $valore = null; |
||
223 | switch ($tipocampo) { |
||
224 | case 'date': |
||
225 | $d = substr($vettorecella, 0, 2); |
||
226 | $m = substr($vettorecella, 3, 2); |
||
227 | $y = substr($vettorecella, 6, 4); |
||
228 | $t_date = \PHPExcel_Shared_Date::FormattedPHPToExcel($y, $m, $d); |
||
229 | $valore = $t_date; |
||
230 | break; |
||
231 | case 'boolean': |
||
232 | $valore = ($vettorecella == 1) ? 'SI' : 'NO'; |
||
233 | break; |
||
234 | default: |
||
235 | $valore = $vettorecella; |
||
236 | break; |
||
237 | } |
||
238 | return $valore; |
||
239 | } |
||
240 | |||
241 | 1 | private function printBodyXls($righe, $modellicolonne, $sheet) |
|
242 | { |
||
243 | 1 | $row = 2; |
|
244 | 1 | foreach ($righe as $riga) { |
|
245 | 1 | $vettorecelle = $riga->cell; |
|
246 | 1 | $col = 0; |
|
247 | 1 | foreach ($vettorecelle as $vettorecella) { |
|
248 | 1 | if ($vettorecella === '' || $vettorecella === null) { |
|
249 | $col = $col + 1; |
||
250 | continue; |
||
251 | } |
||
252 | 1 | $valore = $this->getValueCell($modellicolonne[$col]['tipocampo'], $vettorecella); |
|
253 | 1 | $sheet->setCellValueByColumnAndRow($col, $row, $valore); |
|
254 | 1 | $col = $col + 1; |
|
255 | } |
||
256 | 1 | $sheet->getRowDimension($row)->setRowHeight(18); |
|
257 | 1 | ++$row; |
|
258 | } |
||
259 | |||
260 | 1 | $indicecolonna = 0; |
|
261 | 1 | foreach ($modellicolonne as $modellocolonna) { |
|
262 | 1 | $letteracolonna = \PHPExcel_Cell::stringFromColumnIndex($indicecolonna); |
|
263 | 1 | switch ($modellocolonna['tipocampo']) { |
|
264 | case 'text': |
||
265 | $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
||
266 | ->getNumberFormat() |
||
267 | ->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_GENERAL); |
||
268 | break; |
||
269 | case 'string': |
||
270 | 1 | $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
|
271 | 1 | ->getNumberFormat() |
|
272 | 1 | ->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_GENERAL); |
|
273 | 1 | break; |
|
274 | case 'integer': |
||
275 | 1 | $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
|
276 | 1 | ->getNumberFormat() |
|
277 | 1 | ->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER); |
|
278 | 1 | break; |
|
279 | case 'float': |
||
280 | $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
||
281 | ->getNumberFormat() |
||
282 | ->setFormatCode('#,##0.00'); |
||
283 | break; |
||
284 | case 'number': |
||
285 | $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
||
286 | ->getNumberFormat() |
||
287 | ->setFormatCode('#,##0.00'); |
||
288 | break; |
||
289 | case 'datetime': |
||
290 | $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
||
291 | ->getNumberFormat() |
||
292 | ->setFormatCode('dd/mm/yyyy'); |
||
293 | break; |
||
294 | case 'date': |
||
295 | $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
||
296 | ->getNumberFormat() |
||
297 | ->setFormatCode('dd/mm/yyyy'); |
||
298 | break; |
||
299 | default: |
||
300 | $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row) |
||
301 | ->getNumberFormat() |
||
302 | ->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_GENERAL); |
||
303 | break; |
||
304 | } |
||
305 | |||
306 | 1 | ++$indicecolonna; |
|
307 | } |
||
308 | 1 | } |
|
309 | |||
310 | 1 | private function stampaTestata($pdf, $nomicolonne, $modellicolonne, $larghezzaform, $h, $border, $align, $fill, $ln) |
|
343 | } |
||
344 |
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.