Test Setup Failed
Push — test ( 528f91...abcbcc )
by Jonathan
03:20
created

TextRenderer::calledFrom()   C

Complexity

Conditions 7
Paths 32

Size

Total Lines 31
Code Lines 17

Duplication

Lines 8
Ratio 25.81 %

Importance

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