These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace SilverStripe\SsPak\DataExtractor; |
||
4 | |||
5 | class CsvTableWriter implements TableWriter |
||
6 | { |
||
7 | |||
8 | private $filename; |
||
9 | private $handle; |
||
10 | private $columns; |
||
11 | |||
12 | function __construct($filename) { |
||
0 ignored issues
–
show
|
|||
13 | $this->filename = $filename; |
||
14 | } |
||
15 | |||
16 | function start($columns) { |
||
0 ignored issues
–
show
|
|||
17 | $this->open(); |
||
18 | $this->putRow($columns); |
||
19 | $this->columns = $columns; |
||
20 | } |
||
21 | |||
22 | function finish() { |
||
0 ignored issues
–
show
|
|||
23 | $this->close(); |
||
24 | } |
||
25 | |||
26 | function writeRecord($record) { |
||
0 ignored issues
–
show
|
|||
27 | if (!$this->columns) { |
||
0 ignored issues
–
show
The expression
$this->columns of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent. Consider making the comparison explicit by using
Loading history...
|
|||
28 | $this->start(array_keys($record)); |
||
29 | } |
||
30 | |||
31 | $this->putRow($this->mapFromColumns($record)); |
||
32 | } |
||
33 | |||
34 | private function mapFromColumns($record) { |
||
35 | $row = []; |
||
36 | foreach($this->columns as $i => $column) |
||
37 | { |
||
38 | $row[$i] = isset($record[$column]) ? $record[$column] : null; |
||
39 | } |
||
40 | return $row; |
||
41 | } |
||
42 | |||
43 | private function putRow($row) { |
||
44 | return fputcsv($this->handle, $row); |
||
45 | } |
||
46 | |||
47 | private function open() { |
||
48 | if ($this->handle) { |
||
49 | fclose($this->handle); |
||
50 | $this->handle = null; |
||
51 | } |
||
52 | $this->handle = fopen($this->filename, 'w'); |
||
53 | if (!$this->handle) { |
||
54 | throw new \LogicException("Can't open $this->filename for writing."); |
||
55 | } |
||
56 | } |
||
57 | |||
58 | private function close() { |
||
59 | if ($this->handle) { |
||
60 | fclose($this->handle); |
||
61 | $this->handle = null; |
||
62 | } |
||
63 | } |
||
64 | } |
||
65 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.