Completed
Push — master ( b64a8e...2f04be )
by Steevan
01:57 queued 44s
created

DebugBacktraceConsole::getOutput()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 1
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);
0 ignored issues
show
Bug introduced by
It seems like $output can be null; however, getOutput() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
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)
75
    {
76
        return $output instanceof OutputInterface ? $output : new ConsoleOutput();
0 ignored issues
show
Bug introduced by
The class Symfony\Component\Console\Output\OutputInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
77
    }
78
}
79