Completed
Push — master ( c9e75c...9f5802 )
by Patrick
12:31 queued 10:30
created

ExcelSerializer   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 53
Duplicated Lines 13.21 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
dl 7
loc 53
rs 10
c 0
b 0
f 0
wmc 9
lcom 1
cbo 4

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setRowFromArray() 7 14 4
A serializeData() 0 33 5

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
require_once dirname(__FILE__).'/../vendor/autoload.php';
4
5
use PhpOffice\PhpSpreadsheet\Spreadsheet;
6
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
7
use PhpOffice\PhpSpreadsheet\Writer\Xls;
8
9
class ExcelSerializer extends SpreadSheetSerializer
10
{
11
    protected $types = array('xlsx', 'xls', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/vnd.ms-excel');
12
13
    protected function setRowFromArray(&$sheat, $row, $array, $count = 0)
14
    {
15
        if($count === 0)
16
        {
17
            $count = count($array);
18
        }
19 View Code Duplication
        for($i = 0; $i < $count; $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...
20
        {
21
            if(isset($array[$i]))
22
            {
23
                $sheat->setCellValueByColumnAndRow($i+1, $row, $array[$i]);
24
            }
25
        }
26
    }
27
28
    public function serializeData(&$type, $array)
29
    {
30
        if($this->supportsType($type) === false)
31
        {
32
            return null;
33
        }
34
        $data = $this->getArray($array);
35
        $ssheat = new Spreadsheet();
36
        $sheat = $ssheat->getActiveSheet();
37
        $keys = array_shift($data);
38
        $rowCount = count($data);
39
        $colCount = count($keys);
40
        $this->setRowFromArray($sheat, 1, $keys, $colCount);
41
        for($i = 0; $i < $rowCount; $i++)
42
        {
43
            $this->setRowFromArray($sheat, (2 + $i), $data[$i], $colCount);
44
        }
45
        $writerType = 'Excel5';
0 ignored issues
show
Unused Code introduced by
$writerType 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...
46
        if(strcasecmp($type, 'xlsx') === 0 || strcasecmp($type, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet') === 0)
47
        {
48
            $writer = new Xlsx($ssheat);
49
            $writerType = 'Excel2007';
0 ignored issues
show
Unused Code introduced by
$writerType 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...
50
            $type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
51
        }
52
        else
53
        {
54
            $writer = new Xls($ssheat);
55
            $type = 'application/vnd.ms-excel';
56
        }
57
        ob_start();
58
        $writer->save('php://output');
59
        return ob_get_clean();
60
    }
61
}
62