Passed
Push — master ( a8e014...32f983 )
by Andrea
53:55
created

EsportaTabellaXls::esportaexcel()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 56
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 56
ccs 0
cts 37
cp 0
rs 9.7251
c 0
b 0
f 0
cc 3
eloc 33
nc 4
nop 1
crap 12

How to fix   Long Method   

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
class EsportaTabellaXls
6
{
7
    public function esportaexcel($parametri = array())
8
    {
9
        set_time_limit(960);
10
        ini_set('memory_limit', '2048M');
11
12
        $cacheMethod = \PHPExcel_CachedObjectStorageFactory::cache_to_phpTemp;
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...
13
        $cacheSettings = array('memoryCacheSize' => '8MB');
14
        \PHPExcel_Settings::setCacheStorageMethod($cacheMethod, $cacheSettings);
15
16
        //Creare un nuovo file
17
        $objPHPExcel = new \PHPExcel();
18
19
        $objPHPExcel->setActiveSheetIndex(0);
20
21
        // Set properties
22
        $objPHPExcel->getProperties()->setCreator('Comune di Firenze');
23
        $objPHPExcel->getProperties()->setLastModifiedBy('Comune di Firenze');
24
25
        $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...
26
        $rispostaj = $parametri['griglia'];
27
28
        $modellicolonne = $testata['modellocolonne'];
29
30
        //Scrittura su file
31
        $sheet = $objPHPExcel->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...
32
        $titolosheet = 'Esportazione ' . $testata['tabella'];
33
        $sheet->setTitle(substr($titolosheet, 0, 30));
34
        $sheet->getParent()->getDefaultStyle()->getFont()->setName('Verdana');
35
36
        $this->printHeaderXls($modellicolonne, $testata, $sheet);
37
38
        $risposta = json_decode($rispostaj);
39
        if (isset($risposta->rows)) {
40
            $righe = $risposta->rows;
41
        } else {
42
            $righe = array();
43
        }
44
45
        $this->printBodyXls($righe, $modellicolonne, $sheet);
46
47
        //Si crea un oggetto
48
        $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
49
        $todaydate = date('d-m-y');
50
51
        $filename = 'Exportazione_' . $testata['tabella'];
52
        $filename = $filename . '-' . $todaydate . '-' . strtoupper(md5(uniqid(rand(), true)));
53
        $filename = $filename . '.xls';
54
        $filename = sys_get_temp_dir() . DIRECTORY_SEPARATOR . $filename;
55
56
        if (file_exists($filename)) {
57
            unlink($filename);
58
        }
59
60
        $objWriter->save($filename);
61
62
        return $filename;
63
    }
64
    private function printHeaderXls($modellicolonne, $testata, $sheet)
65
    {
66
        $indicecolonnaheader = 0;
67
        $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
        foreach ($modellicolonne as $modellocolonna) {
69
            //Si imposta la larghezza delle colonne
70
            $letteracolonna = \PHPExcel_Cell::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
            $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
            $indicecolonnaheadertitle = $testata['nomicolonne'][$indicecolonnaheader];
73
            $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
            $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
            $sheet->setCellValueByColumnAndRow($indicecolonnaheader, 1, $coltitle);
76
            $sheet->getColumnDimension($letteracolonna)->setWidth($width);
77
78
            ++$indicecolonnaheader;
79
        }
80
81
        if ($indicecolonnaheader > 0) {
82
            //Si imposta il colore dello sfondo delle celle
83
            //Colore header
84
            $style_header = array(
85
                'fill' => array(
86
                    'type' => \PHPExcel_Style_Fill::FILL_SOLID,
87
                    'color' => array('rgb' => '0404B4'),
88
                ),
89
                'font' => array(
90
                    'bold' => true,
91
                    'color' => array('rgb' => 'FFFFFF'),
92
                ),
93
            );
94
            $sheet->getStyle('A1:' . $letteracolonna . '1')->applyFromArray($style_header);
95
        }
96
97
        $sheet->getRowDimension('1')->setRowHeight(20);
98
    }
99
    private function getValueCell($tipocampo, $vettorecella)
100
    {
101
        $valore = null;
102
        switch ($tipocampo) {
103
            case 'date':
104
                $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...
105
                $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...
106
                $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...
107
                $t_date = \PHPExcel_Shared_Date::FormattedPHPToExcel($y, $m, $d);
0 ignored issues
show
Bug introduced by
$m of type integer is incompatible with the type long expected by parameter $month of PHPExcel_Shared_Date::FormattedPHPToExcel(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
                $t_date = \PHPExcel_Shared_Date::FormattedPHPToExcel($y, /** @scrutinizer ignore-type */ $m, $d);
Loading history...
Bug introduced by
$d of type integer is incompatible with the type long expected by parameter $day of PHPExcel_Shared_Date::FormattedPHPToExcel(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
                $t_date = \PHPExcel_Shared_Date::FormattedPHPToExcel($y, $m, /** @scrutinizer ignore-type */ $d);
Loading history...
Bug introduced by
$y of type integer is incompatible with the type long expected by parameter $year of PHPExcel_Shared_Date::FormattedPHPToExcel(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
                $t_date = \PHPExcel_Shared_Date::FormattedPHPToExcel(/** @scrutinizer ignore-type */ $y, $m, $d);
Loading history...
108
                $valore = $t_date;
109
                break;
110
            case 'boolean':
111
                $valore = ($vettorecella == 1) ? 'SI' : 'NO';
112
                break;
113
            default:
114
                $valore = $vettorecella;
115
                break;
116
        }
117
        return $valore;
118
    }
119
    private function printBodyXls($righe, $modellicolonne, $sheet)
120
    {
121
        $row = 2;
122
        foreach ($righe as $riga) {
123
            $vettorecelle = $riga->cell;
124
            $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...
125
            foreach ($vettorecelle as $vettorecella) {
126
                if ($vettorecella === '' || $vettorecella === null) {
127
                    $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...
128
                    continue;
129
                }
130
                $valore = $this->getValueCell($modellicolonne[$col]['tipocampo'], $vettorecella);
131
                $sheet->setCellValueByColumnAndRow($col, $row, $valore);
132
                $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...
133
            }
134
            $sheet->getRowDimension($row)->setRowHeight(18);
135
            ++$row;
136
        }
137
138
        $indicecolonna = 0;
139
        foreach ($modellicolonne as $modellocolonna) {
140
            $letteracolonna = \PHPExcel_Cell::stringFromColumnIndex($indicecolonna);
141
            switch ($modellocolonna['tipocampo']) {
142
                case 'text':
143
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
144
                            ->getNumberFormat()
145
                            ->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_GENERAL);
146
                    break;
147
                case 'string':
148
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
149
                            ->getNumberFormat()
150
                            ->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_GENERAL);
151
                    break;
152
                case 'integer':
153
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
154
                            ->getNumberFormat()
155
                            ->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_NUMBER);
156
                    break;
157
                case 'float':
158
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
159
                            ->getNumberFormat()
160
                            ->setFormatCode('#,##0.00');
161
                    break;
162
                case 'number':
163
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
164
                            ->getNumberFormat()
165
                            ->setFormatCode('#,##0.00');
166
                    break;
167
                case 'datetime':
168
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
169
                            ->getNumberFormat()
170
                            ->setFormatCode('dd/mm/yyyy');
171
                    break;
172
                case 'date':
173
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
174
                            ->getNumberFormat()
175
                            ->setFormatCode('dd/mm/yyyy');
176
                    break;
177
                default:
178
                    $sheet->getStyle($letteracolonna . '2:' . $letteracolonna . $row)
179
                            ->getNumberFormat()
180
                            ->setFormatCode(\PHPExcel_Style_NumberFormat::FORMAT_GENERAL);
181
                    break;
182
            }
183
184
            ++$indicecolonna;
185
        }
186
    }
187
}
188