Test Setup Failed
Push — test ( b3915a...cbea0e )
by Jonathan
03:08
created

Kint::getSingleCall()   C

Complexity

Conditions 13
Paths 33

Size

Total Lines 58
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 33
nc 33
nop 2
dl 0
loc 58
rs 6.5104
c 0
b 0
f 0

How to fix   Long Method    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;
4
5
use InvalidArgumentException;
6
use Kint\Object\BasicObject;
7
use Kint\Parser\Parser;
8
use Kint\Parser\Plugin;
9
use Kint\Renderer\Renderer;
10
use Kint\Renderer\TextRenderer;
11
12
class Kint
0 ignored issues
show
Coding Style introduced by
The property $enabled_mode 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 $mode_default 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 $mode_default_cli 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 $file_link_format 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 $display_called_from 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 $app_root_dirs 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 $max_depth 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 $cli_detection 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_pool 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...
13
{
14
    /**
15
     * @var mixed Kint mode
16
     *
17
     * false: Disabled
18
     * true: Enabled, default mode selection
19
     * other: Manual mode selection
20
     */
21
    public static $enabled_mode = true;
0 ignored issues
show
Coding Style introduced by
$enabled_mode 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...
22
23
    /**
24
     * Default mode.
25
     *
26
     * @var string
27
     */
28
    public static $mode_default = self::MODE_RICH;
0 ignored issues
show
Coding Style introduced by
$mode_default 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...
29
30
    /**
31
     * Default mode in CLI with cli_detection on.
32
     *
33
     * @var string
34
     */
35
    public static $mode_default_cli = self::MODE_CLI;
0 ignored issues
show
Coding Style introduced by
$mode_default_cli 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...
36
37
    /**
38
     * @var bool Return output instead of echoing
39
     */
40
    public static $return;
41
42
    /**
43
     * @var string format of the link to the source file in trace entries.
44
     *
45
     * Use %f for file path, %l for line number.
46
     *
47
     * [!] EXAMPLE (works with for phpStorm and RemoteCall Plugin):
48
     *
49
     * Kint::$file_link_format = 'http://localhost:8091/?message=%f:%l';
50
     */
51
    public static $file_link_format = '';
0 ignored issues
show
Coding Style introduced by
$file_link_format 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...
52
53
    /**
54
     * @var bool whether to display where kint was called from
55
     */
56
    public static $display_called_from = true;
0 ignored issues
show
Coding Style introduced by
$display_called_from 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
     * @var array base directories of your application that will be displayed instead of the full path.
60
     *
61
     * Keys are paths, values are replacement strings
62
     *
63
     * [!] EXAMPLE (for Laravel 5):
64
     *
65
     * Kint::$app_root_dirs = [
66
     *     base_path() => '<BASE>',
67
     *     app_path() => '<APP>',
68
     *     config_path() => '<CONFIG>',
69
     *     database_path() => '<DATABASE>',
70
     *     public_path() => '<PUBLIC>',
71
     *     resource_path() => '<RESOURCE>',
72
     *     storage_path() => '<STORAGE>',
73
     * ];
74
     *
75
     * Defaults to [$_SERVER['DOCUMENT_ROOT'] => '<ROOT>']
76
     */
77
    public static $app_root_dirs = array();
0 ignored issues
show
Coding Style introduced by
$app_root_dirs 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...
78
79
    /**
80
     * @var int max array/object levels to go deep, if zero no limits are applied
81
     */
82
    public static $max_depth = 6;
0 ignored issues
show
Coding Style introduced by
$max_depth 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...
83
84
    /**
85
     * @var bool expand all trees by default for rich view
86
     */
87
    public static $expanded = false;
88
89
    /**
90
     * @var bool enable detection when Kint is command line.
91
     *
92
     * Formats output with whitespace only; does not HTML-escape it
93
     */
94
    public static $cli_detection = true;
0 ignored issues
show
Coding Style introduced by
$cli_detection 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...
95
96
    /**
97
     * @var array Kint aliases. Add debug functions in Kint wrappers here to fix modifiers and backtraces
98
     */
99
    public static $aliases = array(
100
        array('Kint\\Kint', 'dump'),
101
        array('Kint\\Kint', 'trace'),
102
        array('Kint\\Kint', 'dumpArray'),
103
    );
104
105
    /**
106
     * @var array Kint\Renderer\Renderer descendants. Add to array to extend.
107
     */
108
    public static $renderers = array(
109
        self::MODE_RICH => 'Kint\\Renderer\\RichRenderer',
110
        self::MODE_PLAIN => 'Kint\\Renderer\\PlainRenderer',
111
        self::MODE_TEXT => 'Kint\\Renderer\\TextRenderer',
112
        self::MODE_CLI => 'Kint\\Renderer\\CliRenderer',
113
    );
114
115
    const MODE_RICH = 'r';
116
    const MODE_TEXT = 't';
117
    const MODE_CLI = 'c';
118
    const MODE_PLAIN = 'p';
119
120
    public static $plugins = array(
121
        'Kint\\Parser\\ArrayObjectPlugin',
122
        'Kint\\Parser\\Base64Plugin',
123
        'Kint\\Parser\\BlacklistPlugin',
124
        'Kint\\Parser\\ClassMethodsPlugin',
125
        'Kint\\Parser\\ClassStaticsPlugin',
126
        'Kint\\Parser\\ClosurePlugin',
127
        'Kint\\Parser\\ColorPlugin',
128
        'Kint\\Parser\\DateTimePlugin',
129
        'Kint\\Parser\\FsPathPlugin',
130
        'Kint\\Parser\\IteratorPlugin',
131
        'Kint\\Parser\\JsonPlugin',
132
        'Kint\\Parser\\MicrotimePlugin',
133
        'Kint\\Parser\\SimpleXMLElementPlugin',
134
        'Kint\\Parser\\SplFileInfoPlugin',
135
        'Kint\\Parser\\SplObjectStoragePlugin',
136
        'Kint\\Parser\\StreamPlugin',
137
        'Kint\\Parser\\TablePlugin',
138
        'Kint\\Parser\\ThrowablePlugin',
139
        'Kint\\Parser\\TimestampPlugin',
140
        'Kint\\Parser\\ToStringPlugin',
141
        'Kint\\Parser\\TracePlugin',
142
        'Kint\\Parser\\XmlPlugin',
143
    );
144
145
    protected static $plugin_pool = array();
0 ignored issues
show
Coding Style introduced by
$plugin_pool 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...
146
147
    protected $parser;
148
    protected $renderer;
149
150
    public function __construct(Parser $p, Renderer $r)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. 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...
Comprehensibility introduced by
Avoid variables with short names like $r. 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...
151
    {
152
        $this->parser = $p;
153
        $this->renderer = $r;
154
    }
155
156
    public function setParser(Parser $p)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $p. 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...
157
    {
158
        $this->parser = $p;
159
    }
160
161
    public function getParser()
162
    {
163
        return $this->parser;
164
    }
165
166
    public function setRenderer(Renderer $r)
0 ignored issues
show
Comprehensibility introduced by
Avoid variables with short names like $r. 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...
167
    {
168
        $this->renderer = $r;
169
    }
170
171
    public function getRenderer()
172
    {
173
        return $this->renderer;
174
    }
175
176
    public function setStatesFromStatics(array $statics)
177
    {
178
        $this->renderer->setExpand(!empty($statics['expanded']));
179
        $this->renderer->setReturnMode(!empty($statics['return']));
180
        $this->renderer->setShowTrace(!empty($statics['display_called_from']));
181
182
        $this->parser->setDepthLimit(isset($statics['max_depth']) ? $statics['max_depth'] : false);
183
        $this->parser->clearPlugins();
184
185
        if (!isset($statics['plugins'])) {
186
            return;
187
        }
188
189
        $plugins = array();
190
191
        foreach ($statics['plugins'] as $plugin) {
192
            if ($plugin instanceof Plugin) {
193
                $plugins[] = $plugin;
194
            } elseif (is_string($plugin) && is_subclass_of($plugin, 'Kint\\Parser\\Plugin')) {
195
                if (!isset(self::$plugin_pool[$plugin])) {
196
                    $p = new $plugin();
197
                    self::$plugin_pool[$plugin] = $p;
198
                }
199
                $plugins[] = self::$plugin_pool[$plugin];
200
            }
201
        }
202
203
        $plugins = $this->renderer->filterParserPlugins($plugins);
204
205
        foreach ($plugins as $plugin) {
206
            $this->parser->addPlugin($plugin);
207
        }
208
    }
209
210
    public function setStatesFromCallInfo(array $info)
211
    {
212
        $this->renderer->setCallInfo($info);
213
214
        if (isset($info['modifiers']) && is_array($info['modifiers'])) {
215
            if (in_array('!', $info['modifiers'])) {
216
                $this->renderer->setExpand(true);
217
            }
218
219
            if (in_array('@', $info['modifiers'])) {
220
                $this->renderer->setReturnMode(true);
221
            }
222
223
            if (in_array('+', $info['modifiers'])) {
224
                $this->parser->setDepthLimit(false);
225
            }
226
        }
227
228
        $this->parser->setCallerClass(isset($info['caller']['class']) ? $info['caller']['class'] : null);
229
    }
230
231
    /**
232
     * Renders a list of vars including the pre and post renders.
233
     *
234
     * @param array         $vars Data to dump
235
     * @param BasicObject[] $base Base objects
236
     *
237
     * @return string
238
     */
239
    public function dumpAll(array $vars, array $base)
240
    {
241
        if (array_keys($vars) !== array_keys($base)) {
242
            throw new InvalidArgumentException('Kint::dumpAll requires arrays of identical size and keys as arguments');
243
        }
244
245
        $output = $this->renderer->preRender();
246
247
        if ($vars === array()) {
248
            $output .= $this->renderer->renderNothing();
249
        }
250
251
        foreach ($vars as $key => $arg) {
252
            if (!$base[$key] instanceof BasicObject) {
253
                throw new InvalidArgumentException('Kint::dumpAll requires all elements of the second argument to be BasicObject instances');
254
            }
255
            $output .= $this->dumpVar($arg, $base[$key]);
256
        }
257
258
        $output .= $this->renderer->postRender();
259
260
        return $output;
261
    }
262
263
    /**
264
     * Dumps and renders a var.
265
     *
266
     * @param mixed       $var  Data to dump
267
     * @param BasicObject $base Base object
268
     *
269
     * @return string
270
     */
271
    public function dumpVar(&$var, BasicObject $base)
272
    {
273
        return $this->renderer->render(
274
            $this->parser->parse($var, $base)
275
        );
276
    }
277
278
    /**
279
     * Gets all static settings at once.
280
     *
281
     * @return array Current static settings
282
     */
283
    public static function getStatics()
284
    {
285
        return array(
286
            'aliases' => self::$aliases,
287
            'app_root_dirs' => self::$app_root_dirs,
288
            'cli_detection' => self::$cli_detection,
289
            'display_called_from' => self::$display_called_from,
290
            'enabled_mode' => self::$enabled_mode,
291
            'expanded' => self::$expanded,
292
            'file_link_format' => self::$file_link_format,
293
            'max_depth' => self::$max_depth,
294
            'mode_default' => self::$mode_default,
295
            'mode_default_cli' => self::$mode_default_cli,
296
            'plugins' => self::$plugins,
297
            'renderers' => self::$renderers,
298
            'return' => self::$return,
299
        );
300
    }
301
302
    /**
303
     * Creates a Kint instances based on static settings.
304
     *
305
     * Also calls setStatesFromStatics for you
306
     *
307
     * @param array $statics array of statics as returned by getStatics
308
     *
309
     * @return Kint|null
310
     */
311
    public static function createFromStatics(array $statics)
312
    {
313
        $mode = false;
314
315
        if (isset($statics['enabled_mode'])) {
316
            $mode = $statics['enabled_mode'];
317
318
            if ($statics['enabled_mode'] === true && isset($statics['mode_default'])) {
319
                $mode = $statics['mode_default'];
320
321
                if (PHP_SAPI === 'cli' && !empty($statics['cli_detection']) && isset($statics['mode_default_cli'])) {
322
                    $mode = $statics['mode_default_cli'];
323
                }
324
            }
325
        }
326
327
        if (!$mode) {
328
            return null;
329
        }
330
331
        if (!isset($statics['renderers'][$mode])) {
332
            $renderer = new TextRenderer();
333
        } else {
334
            $renderer = new $statics['renderers'][$mode]();
335
        }
336
337
        $kintstance = new self(new Parser(), $renderer);
338
339
        return $kintstance;
340
    }
341
342
    /**
343
     * Creates base objects given parameter info.
344
     *
345
     * @param array $params Parameters as returned from getCallInfo
346
     * @param int   $argc   Number of arguments the helper was called with
347
     *
348
     * @return BasicObject[] Base objects for the arguments
349
     */
350
    public static function getBasesFromParamInfo(array $params, $argc)
351
    {
352
        static $blacklist = array(
353
            'null',
354
            'true',
355
            'false',
356
            'array(...)',
357
            'array()',
358
            '[...]',
359
            '[]',
360
            '(...)',
361
            '()',
362
            '"..."',
363
            'b"..."',
364
            "'...'",
365
            "b'...'",
366
        );
367
368
        $params = array_values($params);
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $params. This often makes code more readable.
Loading history...
369
        $bases = array();
370
371
        for ($i = 0; $i < $argc; ++$i) {
372
            if (isset($params[$i])) {
373
                $param = $params[$i];
374
            } else {
375
                $param = null;
376
            }
377
378
            if (!isset($param['name']) || is_numeric($param['name'])) {
379
                $name = null;
380
            } elseif (in_array(strtolower($param['name']), $blacklist, true)) {
381
                $name = null;
382
            } else {
383
                $name = $param['name'];
384
            }
385
386
            if (isset($param['path'])) {
387
                $access_path = $param['path'];
0 ignored issues
show
Coding Style introduced by
$access_path 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...
388
389
                if (!empty($param['expression'])) {
390
                    $access_path = '('.$access_path.')';
0 ignored issues
show
Coding Style introduced by
$access_path 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...
391
                }
392
            } else {
393
                $access_path = '$'.$i;
0 ignored issues
show
Coding Style introduced by
$access_path 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...
394
            }
395
396
            $bases[] = BasicObject::blank($name, $access_path);
0 ignored issues
show
Coding Style introduced by
$access_path 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...
397
        }
398
399
        return $bases;
400
    }
401
402
    /**
403
     * Gets call info from the backtrace, alias, and argument count.
404
     *
405
     * Aliases must be normalized beforehand (Utils::normalizeAliases)
406
     *
407
     * @param array $aliases Call aliases as found in Kint::$aliases
408
     * @param array $trace   Backtrace
409
     * @param int   $argc    Number of arguments
410
     *
411
     * @return array[5] Call info
0 ignored issues
show
Documentation introduced by
The doc-type array[5] could not be parsed: Expected "]" at position 2, but found "5". (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
412
     */
413
    public static function getCallInfo(array $aliases, array $trace, $argc)
414
    {
415
        $found = false;
416
        $callee = null;
417
        $caller = null;
418
        $miniTrace = array();
419
420
        foreach ($trace as $index => $frame) {
421
            if (Utils::traceFrameIsListed($frame, $aliases)) {
422
                $found = true;
423
                $miniTrace = array();
424
            }
425
426
            if (!Utils::traceFrameIsListed($frame, array('spl_autoload_call'))) {
427
                $miniTrace[] = $frame;
428
            }
429
        }
430
431
        if ($found) {
432
            $callee = reset($miniTrace) ?: null;
433
            $caller = next($miniTrace) ?: null;
434
        }
435
436
        foreach ($miniTrace as $index => $frame) {
437
            if (!isset($frame['file'], $frame['line'])) {
438
                unset($miniTrace[$index]);
439
            } else {
440
                unset($frame['object'], $frame['args']);
441
                $miniTrace[$index] = $frame;
442
            }
443
        }
444
445
        $miniTrace = array_values($miniTrace);
446
447
        $call = self::getSingleCall($callee ?: array(), $argc);
448
449
        $ret = array(
450
            'params' => null,
451
            'modifiers' => array(),
452
            'callee' => $callee,
453
            'caller' => $caller,
454
            'trace' => $miniTrace,
455
        );
456
457
        if ($call) {
458
            $ret['params'] = $call['parameters'];
459
            $ret['modifiers'] = $call['modifiers'];
460
        }
461
462
        return $ret;
463
    }
464
465
    /**
466
     * Returns specific function call info from a stack trace frame, or null if no match could be found.
467
     *
468
     * @param array $frame The stack trace frame in question
469
     * @param int   $argc  The amount of arguments received
470
     *
471
     * @return array|null params and modifiers, or null if a specific call could not be determined
472
     */
473
    protected static function getSingleCall(array $frame, $argc)
474
    {
475
        if (!isset($frame['file'], $frame['line'], $frame['function']) || !is_readable($frame['file'])) {
476
            return null;
477
        }
478
479
        if (empty($frame['class'])) {
480
            $callfunc = $frame['function'];
481
        } else {
482
            $callfunc = array($frame['class'], $frame['function']);
483
        }
484
485
        $calls = CallFinder::getFunctionCalls(file_get_contents($frame['file']), $frame['line'], $callfunc);
486
487
        $return = null;
488
489
        foreach ($calls as $call) {
490
            $is_unpack = false;
0 ignored issues
show
Coding Style introduced by
$is_unpack 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...
491
492
            // Handle argument unpacking as a last resort
493
            if (KINT_PHP56) {
494
                foreach ($call['parameters'] as $i => &$param) {
495
                    if (strpos($param['name'], '...') === 0) {
496
                        if ($i === count($call['parameters']) - 1) {
497
                            for ($j = 1; $j + $i < $argc; ++$j) {
498
                                $call['parameters'][] = array(
499
                                    'name' => 'array_values('.substr($param['name'], 3).')['.$j.']',
500
                                    'path' => 'array_values('.substr($param['path'], 3).')['.$j.']',
501
                                    'expression' => false,
502
                                );
503
                            }
504
505
                            $param['name'] = 'reset('.substr($param['name'], 3).')';
506
                            $param['path'] = 'reset('.substr($param['path'], 3).')';
507
                            $param['expression'] = false;
508
                        } else {
509
                            $call['parameters'] = array_slice($call['parameters'], 0, $i);
510
                        }
511
512
                        $is_unpack = true;
0 ignored issues
show
Coding Style introduced by
$is_unpack 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...
513
                        break;
514
                    }
515
                }
516
            }
517
518
            if ($is_unpack || count($call['parameters']) === $argc) {
0 ignored issues
show
Coding Style introduced by
$is_unpack 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...
519
                if ($return === null) {
520
                    $return = $call;
521
                } else {
522
                    // If we have multiple calls on the same line with the same amount of arguments,
523
                    // we can't be sure which it is so just return null and let them figure it out
524
                    return null;
525
                }
526
            }
527
        }
528
529
        return $return;
530
    }
531
532
    /**
533
     * Dumps a backtrace.
534
     *
535
     * Functionally equivalent to Kint::dump(1) or Kint::dump(debug_backtrace(true))
536
     *
537
     * @return string|int
538
     */
539
    public static function trace()
540
    {
541
        $statics = self::getStatics();
542
        $kintstance = self::createFromStatics($statics);
543
        if (!$kintstance) {
544
            return 0;
545
        }
546
547
        Utils::normalizeAliases(self::$aliases);
548
549
        $args = func_get_args();
550
551
        $call_info = self::getCallInfo(self::$aliases, debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), count($args));
0 ignored issues
show
Coding Style introduced by
$call_info 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...
552
553
        if (in_array('~', $call_info['modifiers'])) {
0 ignored issues
show
Coding Style introduced by
$call_info 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...
554
            $statics['enabled_mode'] = self::MODE_TEXT;
555
        }
556
        if (in_array('-', $call_info['modifiers'])) {
0 ignored issues
show
Coding Style introduced by
$call_info 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...
557
            while (ob_get_level()) {
558
                ob_end_clean();
559
            }
560
        }
561
562
        $kintstance->setStatesFromStatics($statics);
563
        $kintstance->setStatesFromCallInfo($call_info);
0 ignored issues
show
Coding Style introduced by
$call_info 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...
564
565
        $trimmed_trace = array();
0 ignored issues
show
Coding Style introduced by
$trimmed_trace 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...
566
        $trace = debug_backtrace(true);
567
568
        foreach ($trace as $frame) {
569
            if (Utils::traceFrameIsListed($frame, self::$aliases)) {
570
                $trimmed_trace = array();
0 ignored issues
show
Coding Style introduced by
$trimmed_trace 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...
571
            }
572
573
            $trimmed_trace[] = $frame;
0 ignored issues
show
Coding Style introduced by
$trimmed_trace 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...
574
        }
575
576
        $output = $kintstance->dumpAll(
577
            array($trimmed_trace),
0 ignored issues
show
Coding Style introduced by
$trimmed_trace 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...
578
            array(BasicObject::blank('Kint\\Kint::trace()', 'debug_backtrace(true)'))
579
        );
580
581 View Code Duplication
        if (self::$return || in_array('@', $call_info['modifiers'])) {
0 ignored issues
show
Coding Style introduced by
$call_info 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...
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...
582
            return $output;
583
        } else {
584
            echo $output;
585
586
            return 0;
587
        }
588
    }
