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

StringWriter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Test Coverage

Coverage 90.91%

Importance

Changes 1
Bugs 1 Features 0
Metric Value
wmc 4
c 1
b 1
f 0
lcom 0
cbo 2
dl 0
loc 25
ccs 10
cts 11
cp 0.9091
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
B write() 0 22 4
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