Passed
Push — master ( 9ac357...f303b1 )
by Melech
01:29
created

WhoopsThrowableHandler::getTraceCode()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Valkyrja Framework package.
7
 *
8
 * (c) Melech Mizrachi <[email protected]>
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Valkyrja\Throwable\Handler;
15
16
use Override;
0 ignored issues
show
Bug introduced by
The type Override was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
17
use Valkyrja\Throwable\Handler\Abstract\ThrowableHandler;
18
use Whoops\Handler\JsonResponseHandler;
19
use Whoops\Handler\PrettyPageHandler;
20
use Whoops\Run;
21
use Whoops\Util\Misc;
22
23
use const E_ALL;
24
25
class WhoopsThrowableHandler extends ThrowableHandler
26
{
27
    /**
28
     * Whether debug is enabled or not.
29
     *
30
     * @var bool
31
     */
32
    public static bool $enabled = false;
33
34
    /**
35
     * @inheritDoc
36
     */
37
    #[Override]
38
    public static function enable(int $errorReportingLevel = E_ALL, bool $displayErrors = false): void
39
    {
40
        // If debug is already enabled
41
        if (static::$enabled) {
42
            // Don't do things twice
43
            return;
44
        }
45
46
        // Debug is enabled
47
        static::$enabled = true;
48
49
        $run = new Run();
50
51
        // We want the error page to be shown by default, if this is a
52
        // regular request, so that's the first thing to go into the stack:
53
        $run->pushHandler(new PrettyPageHandler());
54
55
        // Now, we want a second handler that will run before the error page,
56
        // and immediately return an error message in JSON format, if something
57
        // goes awry.
58
        if (Misc::isAjaxRequest()) {
59
            $jsonHandler = new JsonResponseHandler();
60
61
            // You can also tell JsonResponseHandler to give you a full stack trace:
62
            // $jsonHandler->addTraceToOutput(true);
63
64
            // You can also return a result compliant to the json:api spec
65
            // re: http://jsonapi.org/examples/#error-objects
66
            // tl;dr: error[] becomes errors[[]]
67
            $jsonHandler->setJsonApi(true);
68
69
            // And push it into the stack:
70
            $run->pushHandler($jsonHandler);
71
        }
72
73
        // That's it! Register Whoops and throw a dummy exception:
74
        $run->register();
75
    }
76
}
77