Test Setup Failed
Push — test ( 3bf7a5...16e845 )
by Jonathan
03:34
created

TextRenderer::colorValue()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Kint\Renderer;
4
5
use Kint\Kint;
6
use Kint\Object\BasicObject;
7
use Kint\Object\BlobObject;
8
use Kint\Object\InstanceObject;
9
10
class TextRenderer extends Renderer
0 ignored issues
show
Coding Style introduced by
The property $parser_plugin_whitelist is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $strlen_max is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $default_width is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $default_indent is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $header_width is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $indent_width is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $plugin_objs is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $previous_caller is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
Coding Style introduced by
The property $show_minitrace is not named in camelCase.

This check marks property names that have not been written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes databaseConnectionString.

Loading history...
11
{
12
    /**
13
     * TextRenderer plugins should be instances of Kint\Renderer\Text\Plugin.
14
     */
15
    public static $plugins = array(
16
        'blacklist' => 'Kint\\Renderer\\Text\\BlacklistPlugin',
17
        'depth_limit' => 'Kint\\Renderer\\Text\\DepthLimitPlugin',
18
        'nothing' => 'Kint\\Renderer\\Text\\NothingPlugin',
19
        'recursion' => 'Kint\\Renderer\\Text\\RecursionPlugin',
20
        'trace' => 'Kint\\Renderer\\Text\\TracePlugin',
21
    );
22
23
    /**
24
     * Parser plugins must be instanceof one of these or
25
     * it will be removed for performance reasons.
26
     */
27
    public static $parser_plugin_whitelist = array(
0 ignored issues
show
Coding Style introduced by
$parser_plugin_whitelist does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
Comprehensibility Naming introduced by
The variable name $parser_plugin_whitelist exceeds the maximum configured length of 20.

Very long variable names usually make code harder to read. It is therefore recommended not to make variable names too verbose.

Loading history...
28
        'Kint\\Parser\\BlacklistPlugin',
29
        'Kint\\Parser\\StreamPlugin',
30
        'Kint\\Parser\\TracePlugin',
31
    );
32
33
    /**
34
     * The maximum length of a string before it is truncated.
35
     *
36
     * Falsey to disable
37
     *
38
     * @var int
39
     */
40
    public static $strlen_max = 0;
0 ignored issues
show
Coding Style introduced by
$strlen_max does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
41
42
    /**
43
     * The default width of the terminal for headers.
44
     *
45
     * @var int
46
     */
47
    public static $default_width = 80;
0 ignored issues
show
Coding Style introduced by
$default_width does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
48
49
    /**
50
     * Indentation width.
51
     *
52
     * @var int
53
     */
54
    public static $default_indent = 4;
0 ignored issues
show
Coding Style introduced by
$default_indent does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
55
56
    /**
57
     * Decorate the header and footer.
58
     *
59
     * @var bool
60
     */
61
    public static $decorations = true;
62
63
    /**
64
     * Sort mode for object properties.
65
     *
66
     * @var int
67
     */
68
    public static $sort = self::SORT_NONE;
69
70
    public $header_width = 80;
0 ignored issues
show
Coding Style introduced by
$header_width does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
71
    public $indent_width = 4;
0 ignored issues
show
Coding Style introduced by
$indent_width does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
72
73
    protected $plugin_objs = array();
0 ignored issues
show
Coding Style introduced by
$plugin_objs does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
74
    protected $previous_caller;
0 ignored issues
show
Coding Style introduced by
$previous_caller does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
75
    protected $callee;
76
    protected $show_minitrace = true;
0 ignored issues
show
Coding Style introduced by
$show_minitrace does not seem to conform to the naming convention (^[a-z][a-zA-Z0-9]*$).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
77
78
    public function __construct(array $params = array())
79
    {
80
        parent::__construct($params);
81
82
        $params += array(
83
            'callee' => null,
84
            'caller' => null,
85
        );
86
87
        $this->callee = $params['callee'];
88
        $this->previous_caller = $params['caller'];
89
        $this->show_minitrace = !empty($params['settings']['display_called_from']);
90
        $this->header_width = self::$default_width;
91
        $this->indent_width = self::$default_indent;
92
    }
93
94
    public function render(BasicObject $o)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Comprehensibility introduced by
Avoid variables with short names like $o. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
95
    {
96
        if ($plugin = $this->getPlugin(self::$plugins, $o->hints)) {
97
            if (strlen($output = $plugin->render($o))) {
98
                return $output;
99
            }
100
        }
101
102
        $out = '';
103
104
        if ($o->depth == 0) {
105
            $out .= $this->colorTitle($this->renderTitle($o)).PHP_EOL;
106
        }
107
108
        $out .= $this->renderHeader($o);
109
        $out .= $this->renderChildren($o).PHP_EOL;
110
111
        return $out;
112
    }
113
114
    public function boxText($text, $width)
115
    {
116
        if (BlobObject::strlen($text) > $width - 4) {
117
            $text = BlobObject::substr($text, 0, $width - 7).'...';
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $text. This often makes code more readable.
Loading history...
118
        }
119
120
        $text .= str_repeat(' ', $width - 4 - BlobObject::strlen($text));
121
122
        $out = '┌'.str_repeat('─', $width - 2).'┐'.PHP_EOL;
123
        $out .= '│ '.$this->escape($text).' │'.PHP_EOL;
124
        $out .= '└'.str_repeat('─', $width - 2).'┘';
125
126
        return $out;
127
    }
128
129
    public function renderTitle(BasicObject $o)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
130
    {
131
        if (($name = $o->getName()) === null) {
132
            $name = 'literal';
133
        }
134
135
        if (self::$decorations) {
136
            return $this->boxText($name, $this->header_width);
137
        } elseif (BlobObject::strlen($name) > $this->header_width) {
138
            return BlobObject::substr($name, 0, $this->header_width - 3).'...';
139
        } else {
140
            return $name;
141
        }
142
    }
143
144
    public function renderHeader(BasicObject $o)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
145
    {
146
        $output = array();
147
148
        if ($o->depth) {
149
            if (($s = $o->getModifiers()) !== null) {
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $s. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
150
                $output[] = $s;
151
            }
152
153
            if ($o->name !== null) {
154
                $output[] = $this->escape(var_export($o->name, true));
155
156
                if (($s = $o->getOperator()) !== null) {
157
                    $output[] = $this->escape($s);
158
                }
159
            }
160
        }
161
162
        if (($s = $o->getType()) !== null) {
163
            if ($o->reference) {
164
                $s = '&'.$s;
165
            }
166
167
            $output[] = $this->colorType($this->escape($s));
168
        }
169
170 View Code Duplication
        if (($s = $o->getSize()) !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
171
            $output[] = '('.$this->escape($s).')';
172
        }
173
174 View Code Duplication
        if (($s = $o->getValueShort()) !== null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
175
            if (self::$strlen_max && BlobObject::strlen($s) > self::$strlen_max) {
176
                $s = substr($s, 0, self::$strlen_max).'...';
177
            }
178
            $output[] = $this->colorValue($this->escape($s));
179
        }
180
181
        return str_repeat(' ', $o->depth * $this->indent_width).implode(' ', $output);
182
    }
183
184
    public function renderChildren(BasicObject $o)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $o. Configured minimum length is 3.

Short variable names may make your code harder to understand. Variable names should be self-descriptive. This check looks for variable names who are shorter than a configured minimum.

Loading history...
185
    {
186
        if ($o->type === 'array') {
187
            $output = ' [';
188
        } elseif ($o->type === 'object') {
189
            $output = ' (';
190
        } else {
191
            return '';
192
        }
193
194
        $children = '';
195
196
        if ($o->value && is_array($o->value->contents)) {
197 View Code Duplication
            if ($o instanceof InstanceObject) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
198
                foreach (self::sortContents($o->value->contents, self::$sort) as $obj) {
199
                    $output .= $this->render($obj);
200
                }
201
            } else {
202
                foreach ($o->value->contents as $child) {
203
                    $children .= $this->render($child);
204
                }
205
            }
206
        }
207
208
        if ($children) {
209
            $output .= PHP_EOL.$children;
210
            $output .= str_repeat(' ', $o->depth * $this->indent_width);
211
        }
212
213
        if ($o->type === 'array') {
214
            $output .= ']';
215
        } elseif ($o->type === 'object') {
216
            $output .= ')';
217
        }
218
219
        return $output;
220
    }
