GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 7c9800...6d277d )
by Robert
25:06
created

ErrorAction::findException()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 4
nc 2
nop 0
crap 2
1
<?php
2
/**
3
 * @link http://www.yiiframework.com/
4
 * @copyright Copyright (c) 2008 Yii Software LLC
5
 * @license http://www.yiiframework.com/license/
6
 */
7
8
namespace yii\web;
9
10
use Yii;
11
use yii\base\Action;
12
use yii\base\Exception;
13
use yii\base\UserException;
14
15
/**
16
 * ErrorAction displays application errors using a specified view.
17
 *
18
 * To use ErrorAction, you need to do the following steps:
19
 *
20
 * First, declare an action of ErrorAction type in the `actions()` method of your `SiteController`
21
 * class (or whatever controller you prefer), like the following:
22
 *
23
 * ```php
24
 * public function actions()
25
 * {
26
 *     return [
27
 *         'error' => ['class' => 'yii\web\ErrorAction'],
28
 *     ];
29
 * }
30
 * ```
31
 *
32
 * Then, create a view file for this action. If the route of your error action is `site/error`, then
33
 * the view file should be `views/site/error.php`. In this view file, the following variables are available:
34
 *
35
 * - `$name`: the error name
36
 * - `$message`: the error message
37
 * - `$exception`: the exception being handled
38
 *
39
 * Finally, configure the "errorHandler" application component as follows,
40
 *
41
 * ```php
42
 * 'errorHandler' => [
43
 *     'errorAction' => 'site/error',
44
 * ]
45
 * ```
46
 *
47
 * @author Qiang Xue <[email protected]>
48
 * @author Dmitry Naumenko <[email protected]>
49
 * @since 2.0
50
 */
51
class ErrorAction extends Action
52
{
53
    /**
54
     * @var string the view file to be rendered. If not set, it will take the value of [[id]].
55
     * That means, if you name the action as "error" in "SiteController", then the view name
56
     * would be "error", and the corresponding view file would be "views/site/error.php".
57
     */
58
    public $view;
59
    /**
60
     * @var string the name of the error when the exception name cannot be determined.
61
     * Defaults to "Error".
62
     */
63
    public $defaultName;
64
    /**
65
     * @var string the message to be displayed when the exception message contains sensitive information.
66
     * Defaults to "An internal server error occurred.".
67
     */
68
    public $defaultMessage;
69
    /**
70
     * @var \Exception the exception object, normally is filled on [[init()]] method call.
71
     * @see [[findException()]] to know default way of obtaining exception.
72
     * @since 2.0.11
73
     */
74
    protected $exception;
75
76
    /**
77
     * {@inheritdoc}
78
     */
79 7
    public function init()
80
    {
81 7
        $this->exception = $this->findException();
82
83 7
        if ($this->defaultMessage === null) {
84 6
            $this->defaultMessage = Yii::t('yii', 'An internal server error occurred.');
85 6
        }
86
87 7
        if ($this->defaultName === null) {
88 6
            $this->defaultName = Yii::t('yii', 'Error');
89 6
        }
90 7
    }
91
92
    /**
93
     * Runs the action.
94
     *
95
     * @return string result content
96
     */
97 6
    public function run()
98
    {
99 6
        if (Yii::$app->getRequest()->getIsAjax()) {
100 1
            return $this->renderAjaxResponse();
101
        }
102
103 5
        return $this->renderHtmlResponse();
104
    }
105
106
    /**
107
     * Builds string that represents the exception.
108
     * Normally used to generate a response to AJAX request.
109
     * @return string
110
     * @since 2.0.11
111
     */
112 1
    protected function renderAjaxResponse()
113
    {
114 1
        return $this->getExceptionName() . ': ' . $this->getExceptionMessage();
115
    }
116
117
    /**
118
     * Renders a view that represents the exception.
119
     * @return string
120
     * @since 2.0.11
121
     */
122 6
    protected function renderHtmlResponse()
123
    {
124 6
        return $this->controller->render($this->view ?: $this->id, $this->getViewRenderParams());
125
    }
126
127
    /**
128
     * Builds array of parameters that will be passed to the view.
129
     * @return array
130
     * @since 2.0.11
131
     */
132 6
    protected function getViewRenderParams()
133
    {
134
        return [
135 6
            'name' => $this->getExceptionName(),
136 6
            'message' => $this->getExceptionMessage(),
137 6
            'exception' => $this->exception,
138 6
        ];
139
    }
140
141
    /**
142
     * Gets exception from the [[yii\web\ErrorHandler|ErrorHandler]] component.
143
     * In case there is no exception in the component, treat as the action has been invoked
144
     * not from error handler, but by direct route, so '404 Not Found' error will be displayed.
145
     * @return \Exception
146
     * @since 2.0.11
147
     */
148 7
    protected function findException()
149
    {
150 7
        if (($exception = Yii::$app->getErrorHandler()->exception) === null) {
151 3
            $exception = new NotFoundHttpException(Yii::t('yii', 'Page not found.'));
152 3
        }
153
154 7
        return $exception;
155
    }
156
157
    /**
158
     * Gets the code from the [[exception]].
159
     * @return mixed
160
     * @since 2.0.11
161
     */
162 7
    protected function getExceptionCode()
163
    {
164 7
        if ($this->exception instanceof HttpException) {
165 3
            return $this->exception->statusCode;
166
        }
167
168 4
        return $this->exception->getCode();
169
    }
170
171
    /**
172
     * Returns the exception name, followed by the code (if present).
173
     *
174
     * @return string
175
     * @since 2.0.11
176
     */
177 7
    protected function getExceptionName()
178
    {
179 7
        if ($this->exception instanceof Exception) {
180 5
            $name = $this->exception->getName();
181 5
        } else {
182 2
            $name = $this->defaultName;
183
        }
184
185 7
        if ($code = $this->getExceptionCode()) {
186 3
            $name .= " (#$code)";
187 3
        }
188
189 7
        return $name;
190
    }
191
192
    /**
193
     * Returns the [[exception]] message for [[yii\base\UserException]] only.
194
     * For other cases [[defaultMessage]] will be returned.
195
     * @return string
196
     * @since 2.0.11
197
     */
198 7
    protected function getExceptionMessage()
199
    {
200 7
        if ($this->exception instanceof UserException) {
201 4
            return $this->exception->getMessage();
202
        }
203
204 3
        return $this->defaultMessage;
205
    }
206
}
207