Test Setup Failed
Push — next ( 738e04...b06172 )
by Jonathan
25:05
created

TextRenderer::renderChildren()   C

Complexity

Conditions 12
Paths 37

Size

Total Lines 37
Code Lines 23

Duplication

Lines 9
Ratio 24.32 %

Importance

Changes 0
Metric Value
cc 12
eloc 23
nc 37
nop 1
dl 9
loc 37
rs 5.1612
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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
        'microtime' => 'Kint\\Renderer\\Text\\MicrotimePlugin',
19
        'nothing' => 'Kint\\Renderer\\Text\\NothingPlugin',
20
        'recursion' => 'Kint\\Renderer\\Text\\RecursionPlugin',
21
        'trace' => 'Kint\\Renderer\\Text\\TracePlugin',
22
    );
23
24
    /**
25
     * Parser plugins must be instanceof one of these or
26
     * it will be removed for performance reasons.
27
     */
28
    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...
29
        'Kint\\Parser\\BlacklistPlugin',
30
        'Kint\\Parser\\MicrotimePlugin',
31
        'Kint\\Parser\\StreamPlugin',
32
        'Kint\\Parser\\TracePlugin',
33
    );
34
35
    /**
36
     * The maximum length of a string before it is truncated.
37
     *
38
     * Falsey to disable
39
     *
40
     * @var int
41
     */
42
    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...
43
44
    /**
45
     * The default width of the terminal for headers.
46
     *
47
     * @var int
48
     */
49
    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...
50
51
    /**
52
     * Indentation width.
53
     *
54
     * @var int
55
     */
56
    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...
57
58
    /**
59
     * Decorate the header and footer.
60
     *
61
     * @var bool
62
     */
63
    public static $decorations = true;
64
65
    /**
66
     * Sort mode for object properties.
67
     *
68
     * @var int
69
     */
70
    public static $sort = self::SORT_NONE;
71
72
    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...
73
    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...
74
75
    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...
76
    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...
77
    protected $callee;
78
    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...
79
80
    public function __construct(array $params = array())
81
    {
82
        parent::__construct($params);
83
84
        $params += array(
85
            'callee' => null,
86
            'caller' => null,
87
        );
88
89
        $this->callee = $params['callee'];
90
        $this->previous_caller = $params['caller'];
91
        $this->show_minitrace = !empty($params['settings']['display_called_from']);
92
        $this->header_width = self::$default_width;
93
        $this->indent_width = self::$default_indent;
94
    }
95
96
    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...
97
    {
98
        if ($plugin = $this->getPlugin(self::$plugins, $o->hints)) {
99
            if (strlen($output = $plugin->render($o))) {
100
                return $output;
101
            }
102
        }
103
104
        $out = '';
105
106
        if ($o->depth == 0) {
107
            $out .= $this->colorTitle($this->renderTitle($o)).PHP_EOL;
108
        }
109
110
        $out .= $this->renderHeader($o);
111
        $out .= $this->renderChildren($o).PHP_EOL;
112
113
        return $out;
114
    }
115
116
    public function boxText($text, $width)
117
    {
118
        if (BlobObject::strlen($text) > $width - 4) {
119
            $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...
120
        }
121
122
        $text .= str_repeat(' ', $width - 4 - BlobObject::strlen($text));
123
124
        $out = '┌'.str_repeat('─', $width - 2).'┐'.PHP_EOL;
125
        $out .= '│ '.$this->escape($text).' │'.PHP_EOL;
126
        $out .= '└'.str_repeat('─', $width - 2).'┘';
127
128
        return $out;
129
    }
130
131
    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...
132
    {
133
        if (($name = $o->getName()) === null) {
134
            $name = 'literal';
135
        }
136
137
        if (self::$decorations) {
138
            return $this->boxText($name, $this->header_width);
139
        } elseif (BlobObject::strlen($name) > $this->header_width) {
140
            return BlobObject::substr($name, 0, $this->header_width - 3).'...';
141
        } else {
142
            return $name;
143
        }
144
    }
145
146
    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...
147
    {
148
        $output = array();
149
150
        if ($o->depth) {
151
            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...
152
                $output[] = $s;
153
            }
154
155
            if ($o->name !== null) {
156
                $output[] = $this->escape(var_export($o->name, true));
157
158
                if (($s = $o->getOperator()) !== null) {
159
                    $output[] = $this->escape($s);
160
                }
161
            }
162
        }
