Passed
Push — master ( d1a01a...ada359 )
by Andrew
01:39
created

StringWriter::write()   B

Complexity

Conditions 4
Paths 3

Size

Total Lines 22
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 22
ccs 10
cts 11
cp 0.9091
rs 8.9197
cc 4
eloc 14
nc 3
nop 2
crap 4.0119
1
<?php
2
3
namespace CsvParser\Writer;
4
5
class StringWriter implements WriterInterface
6
{
7 4
    public static function write(\CsvParser\Parser $parser, \CsvParser\Csv $csv)
8
    {
9 4
        $data = $csv->getData();
10
11 4
        if ($data && !empty($data)) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $data 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 empty(..) or ! empty(...) instead.

Loading history...
12
            $output = array(
13
                implode($parser->fieldDelimiter, array_map(function ($value) use ($parser) {
14 4
                    return $parser->fieldEnclosure . str_replace($parser->fieldEnclosure, $parser->fieldEnclosure.$parser->fieldEnclosure, $value) . $parser->fieldEnclosure;
15 4
                }, array_keys(current($data))))
16
            );
17
18 4
            foreach ($data as $line) {
19 4
                $output[] = implode($parser->fieldDelimiter, array_map(function ($value) use ($parser) {
20 4
                    return $parser->fieldEnclosure . str_replace($parser->fieldEnclosure, $parser->fieldEnclosure.$parser->fieldEnclosure, $value) . $parser->fieldEnclosure;
21 4
                }, $line));
22
            }
23
24 4
            return implode($parser->lineDelimiter, $output);
25
        } else {
26
            return '';
27
        }
28
    }
29
}
30