Test Setup Failed
Push — test ( dd7d7c...b3915a )
by Jonathan
03:06
created

TextRenderer::parserPlugins()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 15
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

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