163
164
        if (($s = $o->getType()) !== null) {
165
            if ($o->reference) {
166
                $s = '&'.$s;
167
            }
168
169
            $output[] = $this->colorType($this->escape($s));
170
        }
171
172 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...
173
            $output[] = '('.$this->escape($s).')';
174
        }
175
176 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...
177
            if (self::$strlen_max && BlobObject::strlen($s) > self::$strlen_max) {
178
                $s = substr($s, 0, self::$strlen_max).'...';
179
            }
180
            $output[] = $this->colorValue($this->escape($s));
181
        }
182
183
        return str_repeat(' ', $o->depth * $this->indent_width).implode(' ', $output);
184
    }
185
186
    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...
187
    {
188
        if ($o->type === 'array') {
189
            $output = ' [';
190
        } elseif ($o->type === 'object') {
191
            $output = ' (';
192
        } else {
193
            return '';
194
        }
195
196
        $children = '';
197
198
        if ($o->value && is_array($o->value->contents)) {
199 View Code Duplication
            if ($o instanceof InstanceObject && $o->value->getName() === 'properties') {
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...
200
                foreach (self::sortProperties($o->value->contents, self::$sort) as $obj) {
201
                    $children .= $this->render($obj);
202
                }
203
            } else {
204
                foreach ($o->value->contents as $child) {
205
                    $children .= $this->render($child);
206
                }
207
            }
208
        }
209
210
        if ($children) {
211
            $output .= PHP_EOL.$children;
212
            $output .= str_repeat(' ', $o->depth * $this->indent_width);
213
        }
214
215
        if ($o->type === 'array') {
216
            $output .= ']';
217
        } elseif ($o->type === 'object') {
218
            $output .= ')';
219
        }
220
221
        return $output;
222
    }
223
224
    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...
225
    {
226
        return $string;
227
    }
228
229
    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...
230
    {
231
        return $string;
232
    }
233
234
    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...
235
    {
236
        return $string;
237
    }
238
239
    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...
240
    {
241
        if (self::$decorations) {
242
            $output = str_repeat('═', $this->header_width);
243
        } else {
244
            $output = '';
245
        }
246
247
        if (!$this->show_minitrace) {
248
            return $this->colorTitle($output);
249
        } else {
250
            if ($output) {
251
                $output .= PHP_EOL;
252
            }
253
254
            return $this->colorTitle($output.$this->calledFrom().PHP_EOL);
255
        }
256
    }
257
258
    public function parserPlugins(array $plugins)
259
    {
260
        $return = array();
261
262
        foreach ($plugins as $index => $plugin) {
263
            foreach (self::$parser_plugin_whitelist as $whitelist) {
264
                if ($plugin instanceof $whitelist) {
265
                    $return[] = $plugin;
266
                    continue 2;
267
                }
268
            }
269
        }
270
271
        return $return;
272
    }
273
274
    protected function calledFrom()
275
    {
276
        $output = '';
277
278
        if (isset($this->callee['file'])) {
279
            $output .= 'Called from '.$this->ideLink($this->callee['file'], $this->callee['line']);
280
        }
281
282
        $caller = '';
283
284
        if (isset($this->previous_caller['class'])) {
285
            $caller .= $this->previous_caller['class'];
286
        }
287
        if (isset($this->previous_caller['type'])) {
288
            $caller .= $this->previous_caller['type'];
289
        }
290 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...
291
            && !in_array(
292
                $this->previous_caller['function'],
293
                array('include', 'include_once', 'require', 'require_once')
294
            )
295
        ) {
296
            $caller .= $this->previous_caller['function'].'()';
297
        }
298
299
        if ($caller) {
300
            $output .= ' ['.$caller.']';
301
        }
302
303
        return $output;
304
    }
305
306
    public function ideLink($file, $line)
307
    {
308
        return $this->escape(Kint::shortenPath($file)).':'.$line;
309
    }
310
311 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...
312
    {
313
        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...
314
            $plugin = end($plugins);
315
316
            if (!isset($this->plugin_objs[$plugin])) {
317
                $this->plugin_objs[$plugin] = new $plugin($this);
318
            }
319
320
            return $this->plugin_objs[$plugin];
321
        }
322
    }
323
324
    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...
325
    {
326
        return $string;
327
    }
328
}
329