ExcelWriter   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 6 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 1
cbo 5
dl 6
loc 100
ccs 38
cts 38
cp 1
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A prepare() 0 16 4
B writeItem() 6 21 5
A finish() 0 5 1

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
3
namespace Port\Excel;
4
5
use Port\Writer;
6
use PHPExcel;
7
use PHPExcel_IOFactory;
8
9
/**
10
 * Writes to an Excel file
11
 *
12
 * @author David de Boer <[email protected]>
13
 */
14
class ExcelWriter implements Writer
15
{
16
    /**
17
     * @var string
18
     */
19
    protected $filename;
20
21
    /**
22
     * @var null|string
23
     */
24
    protected $sheet;
25
26
    /**
27
     * @var string
28
     */
29
    protected $type;
30
31
    /**
32
     * @var boolean
33
     */
34
    protected $prependHeaderRow;
35
36
    /**
37
     * @var PHPExcel
38
     */
39
    protected $excel;
40
41
    /**
42
     * @var integer
43
     */
44
    protected $row = 1;
45
46
    /**
47
     * @param \SplFileObject $file  File
48
     * @param string         $sheet Sheet title (optional)
49
     * @param string         $type  Excel file type (defaults to Excel2007)
50
     * @param boolean        $prependHeaderRow
51
     */
52 4
    public function __construct(\SplFileObject $file, $sheet = null, $type = 'Excel2007', $prependHeaderRow = false)
53
    {
54 4
        $this->filename = $file->getPathname();
55 4
        $this->sheet = $sheet;
56 4
        $this->type = $type;
57 4
        $this->prependHeaderRow = $prependHeaderRow;
58 4
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 4
    public function prepare()
64
    {
65 4
        $reader = PHPExcel_IOFactory::createReader($this->type);
66 4
        if ($reader->canRead($this->filename)) {
67 1
            $this->excel = $reader->load($this->filename);
68 1
        } else {
69 4
            $this->excel = new PHPExcel();
70
        }
71
72 4
        if (null !== $this->sheet) {
73 1
            if (!$this->excel->sheetNameExists($this->sheet)) {
74 1
                $this->excel->createSheet()->setTitle($this->sheet);
75 1
            }
76 1
            $this->excel->setActiveSheetIndexByName($this->sheet);
77 1
        }
78 4
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 4
    public function writeItem(array $item)
84
    {
85 4
        $count = count($item);
86
87 4
        if ($this->prependHeaderRow && 1 == $this->row) {
88 1
            $headers = array_keys($item);
89
90 1 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...
91 1
                $this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $headers[$i]);
92 1
            }
93 1
            $this->row++;
94 1
        }
95
96 4
        $values = array_values($item);
97
98 4 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...
99 4
            $this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $values[$i]);
100 4
        }
101
102 4
        $this->row++;
103 4
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 4
    public function finish()
109
    {
110 4
        $writer = \PHPExcel_IOFactory::createWriter($this->excel, $this->type);
111 4
        $writer->save($this->filename);
112 4
    }
113
}
114