Completed
Push — master ( fa856c...54ad29 )
by Greg
04:32
created

VarDumpFormatter   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 25
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 0
Metric Value
wmc 2
c 0
b 0
f 0
lcom 0
cbo 4
dl 0
loc 25
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A write() 0 19 2
1
<?php
2
3
namespace Consolidation\OutputFormatters\Formatters;
4
5
use Consolidation\OutputFormatters\Options\FormatterOptions;
6
use Symfony\Component\Console\Output\OutputInterface;
7
use Symfony\Component\Console\Output\StreamOutput;
8
use Symfony\Component\VarDumper\Cloner\VarCloner;
9
use Symfony\Component\VarDumper\Dumper\CliDumper;
10
11
/**
12
 * Var_dump formatter
13
 *
14
 * Run provided data through Symfony VarDumper component.
15
 */
16
class VarDumpFormatter implements FormatterInterface
17
{
18
    /**
19
     * @inheritdoc
20
     */
21
    public function write(OutputInterface $output, $data, FormatterOptions $options)
22
    {
23
        $dumper = new CliDumper();
24
        $cloned_data = (new VarCloner())->cloneVar($data);
25
26
        if ($output instanceof StreamOutput) {
27
            // When stream output is used the dumper is smart enough to
28
            // determine whether or not to apply colors to the dump.
29
            // @see Symfony\Component\VarDumper\Dumper\CliDumper::supportsColors
30
            $dumper->dump($cloned_data, $output->getStream());
31
        } else {
32
            // @todo Use dumper return value to get output once we stop support
33
            // VarDumper v2.
34
            $stream = fopen('php://memory', 'r+b');
35
            $dumper->dump($cloned_data, $stream);
36
            $output->writeln(stream_get_contents($stream, -1, 0));
37
            fclose($stream);
38
        }
39
    }
40
}
41