Issues (910)

framework/console/ErrorHandler.php (2 issues)

1
<?php
2
/**
3
 * @link https://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license https://www.yiiframework.com/license/
6
 */
7
8
namespace yii\console;
9
10
use Yii;
11
use yii\base\ErrorException;
12
use yii\base\UserException;
13
use yii\helpers\Console;
14
15
/**
16
 * ErrorHandler handles uncaught PHP errors and exceptions.
17
 *
18
 * ErrorHandler is configured as an application component in [[\yii\base\Application]] by default.
19
 * You can access that instance via `Yii::$app->errorHandler`.
20
 *
21
 * @author Carsten Brandt <[email protected]>
22
 * @since 2.0
23
 */
24
class ErrorHandler extends \yii\base\ErrorHandler
25
{
26
    /**
27
     * Renders an exception using ansi format for console output.
28
     * @param \Throwable $exception the exception to be rendered.
29
     */
30
    protected function renderException($exception)
31
    {
32
        $previous = $exception->getPrevious();
33
        if ($exception instanceof UnknownCommandException) {
34
            // display message and suggest alternatives in case of unknown command
35
            $message = $this->formatMessage($exception->getName() . ': ') . $exception->command;
36
            $alternatives = $exception->getSuggestedAlternatives();
37
            if (count($alternatives) === 1) {
38
                $message .= "\n\nDid you mean \"" . reset($alternatives) . '"?';
39
            } elseif (count($alternatives) > 1) {
40
                $message .= "\n\nDid you mean one of these?\n    - " . implode("\n    - ", $alternatives);
41
            }
42
        } elseif ($exception instanceof UserException && ($exception instanceof Exception || !YII_DEBUG)) {
43
            $message = $this->formatMessage($exception->getName() . ': ') . $exception->getMessage();
44
        } elseif (YII_DEBUG) {
45
            if ($exception instanceof Exception) {
46
                $message = $this->formatMessage("Exception ({$exception->getName()})");
47
            } elseif ($exception instanceof ErrorException) {
48
                $message = $this->formatMessage($exception->getName());
49
            } else {
50
                $message = $this->formatMessage('Exception');
51
            }
52
            $message .= $this->formatMessage(" '" . get_class($exception) . "'", [Console::BOLD, Console::FG_BLUE])
53
                . ' with message ' . $this->formatMessage("'{$exception->getMessage()}'", [Console::BOLD]) //. "\n"
54
                . "\n\nin " . dirname($exception->getFile()) . DIRECTORY_SEPARATOR . $this->formatMessage(basename($exception->getFile()), [Console::BOLD])
55
                . ':' . $this->formatMessage($exception->getLine(), [Console::BOLD, Console::FG_YELLOW]) . "\n";
56
            if ($exception instanceof \yii\db\Exception && !empty($exception->errorInfo)) {
57
                $message .= "\n" . $this->formatMessage("Error Info:\n", [Console::BOLD]) . print_r($exception->errorInfo, true);
0 ignored issues
show
Are you sure print_r($exception->errorInfo, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
                $message .= "\n" . $this->formatMessage("Error Info:\n", [Console::BOLD]) . /** @scrutinizer ignore-type */ print_r($exception->errorInfo, true);
Loading history...
58
            }
59
            if ($previous === null) {
60
                $message .= "\n" . $this->formatMessage("Stack trace:\n", [Console::BOLD]) . $exception->getTraceAsString();
61
            }
62
        } else {
63
            $message = $this->formatMessage('Error: ') . $exception->getMessage();
64
        }
65
66
        if (PHP_SAPI === 'cli') {
67
            Console::stderr($message . "\n");
68
        } else {
69
            echo $message . "\n";
70
        }
71
        if (YII_DEBUG && $previous !== null) {
72
            $causedBy = $this->formatMessage('Caused by: ', [Console::BOLD]);
73
            if (PHP_SAPI === 'cli') {
74
                Console::stderr($causedBy);
75
            } else {
76
                echo $causedBy;
77
            }
78
            $this->renderException($previous);
79
        }
80
    }
81
82
    /**
83
     * Colorizes a message for console output.
84
     * @param string $message the message to colorize.
85
     * @param array $format the message format.
86
     * @return string the colorized message.
87
     * @see Console::ansiFormat() for details on how to specify the message format.
88
     */
89
    protected function formatMessage($message, $format = [Console::FG_RED, Console::BOLD])
90
    {
91
        $stream = (PHP_SAPI === 'cli') ? \STDERR : \STDOUT;
92
        // try controller first to allow check for --color switch
93
        if (
94
            Yii::$app->controller instanceof \yii\console\Controller && Yii::$app->controller->isColorEnabled($stream)
0 ignored issues
show
Consider adding parentheses for clarity. Current Interpretation: (Yii::app->controller in...ortsAnsiColors($stream), Probably Intended Meaning: Yii::app->controller ins...rtsAnsiColors($stream))
Loading history...
95
            || Yii::$app instanceof \yii\console\Application && Console::streamSupportsAnsiColors($stream)
96
        ) {
97
            $message = Console::ansiFormat($message, $format);
98
        }
99
100
        return $message;
101
    }
102
}
103