Completed
Push — master ( 565628...6dc795 )
by Greg
03:21
created

CsvFormatter::validate()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 18
Code Lines 8

Duplication

Lines 18
Ratio 100 %

Code Coverage

Tests 10
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 18
loc 18
ccs 10
cts 10
cp 1
rs 9.4285
cc 3
eloc 8
nc 2
nop 1
crap 3
1
<?php
2
namespace Consolidation\OutputFormatters\Formatters;
3
4
use Consolidation\OutputFormatters\FormatterInterface;
5
use Consolidation\OutputFormatters\ValidationInterface;
6
use Consolidation\OutputFormatters\Transformations\TableTransformation;
7
use Consolidation\OutputFormatters\Exception\IncompatibleDataException;
8
use Symfony\Component\Console\Output\OutputInterface;
9
10
class CsvFormatter implements FormatterInterface, ValidationInterface
11
{
12 10 View Code Duplication
    public function validate($structuredData)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
13
    {
14
        // If the provided data was of class RowsOfFields
15
        // or AssociativeList, it will be converted into
16
        // a TableTransformation object.
17 10
        if (!is_array($structuredData) && (!$structuredData instanceof TableTransformation)) {
18 1
            throw new IncompatibleDataException(
19 1
                $this,
20 1
                $structuredData,
21
                [
22 1
                    new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\RowsOfFields'),
23 1
                    new \ReflectionClass('\Consolidation\OutputFormatters\StructuredData\AssociativeList'),
24 1
                    [],
25
                ]
26 1
            );
27
        }
28 9
        return $structuredData;
29
    }
30
31
    /**
32
     * @inheritdoc
33
     */
34 9
    public function write(OutputInterface $output, $data, $options = [])
35
    {
36 9
        if ($this->isMultiLine($data)) {
37 4
            $this->writeCsvLines($output, $data, $options);
38 4
            return;
39
        }
40 5
        $this->writeCsvLine($output, $data, $options);
41 5
    }
42
43 9
    protected function isMultiLine($data)
44
    {
45
        return
46 9
            ($data instanceof TableTransformation) ||
47
            (
48 6
                !empty($data) &&
49 6
                is_array($data[0])
50 9
            );
51
    }
52
53 4
    protected function writeCsvLines(OutputInterface $output, $data, $options)
54
    {
55
        $options += [
56 4
            'include-field-labels' => true,
57
        ];
58
59 4
        if ($options['include-field-labels'] && ($data instanceof TableTransformation)) {
60 3
            $headers = $data->getHeaders();
61 3
            $this->writeCsvLine($output, $headers, $options);
62 3
        }
63
64 4
        foreach ($data as $line) {
65 4
            $this->writeCsvLine($output, $line, $options);
66 4
        }
67 4
    }
68
69 9
    protected function writeCsvLine(OutputInterface $output, $data, $options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
70
    {
71 9
        $output->write($this->csvEscape($data));
72 9
    }
73
74 9
    protected function csvEscape($data, $delimiter = ',')
75
    {
76 9
        $buffer = fopen('php://temp', 'r+');
77 9
        fputcsv($buffer, $data, $delimiter);
78 9
        rewind($buffer);
79 9
        $csv = fgets($buffer);
80 9
        fclose($buffer);
81 9
        return $csv;
82
    }
83
}
84