Passed
Branch develop (e9a151)
by Stefan
03:34 queued 45s
created

Writer::addRow()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
crap 1
1
<?php
2
3
namespace OneSheet;
4
5
use OneSheet\Style\Style;
6
use OneSheet\Style\Styler;
7
8
/**
9
 * Class Writer
10
 *
11
 * @package OneSheet
12
 */
13
class Writer
14
{
15
    /**
16
     * @var SheetFile
17
     */
18
    private $sheetFile;
19
20
    /**
21
     * @var Styler
22
     */
23
    private $styler;
24
25
    /**
26
     * @var Sheet
27
     */
28
    private $sheet;
29
30
    /**
31
     * Writer constructor.
32
     *
33
     * @param string $encoding
34
     */
35 2
    public function __construct($encoding = 'utf-8')
36
    {
37 2
        $this->encoding = $encoding;
0 ignored issues
show
Bug introduced by
The property encoding does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38 2
        $this->initialize();
39 2
    }
40
41
    /**
42
     * Initialize writer defaults.
43
     */
44 2
    private function initialize()
45
    {
46 2
        $this->sheetFile = new SheetFile(sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid() . '.xml', 'wb+');
0 ignored issues
show
Unused Code introduced by
The call to SheetFile::__construct() has too many arguments starting with sys_get_temp_dir() . DIR...TOR . uniqid() . '.xml'.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
47 2
        $this->sheetFile->fwrite(str_repeat(' ', pow(2, 20)) . '<sheetData>');
48 2
        $this->sheet = new Sheet();
49 2
        $this->styler = new Styler();
50 2
    }
51
52
    /**
53
     * @return Sheet
54
     */
55 2
    public function getSheet()
56
    {
57 2
        return $this->sheet;
58
    }
59
60
    /**
61
     * @param array     $rows
62
     * @param Style|int $style
63
     */
64 1
    public function addRows(array $rows, $style = 0)
65
    {
66 1
        foreach ($rows as $row) {
67 1
            $this->addRow($row, $style);
68 1
        }
69 1
    }
70
71
    /**
72
     * @param array     $row
73
     * @param Style|int $style
74
     */
75 1
    public function addRow(array $row, $style = 0)
76
    {
77 1
        $style = $this->loadOrRegisterStyle($style);
78 1
        $this->sheetFile->fwrite($this->sheet->addRow($row, $style));
79 1
    }
80
81
    /**
82
     * Load or register a given style.
83
     *
84
     * @param Style|int $style
85
     * @return Style
86
     */
87 1
    private function loadOrRegisterStyle($style = 0)
88
    {
89 1
        if ($style instanceof Style) {
90 1
            $this->styler->addStyle($style);
91 1
        } else {
92
            $style = $this->styler->getStyleById((int)$style);
93
        }
94
95 1
        return $style;
96
    }
97
98
    /**
99
     * Wrap things up and write/output xlsx.
100
     *
101
     * @param string $fileName
102
     */
103 1
    public function writeToFile($fileName = 'dummy.xlsx')
104
    {
105 1
        $finalizer = new Finalizer($this->sheet, $this->styler, $this->sheetFile);
106 1
        $finalizer->finalize($fileName);
107 1
    }
108
}
109