Completed
Push — master ( 0fb3d7...53ff75 )
by Markus
03:02
created

ExcelWriter   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 100
Duplicated Lines 6 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 75%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 11
c 1
b 1
f 0
lcom 1
cbo 6
dl 6
loc 100
ccs 24
cts 32
cp 0.75
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A finish() 0 5 1
A prepare() 0 16 4
B writeItem() 6 21 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
3
namespace Mathielen\DataImport\Writer;
4
5
use Ddeboer\DataImport\Writer\AbstractWriter;
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 extends AbstractWriter
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 2
    public function __construct(\SplFileObject $file, $sheet = null, $type = 'Excel2007', $prependHeaderRow = false)
53
    {
54 2
        $this->filename = $file->getPathname();
55 2
        $this->sheet = $sheet;
56 2
        $this->type = $type;
57 2
        $this->prependHeaderRow = $prependHeaderRow;
58 2
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 2
    public function prepare()
64
    {
65 2
        $reader = PHPExcel_IOFactory::createReader($this->type);
66 2
        if ($reader->canRead($this->filename)) {
67
            $this->excel = $reader->load($this->filename);
68
        } else {
69 2
            $this->excel = new PHPExcel();
70
        }
71
72 2
        if (null !== $this->sheet) {
73
            if (!$this->excel->sheetNameExists($this->sheet)) {
74
                $this->excel->createSheet()->setTitle($this->sheet);
75
            }
76
            $this->excel->setActiveSheetIndexByName($this->sheet);
77
        }
78 2
    }
79
80
    /**
81
     * {@inheritdoc}
82
     */
83 2
    public function writeItem(array $item)
84
    {
85 2
        $count = count($item);
86
87 2
        if ($this->prependHeaderRow && 1 == $this->row) {
88
            $headers = array_keys($item);
89
90 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
                $this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $headers[$i]);
92
            }
93
            $this->row++;
94
        }
95
96 2
        $values = array_values($item);
97
98 2 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 2
            $this->excel->getActiveSheet()->setCellValueByColumnAndRow($i, $this->row, $values[$i]);
100
        }
101
102 2
        $this->row++;
103 2
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 2
    public function finish()
109
    {
110 2
        $writer = \PHPExcel_IOFactory::createWriter($this->excel, $this->type);
111 2
        $writer->save($this->filename);
112 2
    }
113
}
114