Passed
Push — master ( 3ebd61...e5f7f7 )
by Radu
01:13
created

CsvCreator::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace WebServCo\Framework;
3
4
final class CsvCreator
5
{
6
    protected $delimiter;
7
    protected $enclosure;
8
9
    public function __construct($delimiter = ',', $enclosure = '"')
10
    {
11
        $this->delimiter = $delimiter;
12
        $this->enclosure = $enclosure;
13
    }
14
15
    public function getCsvFile(string $fileName, array $data, bool $addHeader = true)
16
    {
17
        $csvData = $this->getCsvData($data, $addHeader);
18
        return new \WebServCo\Framework\CsvFile($fileName, $csvData);
0 ignored issues
show
Bug introduced by
It seems like $csvData can also be of type false; however, parameter $fileData of WebServCo\Framework\CsvFile::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

18
        return new \WebServCo\Framework\CsvFile($fileName, /** @scrutinizer ignore-type */ $csvData);
Loading history...
19
    }
20
21
    public function getCsvData(array $data, bool $addHeader = true)
22
    {
23
        $csvData = null;
0 ignored issues
show
Unused Code introduced by
The assignment to $csvData is dead and can be removed.
Loading history...
24
25
        if (empty($data)) {
26
            return false;
27
        }
28
29
        // temporary memory wrapper; if bigger than 5MB will be written to temp file.
30
        $handle = fopen('php://temp/maxmemory: ' . (5*1024*1024), 'r+');
31
32
        if ($addHeader) {
33
            fputcsv($handle, array_keys(current($data)), $this->delimiter, $this->enclosure);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fputcsv() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
            fputcsv(/** @scrutinizer ignore-type */ $handle, array_keys(current($data)), $this->delimiter, $this->enclosure);
Loading history...
34
        }
35
36
        foreach ($data as $item) {
37
            fputcsv($handle, $item, $this->delimiter, $this->enclosure);
38
        }
39
40
        rewind($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of rewind() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

40
        rewind(/** @scrutinizer ignore-type */ $handle);
Loading history...
41
42
        $csvData = stream_get_contents($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of stream_get_contents() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

42
        $csvData = stream_get_contents(/** @scrutinizer ignore-type */ $handle);
Loading history...
43
44
        fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

44
        fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
45
46
        return $csvData;
47
    }
48
}
49