Completed
Push — master ( 5c42e9...5ebab4 )
by Greg
02:11
created

FormatterManager::getFormatter()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 13
rs 8.8571
cc 5
eloc 8
nc 6
nop 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
            'default' => '\Consolidation\OutputFormatters\Formatters\DefaultFormatter',
17
            'yaml' => '\Consolidation\OutputFormatters\Formatters\YamlFormatter',
18
            'json' => '\Consolidation\OutputFormatters\Formatters\JsonFormatter',
19
            'print-r' => '\Consolidation\OutputFormatters\Formatters\PrintRFormatter',
20
            'var_export' => '\Consolidation\OutputFormatters\Formatters\VarExportFormatter',
21
            'list' => '\Consolidation\OutputFormatters\Formatters\ListFormatter',
22
            'csv' => '\Consolidation\OutputFormatters\Formatters\CsvFormatter',
23
            'table' => '\Consolidation\OutputFormatters\Formatters\TableFormatter',
24
        ];
25
    }
26
27
    /**
28
     * Format and write output
29
     *
30
     * @param OutputInterface $output Output stream to write to
31
     * @param string $format Data format to output in
32
     * @param mixed $structuredOutput Data to output
33
     * @param array $annotationData Configuration information for formatter
34
     * @param array $options User options
35
     */
36
    public function write(OutputInterface $output, $format, $structuredOutput, $annotationData = [], $options = [])
37
    {
38
        $formatter = $this->getFormatter($format, $annotationData);
39
40
        // Restructure the output data (e.g. select fields to display, etc.).
41
        $structuredOutput = $this->restructureData($structuredOutput, $annotationData, $options);
42
43
        // Make sure that the provided data is in the correct format for the selected formatter.
44
        $structuredOutput = $this->validateData($formatter, $structuredOutput);
0 ignored issues
show
Bug introduced by
It seems like $formatter defined by $this->getFormatter($format, $annotationData) on line 38 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...
45
46
        $formatter->write($output, $structuredOutput, $options);
47
    }
48
49
    /**
50
     * Fetch the requested formatter.
51
     *
52
     * @param string $format Identifier for requested formatter
53
     * @param array $configurationData Configuration data for formatter
54
     * @return FormatterInterface
55
     */
56
    public function getFormatter($format, $configurationData = [])
57
    {
58
        if (empty($format)) {
59
            $format = 'default';
60
        }
61
        if (is_string($format) && array_key_exists($format, $this->formatters)) {
62
            $formatter = new $this->formatters[$format];
63
            if ($formatter instanceof ConfigureInterface) {
64
                $formatter->configure($configurationData);
65
            }
66
            return $formatter;
67
        }
68
    }
69
70
    /**
71
     * Determine if the provided data is compatible with the formatter being used.
72
     *
73
     * @param FormatterInterface $formatter Formatter being used
74
     * @param mixed $structuredOutput Data to validate
75
     * @return mixed
76
     */
77
    public function validateData(FormatterInterface $formatter, $structuredOutput)
78
    {
79
        // If the formatter implements ValidationInterface, then let it
80
        // test the data and throw or return an error
81
        if ($formatter instanceof ValidationInterface) {
82
            return $formatter->validate($structuredOutput);
83
        }
84
        // If the formatter does not implement ValidationInterface, then
85
        // it will never be passed an ArrayObject; we will always give
86
        // it a simple array.
87
        if ($structuredOutput instanceof \ArrayObject) {
88
            return $structuredOutput->getArrayCopy();
89
        }
90
91
        return $structuredOutput;
92
    }
93
94
    /**
95
     * Restructure the data as necessary (e.g. to select or reorder fields).
96
     *
97
     * @param mixed $structuredOutput
98
     * @param array $configurationData
99
     * @param array $options
100
     * @return mixed
101
     */
102
    public function restructureData($structuredOutput, $configurationData, $options)
103
    {
104
        if ($structuredOutput instanceof RestructureInterface) {
105
            return $structuredOutput->restructure($configurationData, $options);
106
        }
107
        return $structuredOutput;
108
    }
109
}
110