Completed
Push — master ( 838b08...10b695 )
by Greg
02:10
created

FormatterManager::write()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 1
eloc 5
nc 1
nop 5
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
    /**
27
     * Format and write output
28
     *
29
     * @param OutputInterface $output Output stream to write to
30
     * @param string $format Data format to output in
31
     * @param mixed $structuredOutput Data to output
32
     * @param array $annotationData Configuration information for formatter
33
     * @param array $options User options
34
     */
35
    public function write(OutputInterface $output, $format, $structuredOutput, $annotationData = [], $options = [])
36
    {
37
        $formatter = $this->getFormatter($format, $annotationData);
38
39
        // Restructure the output data (e.g. select fields to display, etc.).
40
        $structuredOutput = $this->restructureData($structuredOutput, $annotationData, $options);
41
42
        // Make sure that the provided data is in the correct format for the selected formatter.
43
        $structuredOutput = $this->validateData($formatter, $structuredOutput);
0 ignored issues
show
Bug introduced by
It seems like $formatter defined by $this->getFormatter($format, $annotationData) on line 37 can be null; however, Consolidation\OutputForm...Manager::validateData() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
44
45
        $formatter->write($output, $structuredOutput, $options);
46
    }
47
48
    /**
49
     * Fetch the requested formatter.
50
     *
51
     * @param string $format Identifier for requested formatter
52
     * @param array $configurationData Configuration data for formatter
53
     * @return FormatterInterface
54
     */
55
    public function getFormatter($format, $configurationData = [])
56
    {
57
        if (is_string($format) && array_key_exists($format, $this->formatters)) {
58
            $formatter = new $this->formatters[$format];
59
            if ($formatter instanceof ConfigureInterface) {
60
                $formatter->configure($configurationData);
61
            }
62
            return $formatter;
63
        }
64
    }
65
66
    /**
67
     * Determine if the provided data is compatible with the formatter being used.
68
     *
69
     * @param FormatterInterface $formatter Formatter being used
70
     * @param mixed $structuredOutput Data to validate
71
     * @return mixed
72
     */
73
    public function validateData(FormatterInterface $formatter, $structuredOutput)
74
    {
75
        // If the formatter implements ValidationInterface, then let it
76
        // test the data and throw or return an error
77
        if ($formatter instanceof ValidationInterface) {
78
            return $formatter->validate($structuredOutput);
79
        }
80
        // If the formatter does not implement ValidationInterface, then
81
        // it will never be passed an ArrayObject; we will always give
82
        // it a simple array.
83
        if ($structuredOutput instanceof \ArrayObject) {
84
            return $structuredOutput->getArrayCopy();
85
        }
86
87
        return $structuredOutput;
88
    }
89
90
    /**
91
     * Restructure the data as necessary (e.g. to select or reorder fields).
92
     *
93
     * @param mixed $structuredOutput
94
     * @param array $configurationData
95
     * @param array $options
96
     * @return mixed
97
     */
98
    public function restructureData($structuredOutput, $configurationData, $options)
99
    {
100
        if ($structuredOutput instanceof RestructureInterface) {
101
            return $structuredOutput->restructure($configurationData, $options);
102
        }
103
        return $structuredOutput;
104
    }
105
}
106