Passed
Push — master ( a9ff5d...1891a0 )
by Melech
01:39 queued 16s
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 Throwable;
18
use Valkyrja\Throwable\Handler\Contract\ThrowableHandlerContract as Contract;
19
use Whoops\Handler\JsonResponseHandler;
20
use Whoops\Handler\PrettyPageHandler;
21
use Whoops\Run;
22
use Whoops\Util\Misc;
23
24
use const E_ALL;
25
26
class WhoopsThrowableHandler implements Contract
27
{
28
    /**
29
     * Whether debug is enabled or not.
30
     *
31
     * @var bool
32
     */
33
    public static bool $enabled = false;
34
35
    /**
36
     * Enable debug mode.
37
     *
38
     * @param int  $errorReportingLevel [optional] The error reporting level
39
     * @param bool $displayErrors       [optional] Whether to display errors
40
     *
41
     * @return void
42
     */
43
    #[Override]
44
    public static function enable(int $errorReportingLevel = E_ALL, bool $displayErrors = false): void
45
    {
46
        // If debug is already enabled
47
        if (static::$enabled) {
48
            // Don't do things twice
49
            return;
50
        }
51
52
        // Debug is enabled
53
        static::$enabled = true;
54
55
        $run = new Run();
56
57
        // We want the error page to be shown by default, if this is a
58
        // regular request, so that's the first thing to go into the stack:
59
        $run->pushHandler(new PrettyPageHandler());
60
61
        // Now, we want a second handler that will run before the error page,
62
        // and immediately return an error message in JSON format, if something
63
        // goes awry.
64
        if (Misc::isAjaxRequest()) {
65
            $jsonHandler = new JsonResponseHandler();
66
67
            // You can also tell JsonResponseHandler to give you a full stack trace:
68
            // $jsonHandler->addTraceToOutput(true);
69
70
            // You can also return a result compliant to the json:api spec
71
            // re: http://jsonapi.org/examples/#error-objects
72
            // tl;dr: error[] becomes errors[[]]
73
            $jsonHandler->setJsonApi(true);
74
75
            // And push it into the stack:
76
            $run->pushHandler($jsonHandler);
77
        }
78
79
        // That's it! Register Whoops and throw a dummy exception:
80
        $run->register();
81
    }
82
83
    /**
84
     * Get trace code for a throwable/exception.
85
     *
86
     * @param Throwable $throwable The exception/throwable
87
     *
88
     * @return string
89
     */
90
    #[Override]
91
    public static function getTraceCode(Throwable $throwable): string
92
    {
93
        return md5($throwable->getTraceAsString());
94
    }
95
}
96