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

CsvFormatter   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 74
Duplicated Lines 24.32 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 97.62%

Importance

Changes 2
Bugs 0 Features 2
Metric Value
wmc 14
c 2
b 0
f 2
lcom 1
cbo 3
dl 18
loc 74
ccs 41
cts 42
cp 0.9762
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A validate() 18 18 3
A write() 0 8 2
A isMultiLine() 0 9 3
A writeCsvLines() 0 15 4
A writeCsvLine() 0 4 1
A csvEscape() 0 9 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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