Completed
Push — master ( e938ca...eb16f0 )
by Patrick
02:57
created

ExcelSerializer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 42
Duplicated Lines 19.05 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 2
Bugs 1 Features 1
Metric Value
c 2
b 1
f 1
dl 8
loc 42
rs 10
wmc 8
lcom 0
cbo 1

1 Method

Rating   Name   Duplication   Size   Complexity  
C serializeData() 8 37 8

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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 View Code Duplication
        for($i = 0; $i < $colCount; $i++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
27
        {
28
            $sheat->setCellValueByColumnAndRow($i, 1, $keys[$i]);
29
        }
30
        for($i = 0; $i < $rowCount; $i++)
31
        {
32 View Code Duplication
            for($j = 0; $j < $colCount; $j++)
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
33
            {
34
                $sheat->setCellValueByColumnAndRow($j, (2 + $i), $data[$i][$j]);
35
            }
36
        }
37
        $writerType = 'Excel5';
38
        if(strcasecmp($type, 'xlsx') === 0 || strcasecmp($type, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') === 0)
39
        {
40
            $writerType = 'Excel2007';
41
        }
42
        $writer = \PHPExcel_IOFactory::createWriter($ssheat, $writerType);
43
        ob_start();
44
        $writer->save('php://output');
45
        return ob_get_clean();
46
    }
47
}
48