DebugBacktraceConsole   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 4
dl 0
loc 72
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A dump() 0 13 2
A eDump() 0 5 1
A writeBacktraceDump() 0 9 1
A writeCaller() 0 14 2
A getOutput() 0 4 2
1
<?php
2
3
use Symfony\Component\Console\Output\ConsoleOutput;
4
use Symfony\Component\Console\Output\OutputInterface;
5
use Symfony\Component\Console\Helper\Table;
6
7
class DebugBacktraceConsole extends \DebugBacktrace
8
{
9
    /** @var ?string */
10
    protected static $callerRemoveFile = __FILE__;
11
12
    /**
13
     * @param OutputInterface $output
14
     * @param int $offset
15
     * @param int|null $limit
16
     */
17
    public static function dump(OutputInterface $output = null, $offset = 0, $limit = null)
18
    {
19
        $output = static::getOutput($output);
20
21
        static::writeCaller($output);
22
23
        $table = new Table($output);
24
        $table->setHeaders(['#', 'file', 'line', 'call']);
25
        foreach (static::getBacktraces($offset, $limit) as $index => $backtrace) {
26
            static::writeBacktraceDump($table, $backtrace, $index);
27
        }
28
        $table->render();
29
    }
30
31
    /**
32
     * @param OutputInterface $output
33
     * @param int $offset
34
     * @param int|null $limit
35
     */
36
    public static function eDump(OutputInterface $output = null, $offset = 0, $limit = null)
37
    {
38
        static::dump($output, $offset + 1, $limit);
39
        exit();
40
    }
41
42
    /**
43
     * @param Table $table
44
     * @param array $backtrace
45
     * @param int $index
46
     */
47
    protected static function writeBacktraceDump(Table $table, array $backtrace, $index)
48
    {
49
        $table->addRow([
50
            $index + 1,
51
            static::getFilePath($backtrace['file']),
52
            $backtrace['line'],
53
            $backtrace['call']
54
        ]);
55
    }
56
57
    /** @param OutputInterface $output */
58
    protected static function writeCaller(OutputInterface $output)
59
    {
60
        $caller = static::getCaller();
61
62
        if (is_array($caller)) {
63
            $output->writeln(
64
                'From '
65
                . '<info>' . $caller['file'] . '</info>'
66
                . '<comment>#' . $caller['line'] . '</comment>'
67
            );
68
        } else {
69
            $output->writeln('<error>Unkonw caller</error>');
70
        }
71
    }
72
73
    /** @eturn OutputInterface */
74
    protected static function getOutput(OutputInterface $output = null)
75
    {
76
        return $output instanceof OutputInterface ? $output : new ConsoleOutput();
77
    }
78
}
79