Completed
Pull Request — master (#23)
by Greg
03:09
created

StringFormatter::overrideOptions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 0
cts 0
cp 0
rs 9.4285
cc 1
eloc 5
nc 1
nop 2
crap 2
1
<?php
2
namespace Consolidation\OutputFormatters\Formatters;
3
4
use Consolidation\OutputFormatters\FormatterInterface;
5
use Consolidation\OutputFormatters\ValidationInterface;
6
use Consolidation\OutputFormatters\OverrideOptionsInterface;
7
use Consolidation\OutputFormatters\FormatterOptions;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Consolidation\OutputFormatters\StructuredData\RestructureInterface;
10
11
/**
12
 * String formatter
13
 *
14
 * This formatter is used as the default action when no
15
 * particular formatter is requested.  It will print the
16
 * provided data only if it is a string; if any other
17
 * type is given, then nothing is printed.
18
 */
19
class StringFormatter implements FormatterInterface, ValidationInterface, OverrideOptionsInterface
20 1
{
21
    /**
22 1
     * @inheritdoc
23 1
     */
24 1
    public function write(OutputInterface $output, $data, FormatterOptions $options)
25 1
    {
26
        if (is_string($data)) {
27
            return $output->writeln($data);
28
        }
29
        return $this->reduceToSigleFieldAndWrite($output, $data, $options);
30
    }
31
32
    /**
33
     * @inheritdoc
34
     */
35
    public function overrideOptions($structuredOutput, FormatterOptions $options)
36
    {
37
        $defaultField = $options->get(FormatterOptions::SINGLE_FIELD_DEFAULT, [], []);
0 ignored issues
show
Documentation introduced by
array() is of type array, but the function expects a boolean.

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...
38
        return $options->override(
39
            [
40
                FormatterOptions::FIELDS => $defaultField,
41
                FormatterOptions::INCLUDE_FIELD_LABELS => false,
42
            ]
43
        );
44
    }
45
46
    /**
47
     * If the data provided to a 'string' formatter is a table, then try
48
     * to emit it as a TSV value.
49
     *
50
     * @param OutputInterface $output
51
     * @param mixed $data
52
     * @param FormatterOptions $options
53
     */
54
    protected function reduceToSigleFieldAndWrite(OutputInterface $output, $data, FormatterOptions $options)
55
    {
56
        $alternateFormatter = new TsvFormatter();
57
        try {
58
            $data = $alternateFormatter->validate($data);
59
            $alternateFormatter->write($output, $data, $options);
60
        } catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
61
        }
62
    }
63
64
    /**
65
     * Do not return any valid data types -- this formatter will never show up
66
     * in a list of valid formats.
67
     */
68
    public function validDataTypes()
69
    {
70
        return [];
71
    }
72
73
    /**
74
     * Always validate any data, though. This format will never
75
     * cause an error if it is selected for an incompatible data type; at
76
     * worse, it simply does not print any data.
77
     */
78
    public function validate($structuredData)
79
    {
80
        return $structuredData;
81
    }
82
}
83