589
590
    /**
591
     * Dumps some data.
592
     *
593
     * Functionally equivalent to Kint::dump(1) or Kint::dump(debug_backtrace(true))
594
     *
595
     * @return string|int
596
     */
597
    public static function dump()
598
    {
599
        $statics = self::getStatics();
600
        $kintstance = self::createFromStatics($statics);
601
        if (!$kintstance) {
602
            return 0;
603
        }
604
605
        Utils::normalizeAliases(self::$aliases);
606
607
        $args = func_get_args();
608
609
        $call_info = self::getCallInfo(self::$aliases, debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS), count($args));
0 ignored issues
show
Coding Style introduced by
$call_info 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...
610
611
        if (in_array('~', $call_info['modifiers'])) {
0 ignored issues
show
Coding Style introduced by
$call_info 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...
612
            $statics['enabled_mode'] = self::MODE_TEXT;
613
        }
614
        if (in_array('-', $call_info['modifiers'])) {
0 ignored issues
show
Coding Style introduced by
$call_info 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...
615
            while (ob_get_level()) {
616
                ob_end_clean();
617
            }
618
        }
619
620
        $kintstance->setStatesFromStatics($statics);
621
        $kintstance->setStatesFromCallInfo($call_info);
0 ignored issues
show
Coding Style introduced by
$call_info 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...
622
623
        // If the call is Kint::dump(1) then dump a backtrace instead