221
222
    public function colorValue($string)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
223
    {
224
        return $string;
225
    }
226
227
    public function colorType($string)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
228
    {
229
        return $string;
230
    }
231
232
    public function colorTitle($string)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
233
    {
234
        return $string;
235
    }
236
237
    public function postRender()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
238
    {
239
        if (self::$decorations) {
240
            $output = str_repeat('═', $this->header_width);
241
        } else {
242
            $output = '';
243
        }
244
245
        if (!$this->show_minitrace) {
246
            return $this->colorTitle($output);
247
        } else {
248
            if ($output) {
249
                $output .= PHP_EOL;
250
            }
251
252
            return $this->colorTitle($output.$this->calledFrom().PHP_EOL);
253
        }
254
    }
255
256
    public function parserPlugins(array $plugins)
257
    {
258
        $return = array();
259
260
        foreach ($plugins as $index => $plugin) {
261
            foreach (self::$parser_plugin_whitelist as $whitelist) {
262
                if ($plugin instanceof $whitelist) {
263
                    $return[] = $plugin;
264
                    continue 2;
265
                }
266
            }
267
        }
268
269
        return $return;
270
    }
271
272
    protected function calledFrom()
273
    {
274
        $output = '';
275
276
        if (isset($this->callee['file'])) {
277
            $output .= 'Called from '.$this->ideLink($this->callee['file'], $this->callee['line']);
278
        }
279
280
        $caller = '';
281
282
        if (isset($this->previous_caller['class'])) {
283
            $caller .= $this->previous_caller['class'];
284
        }
285
        if (isset($this->previous_caller['type'])) {
286
            $caller .= $this->previous_caller['type'];
287
        }
288 View Code Duplication
        if (isset($this->previous_caller['function'])
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
289
            && !in_array(
290
                $this->previous_caller['function'],
291
                array('include', 'include_once', 'require', 'require_once')
292
            )
293
        ) {
294
            $caller .= $this->previous_caller['function'].'()';
295
        }
296
297
        if ($caller) {
298
            $output .= ' ['.$caller.']';
299
        }
300
301
        return $output;
302
    }
303
304
    public function ideLink($file, $line)
305
    {
306
        return $this->escape(Kint::shortenPath($file)).':'.$line;
307
    }
308
309 View Code Duplication
    protected function getPlugin(array $plugins, array $hints)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
310
    {
311
        if ($plugins = $this->matchPlugins($plugins, $hints)) {
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $plugins. This often makes code more readable.
Loading history...
312
            $plugin = end($plugins);
313
314
            if (!isset($this->plugin_objs[$plugin])) {
315
                $this->plugin_objs[$plugin] = new $plugin($this);
316
            }
317
318
            return $this->plugin_objs[$plugin];
319
        }
320
    }
321
322
    public function escape($string, $encoding = false)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
323
    {
324
        return $string;
325
    }
326
}
327