Completed
Push — master ( d887f9...fd888a )
by Patrick
03:01
created

ExcelSerializer::serializeData()   C

Complexity

Conditions 8
Paths 14

Size

Total Lines 43
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 8
eloc 27
nc 14
nop 2
dl 0
loc 43
rs 5.3846
c 1
b 0
f 1
1
<?php
2
namespace Serialize;
3
4
require_once dirname(__FILE__) . '/../libs/PHPExcel/Classes/PHPExcel.php';
5
6
class ExcelSerializer extends SpreadSheetSerializer
7
{
8
    protected $types = array('xlsx', 'xls', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel');
9
10
    public function serializeData($type, $array)
11
    {
12
        if($this->supportsType($type) === false)
13
        {
14
            return null;
15
        }
16
        if(count($array) === 0)
17
        {
18
            return null;
19
        }
20
        $data = $this->getArray($array);
21
        $ssheat = new \PHPExcel();
22
        $sheat = $ssheat->setActiveSheetIndex(0);
23
        $keys = array_shift($data);
24
        $rowCount = count($data);
25
        $colCount = count($keys);
26
        for($i = 0; $i < $colCount; $i++)
27
        {
28
            $sheat->setCellValueByColumnAndRow($i, 1, $keys[$i]);
29
        }
30
        for($i = 0; $i < $rowCount; $i++)
31
        {
32
            for($j = 0; $j < $colCount; $j++)
33
            {
34
                $colName = $keys[$j];
0 ignored issues
show
Unused Code introduced by
$colName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
35
                $sheat->setCellValueByColumnAndRow($j, 2+$i, $data[$i][$j]);
36
            }
37
        }
38
        if(strcasecmp($type, 'xlsx') === 0 || strcasecmp($type, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') === 0)
39
        {
40
            $writer = \PHPExcel_IOFactory::createWriter($ssheat, 'Excel2007');
41
            ob_start();
42
            $writer->save('php://output');
43
            return ob_get_clean();
44
        }
45
        else
46
        {
47
            $writer = \PHPExcel_IOFactory::createWriter($ssheat, 'Excel5');
48
            ob_start();
49
            $writer->save('php://output');
50
            return ob_get_clean();
51
        }
52
    }
53
}
54
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
55