624
        if ($args === array(1) && (!isset($call_info['params'][0]['name']) || $call_info['params'][0]['name'] === '1')) {
0 ignored issues
show
Coding Style introduced by
$call_info 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...
625
            $args = debug_backtrace(true);
626
            $trace = array();
627
628
            foreach ($args as $index => $frame) {
629
                if (Utils::traceFrameIsListed($frame, self::$aliases)) {
630
                    $trace = array();
631
                }
632
633
                $trace[] = $frame;
634
            }
635
636
            $tracename = $call_info['callee']['function'].'(1)';
0 ignored issues
show
Coding Style introduced by
$call_info 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...
637
            if (isset($call_info['callee']['class'], $call_info['callee']['type'])) {
0 ignored issues
show
Coding Style introduced by
$call_info 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...
638
                $tracename = $call_info['callee']['class'].$call_info['callee']['type'].$tracename;
0 ignored issues
show
Coding Style introduced by
$call_info 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...
639
            }
640
            $tracebase = BasicObject::blank($tracename, 'debug_backtrace(true)');
641
642
            $output = $kintstance->dumpAll(array($trace), array($tracebase));
643
        } else {
644
            $bases = self::getBasesFromParamInfo(
645
                isset($call_info['params']) ? $call_info['params'] : array(),
0 ignored issues
show
Coding Style introduced by
$call_info 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...
646
                count($args)
647
            );
648
            $output = $kintstance->dumpAll($args, $bases);
649
        }
