Completed
Pull Request — master (#10)
by Greg
02:12
created

FormatterManager   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 9
Bugs 0 Features 6
Metric Value
c 9
b 0
f 6
dl 0
loc 66
wmc 11
lcom 1
cbo 2
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 12 1
A write() 0 12 1
A getFormatter() 0 10 4
A validateData() 0 16 3
A restructureData() 0 7 2
1
<?php
2
namespace Consolidation\OutputFormatters;
3
4
use Symfony\Component\Console\Output\OutputInterface;
5
6
/**
7
 * Manage a collection of formatters; return one on request.
8
 */
9
class FormatterManager
10
{
11
    protected $formatters = [];
12
13
    public function __construct()
14
    {
15
        $this->formatters = [
16
            'yaml' => '\Consolidation\OutputFormatters\Formatters\YamlFormatter',
17
            'json' => '\Consolidation\OutputFormatters\Formatters\JsonFormatter',
18
            'print-r' => '\Consolidation\OutputFormatters\Formatters\PrintRFormatter',
19
            'var_export' => '\Consolidation\OutputFormatters\Formatters\VarExportFormatter',
20
            'list' => '\Consolidation\OutputFormatters\Formatters\ListFormatter',
21
            'csv' => '\Consolidation\OutputFormatters\Formatters\CsvFormatter',
22
            'table' => '\Consolidation\OutputFormatters\Formatters\TableFormatter',
23
        ];
24
    }
25
26
    public function write(OutputInterface $output, $format, $structuredOutput, $annotationData = [], $options = [])
27
    {
28
        $formatter = $this->getFormatter($format, $annotationData);
29
30
        // Restructure the output data (e.g. select fields to display, etc.).
31
        $structuredOutput = $this->restructureData($structuredOutput, $annotationData, $options);
32
33
        // Make sure that the provided data is in the correct format for the selected formatter.
34
        $structuredOutput = $this->validateData($formatter, $structuredOutput);
0 ignored issues
show
Documentation introduced by
$formatter is of type object|null, but the function expects a object<Consolidation\Out...ers\FormatterInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
35
36
        $formatter->write($output, $structuredOutput, $options);
37
    }
38
39
    public function getFormatter($format, $configurationData = [])
40
    {
41
        if (is_string($format) && array_key_exists($format, $this->formatters)) {
42
            $formatter = new $this->formatters[$format];
43
            if ($formatter instanceof ConfigureInterface) {
44
                $formatter->configure($configurationData);
45
            }
46
            return $formatter;
47
        }
48
    }
49
50
    public function validateData(FormatterInterface $formatter, $structuredOutput)
51
    {
52
        // If the formatter implements ValidationInterface, then let it
53
        // test the data and throw or return an error
54
        if ($formatter instanceof ValidationInterface) {
55
            return $formatter->validate($structuredOutput);
56
        }
57
        // If the formatter does not implement ValidationInterface, then
58
        // it will never be passed an ArrayObject; we will always give
59
        // it a simple array.
60
        if ($structuredOutput instanceof \ArrayObject) {
61
            return $structuredOutput->getArrayCopy();
62
        }
63
64
        return $structuredOutput;
65
    }
66
67
    public function restructureData($structuredOutput, $configurationData, $options)
68
    {
69
        if ($structuredOutput instanceof RestructureInterface) {
70
            return $structuredOutput->restructure($configurationData, $options);
71
        }
72
        return $structuredOutput;
73
    }
74
}
75