Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
38.89% covered (danger)
38.89%
7 / 18
CRAP
78.50% covered (warning)
78.50%
230 / 293
CliDumper
0.00% covered (danger)
0.00%
0 / 1
38.89% covered (danger)
38.89%
7 / 18
483.92
78.50% covered (warning)
78.50%
230 / 293
 __construct
0.00% covered (danger)
0.00%
0 / 1
5.93
66.67% covered (warning)
66.67%
4 / 6
 setColors
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 setMaxStringWidth
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setStyles
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 setDisplayOptions
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 dumpScalar
100.00% covered (success)
100.00%
1 / 1
13
100.00% covered (success)
100.00%
32 / 32
 dumpString
0.00% covered (danger)
0.00%
0 / 1
28.68
86.79% covered (warning)
86.79%
46 / 53
 enterHash
0.00% covered (danger)
0.00%
0 / 1
22
95.65% covered (success)
95.65%
22 / 23
 leaveHash
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
5 / 5
 dumpEllipsis
100.00% covered (success)
100.00%
1 / 1
4
100.00% covered (success)
100.00%
7 / 7
 dumpKey
0.00% covered (danger)
0.00%
0 / 1
23
98.08% covered (success)
98.08%
51 / 52
 style
0.00% covered (danger)
0.00%
0 / 1
27.14
88.10% covered (warning)
88.10%
37 / 42
 supportsColors
0.00% covered (danger)
0.00%
0 / 1
239.60
8.33% covered (danger)
8.33%
2 / 24
 dumpLine
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 endValue
100.00% covered (success)
100.00%
1 / 1
8
100.00% covered (success)
100.00%
9 / 9
 hasColorSupport
0.00% covered (danger)
0.00%
0 / 1
39.20
38.46% covered (danger)
38.46%
5 / 13
 isWindowsTrueColor
0.00% covered (danger)
0.00%
0 / 1
30
0.00% covered (danger)
0.00%
0 / 12
 getSourceLink
0.00% covered (danger)
0.00%
0 / 1
4.59
66.67% covered (warning)
66.67%
2 / 3
1<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\VarDumper\Dumper;
13
14use Symfony\Component\VarDumper\Cloner\Cursor;
15use Symfony\Component\VarDumper\Cloner\Stub;
16
17/**
18 * CliDumper dumps variables for command line output.
19 *
20 * @author Nicolas Grekas <p@tchwork.com>
21 */
22class CliDumper extends AbstractDumper
23{
24    public static $defaultColors;
25    public static $defaultOutput = 'php://stdout';
26
27    protected $colors;
28    protected $maxStringWidth = 0;
29    protected $styles = [
30        // See http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
31        'default' => '0;38;5;208',
32        'num' => '1;38;5;38',
33        'const' => '1;38;5;208',
34        'str' => '1;38;5;113',
35        'note' => '38;5;38',
36        'ref' => '38;5;247',
37        'public' => '',
38        'protected' => '',
39        'private' => '',
40        'meta' => '38;5;170',
41        'key' => '38;5;113',
42        'index' => '38;5;38',
43    ];
44
45    protected static $controlCharsRx = '/[\x00-\x1F\x7F]+/';
46    protected static $controlCharsMap = [
47        "\t" => '\t',
48        "\n" => '\n',
49        "\v" => '\v',
50        "\f" => '\f',
51        "\r" => '\r',
52        "\033" => '\e',
53    ];
54
55    protected $collapseNextHash = false;
56    protected $expandNextHash = false;
57
58    private $displayOptions = [
59        'fileLinkFormat' => null,
60    ];
61
62    private $handlesHrefGracefully;
63
64    /**
65     * {@inheritdoc}
66     */
67    public function __construct($output = null, string $charset = null, int $flags = 0)
68    {
69        parent::__construct($output, $charset, $flags);
70
71        if ('\\' === \DIRECTORY_SEPARATOR && !$this->isWindowsTrueColor()) {
72            // Use only the base 16 xterm colors when using ANSICON or standard Windows 10 CLI
73            $this->setStyles([
74                'default' => '31',
75                'num' => '1;34',
76                'const' => '1;31',
77                'str' => '1;32',
78                'note' => '34',
79                'ref' => '1;30',
80                'meta' => '35',
81                'key' => '32',
82                'index' => '34',
83            ]);
84        }
85
86        $this->displayOptions['fileLinkFormat'] = ini_get('xdebug.file_link_format') ?: get_cfg_var('xdebug.file_link_format') ?: 'file://%f#L%l';
87    }
88
89    /**
90     * Enables/disables colored output.
91     */
92    public function setColors(bool $colors)
93    {
94        $this->colors = $colors;
95    }
96
97    /**
98     * Sets the maximum number of characters per line for dumped strings.
99     */
100    public function setMaxStringWidth(int $maxStringWidth)
101    {
102        $this->maxStringWidth = $maxStringWidth;
103    }
104
105    /**
106     * Configures styles.
107     *
108     * @param array $styles A map of style names to style definitions
109     */
110    public function setStyles(array $styles)
111    {
112        $this->styles = $styles + $this->styles;
113    }
114
115    /**
116     * Configures display options.
117     *
118     * @param array $displayOptions A map of display options to customize the behavior
119     */
120    public function setDisplayOptions(array $displayOptions)
121    {
122        $this->displayOptions = $displayOptions + $this->displayOptions;
123    }
124
125    /**
126     * {@inheritdoc}
127     */
128    public function dumpScalar(Cursor $cursor, string $type, $value)
129    {
130        $this->dumpKey($cursor);
131
132        $style = 'const';
133        $attr = $cursor->attr;
134
135        switch ($type) {
136            case 'default':
137                $style = 'default';
138                break;
139
140            case 'integer':
141                $style = 'num';
142                break;
143
144            case 'double':
145                $style = 'num';
146
147                switch (true) {
148                    case \INF === $value:  $value = 'INF'; break;
149                    case -\INF === $value: $value = '-INF'; break;
150                    case is_nan($value):  $value = 'NAN'; break;
151                    default:
152                        $value = (string) $value;
153                        if (false === strpos($value, $this->decimalPoint)) {
154                            $value .= $this->decimalPoint.'0';
155                        }
156                        break;
157                }
158                break;
159
160            case 'NULL':
161                $value = 'null';
162                break;
163
164            case 'boolean':
165                $value = $value ? 'true' : 'false';
166                break;
167
168            default:
169                $attr += ['value' => $this->utf8Encode($value)];
170                $value = $this->utf8Encode($type);
171                break;
172        }
173