Passed
Push — master ( 2a39ec...3b6c56 )
by Andrea
03:42
created

EsportaTabellaXls::printBodyXls()   C

Complexity

Conditions 13
Paths 36

Size

Total Lines 67
Code Lines 58

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 46
CRAP Score 14.4968

Importance

Changes 0
Metric Value
eloc 58
dl 0
loc 67
ccs 46
cts 58
cp 0.7931
rs 6.6166
c 0
b 0
f 0
cc 13
nc 36
nop 3
crap 14.4968

How to fix   Long Method    Complexity   

Long Method

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:

1
<?php
2
3
namespace Fi\CoreBundle\DependencyInjection;
4
5
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6
use PhpOffice\PhpSpreadsheet\Writer\Xls;
7
use PhpOffice\PhpSpreadsheet\Style\Fill;
8
use PhpOffice\PhpSpreadsheet\Style\Border;
9
10
class EsportaTabellaXls
11
{
12 1
    public function esportaexcel($parametri = array())
13
    {
14 1
        set_time_limit(960);
15 1
        ini_set('memory_limit', '2048M');
16
17
        //Creare un nuovo file
18 1
        $spreadsheet = new Spreadsheet();  /* ----Spreadsheet object----- */
19
        $objPHPExcel = new Xls($spreadsheet);  /* ----- Excel (Xls) Object */
20 1
        $spreadsheet->setActiveSheetIndex(0);
21
22
        // Set properties
23 1
        $spreadsheet->getProperties()->setCreator('Comune di Firenze');
24 1
        $spreadsheet->getProperties()->setLastModifiedBy('Comune di Firenze');
25
26 1
        $testata = $parametri['testata'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 3 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
27 1
        $rispostaj = $parametri['griglia'];
28
29 1
        $modellicolonne = $testata['modellocolonne'];
30
31
        //Scrittura su file
32 1
        $sheet = $spreadsheet->getActiveSheet();
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 7 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
33 1
        $titolosheet = 'Esportazione ' . $testata['tabella'];
34 1
        $sheet->setTitle(substr($titolosheet, 0, 30));
35 1
        $sheet->getParent()->getDefaultStyle()->getFont()->setName('Verdana');
36
37 1
        $this->printHeaderXls($modellicolonne, $testata, $sheet);
38
39 1
        $risposta = json_decode($rispostaj);
40 1
        if (isset($risposta->rows)) {
41 1
            $righe = $risposta->rows;
42
        } else {
43
            $righe = array();
44
        }
45
46 1
        $this->printBodyXls($righe, $modellicolonne, $sheet);
47
48
        //Si crea un oggetto
49 1
        $todaydate = date('d-m-y');
50
51 1
        $filename = 'Exportazione_' . $testata['tabella'];
52 1
        $filename = $filename . '-' . $todaydate . '-' . strtoupper(md5(uniqid(rand(), true)));
53 1
        $filename = $filename . '.xls';
54 1
        $filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename;
55
56 1
        if (file_exists($filename)) {
57
            unlink($filename);
58
        }
59
60 1
        $objPHPExcel->save($filename);
61
62 1
        return $filename;
63
    }
64 2
    private function printHeaderXls($modellicolonne, $testata, $sheet)
65
    {
66 2
        $indicecolonnaheader = 0;
67 2
        $letteracolonna = 0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
68 2
        foreach ($modellicolonne as $modellocolonna) {
69
            //Si imposta la larghezza delle colonne
70 2
            $letteracolonna = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($indicecolonnaheader);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 11 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
71 2
            $width = (int) $modellocolonna['width'] / 7;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 20 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
72 2
            $indicecolonnaheadertitle = $testata['nomicolonne'][$indicecolonnaheader];
73 2
            $coltitlecalc = isset($indicecolonnaheadertitle) ? $indicecolonnaheadertitle : $modellocolonna['name'];
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 13 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
74 2
            $coltitle = strtoupper($coltitlecalc);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 17 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
75 2
            $sheet->setCellValueByColumnAndRow($indicecolonnaheader, 1, $coltitle);
76 2
            $sheet->getColumnDimension($letteracolonna)->setWidth($width);
77
78 2
            ++$indicecolonnaheader;
79
        }
80
81 2
        if ($indicecolonnaheader > 0) {
82
            //Si imposta il colore dello sfondo delle celle
83
            //Colore header
84
            $style_header = array(
85 2
                'fill' => array(
86 2
                    'type' => Fill::FILL_SOLID,
87
                    'color' => array('rgb' => 'E5E4E2')
88
                ),
89
                'font' => array(
90
                    'bold' => true
91
                )
92
            );
93 2
            $sheet->getStyle('A1:' . $letteracolonna . '1')->applyFromArray($style_header);
94
        }
95
96 2
        $sheet->getRowDimension('1')->setRowHeight(20);
97 2
    }
98
    private function getValueCell($tipocampo, $vettorecella)
99
    {
100
        $valore = null;
101
        switch ($tipocampo) {
102
            case 'date':
103
                $d = (int) substr($vettorecella, 0, 2);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
104
                $m = (int) substr($vettorecella, 3, 2);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
105
                $y = (int) substr($vettorecella, 6, 4);
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 6 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
106
                $t_date = \PhpOffice\PhpSpreadsheet\Shared\Date::formattedPHPToExcel($y, $m, $d);
107
                $valore = $t_date;
108
                break;
109
            case 'boolean':
110
                $valore = ($vettorecella == 1) ? 'SI' : 'NO';
111
                break;
112
            default:
113
                $valore = $vettorecella;
114
                break;
115
        }
116
        return $valore;
117
    }
118 2
    private function printBodyXls($righe, $modellicolonne, $sheet)
119
    {
120 2
        $row = 2;
121 2
        foreach ($righe as $riga) {
122 2
            $vettorecelle = $riga->cell;
123 2
            $col = 0;
0 ignored issues
show
Coding Style introduced by
Equals sign not aligned with surrounding assignments; expected 10 spaces but found 1 space

This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.

To visualize

$a = "a";
$ab = "ab";
$abc = "abc";

will produce issues in the first and second line, while this second example

$a   = "a";
$ab  = "ab";
$abc = "abc";

will produce no issues.

Loading history...
124 2
            foreach ($vettorecelle as $vettorecella) {
125 2
                if ($vettorecella === '' || $vettorecella === null) {
126 1
                    $col = $col + 1;
0 ignored issues
show
Coding Style introduced by
Increment operators should be used where possible; found "$col = $col + 1;" but expected "$col++"
Loading history...
127 1
                    continue;
128
                }
129 2
                $valore = $this->getValueCell($modellicolonne[$col]['tipocampo'], $vettorecella);
130 2
                $sheet->setCellValueByColumnAndRow($col, $row, $valore);
131 2
                $col = $col + 1;
0 ignored issues
show
Coding Style introduced by
Increment operators should be used where possible; found "$col = $col + 1;" but expected "$col++"
Loading history...
132
            }
133 2
            $sheet->getRowDimension($row)->setRowHeight(18);
134 2
            ++$row;
135
        }
136
137 2
        $indicecolonna = 0;
138 2
        foreach ($modellicolonne as $modellocolonna) {
139 2
            $letteracolonna = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($indicecolonna);
140 2
            switch ($modellocolonna['tipocampo']) {
141 2
                case 'text':
142
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
143
                            ->getNumberFormat()
144
                            ->setFormatCode("@");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal @ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
145
                    break;
146 2
                case 'string':
147 2
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
148 2
                            ->getNumberFormat()
149 2
                            ->setFormatCode("@");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal @ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
150 2
                    break;
151 2
                case 'integer':
152 2
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
153 2
                            ->getNumberFormat()
154 2
                            ->setFormatCode(\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_NUMBER);
155 2
                    break;
156 1
                case 'float':
157 1
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
158 1
                            ->getNumberFormat()
159 1
                            ->setFormatCode('#,##0.00');
160 1
                    break;
161 1
                case 'number':
162
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
163
                            ->getNumberFormat()
164
                            ->setFormatCode('#,##0.00');
165
                    break;
166 1
                case 'datetime':
167
                    //\PhpOffice\PhpSpreadsheet\Style\NumberFormat::FORMAT_DATE_DDMMYYYYSLASH
168
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
169
                            ->getNumberFormat()
170
                            ->setFormatCode("dd/mm/yyyy");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal dd/mm/yyyy does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
171
                    break;
172 1
                case 'date':
173 1
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
174 1
                            ->getNumberFormat()
175 1
                            ->setFormatCode("dd/mm/yyyy");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal dd/mm/yyyy does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
176 1
                    break;
177
                default:
178 1
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
179 1
                            ->getNumberFormat()
180 1
                            ->setFormatCode("@");
0 ignored issues
show
Coding Style Comprehensibility introduced by
The string literal @ does not require double quotes, as per coding-style, please use single quotes.

PHP provides two ways to mark string literals. Either with single quotes 'literal' or with double quotes "literal". The difference between these is that string literals in double quotes may contain variables with are evaluated at run-time as well as escape sequences.

String literals in single quotes on the other hand are evaluated very literally and the only two characters that needs escaping in the literal are the single quote itself (\') and the backslash (\\). Every other character is displayed as is.

Double quoted string literals may contain other variables or more complex escape sequences.

<?php

$singleQuoted = 'Value';
$doubleQuoted = "\tSingle is $singleQuoted";

print $doubleQuoted;

will print an indented: Single is Value

If your string literal does not contain variables or escape sequences, it should be defined using single quotes to make that fact clear.

For more information on PHP string literals and available escape sequences see the PHP core documentation.

Loading history...
181 1
                    break;
182
            }
183
184 2
            ++$indicecolonna;
185
        }
186 2
    }
187
}
188