650
651 View Code Duplication
        if (self::$return || in_array('@', $call_info['modifiers'])) {
0 ignored issues
show
Coding Style introduced by
$call_info 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...
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...
652
            return $output;
653
        } else {
654
            echo $output;
655
656
            return 0;
657
        }
658
    }
659
660
    /**
661
     * generic path display callback, can be configured in app_root_dirs; purpose is
662
     * to show relevant path info and hide as much of the path as possible.
663
     *
664
     * @param string $file
665
     *
666
     * @return string
667
     */
668
    public static function shortenPath($file)
669
    {
670
        $file = array_values(array_filter(explode('/', str_replace('\\', '/', $file)), 'strlen'));
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $file. This often makes code more readable.
Loading history...
671
672
        $longest_match = 0;
0 ignored issues
show
Coding Style introduced by
$longest_match 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...
673
        $match = '/';
674
675
        foreach (self::$app_root_dirs as $path => $alias) {
676
            if (empty($path)) {
677
                continue;
678
            }
679
680
            $path = array_values(array_filter(explode('/', str_replace('\\', '/', $path)), 'strlen'));
681
682
            if (array_slice($file, 0, count($path)) === $path && count($path) > $longest_match) {
0 ignored issues
show
Coding Style introduced by
$longest_match 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...
683
                $longest_match = count($path);
0 ignored issues
show
Coding Style introduced by
$longest_match 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...
684
                $match = $alias;
685
            }
686
        }
687
688
        if ($longest_match) {
0 ignored issues
show
Coding Style introduced by
$longest_match 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...
689
            $file = array_merge(array($match), array_slice($file, $longest_match));
0 ignored issues
show
Coding Style introduced by
$longest_match 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...
Coding Style introduced by
Consider using a different name than the parameter $file. This often makes code more readable.
Loading history...
690
691
            return implode('/', $file);
692
        } else {
693
            // fallback to find common path with Kint dir
694
            $kint = array_values(array_filter(explode('/', str_replace('\\', '/', KINT_DIR)), 'strlen'));
695
696
            foreach ($file as $i => $part) {
697
                if (!isset($kint[$i]) || $kint[$i] !== $part) {
698
                    return ($i ? '.../' : '/').implode('/', array_slice($file, $i));
699
                }
700
            }
701
702
            return '/'.implode('/', $file);
703
        }
704
    }
705
706
    public static function getIdeLink($file, $line)
707
    {
708
        return str_replace(array('%f', '%l'), array($file, $line), self::$file_link_format);
709
    }
710
}
711