Passed
Push — main ( ed3749...c0dba5 )
by Dimitri
03:31
created

ExceptionManager::setBlacklist()   B

Complexity

Conditions 6
Paths 7

Size

Total Lines 33
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 21
nc 7
nop 2
dl 0
loc 33
rs 8.9617
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
namespace BlitzPHP\Debug;
13
14
use BlitzPHP\Container\Services;
15
use BlitzPHP\View\View;
16
use Symfony\Component\Finder\SplFileInfo;
17
use Throwable;
18
use Whoops\Handler\Handler;
0 ignored issues
show
Bug introduced by
The type Whoops\Handler\Handler 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...
19
use Whoops\Handler\HandlerInterface;
0 ignored issues
show
Bug introduced by
The type Whoops\Handler\HandlerInterface 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...
20
use Whoops\Handler\JsonResponseHandler;
0 ignored issues
show
Bug introduced by
The type Whoops\Handler\JsonResponseHandler 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...
21
use Whoops\Handler\PlainTextHandler;
0 ignored issues
show
Bug introduced by
The type Whoops\Handler\PlainTextHandler 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...
22
use Whoops\Handler\PrettyPageHandler;
0 ignored issues
show
Bug introduced by
The type Whoops\Handler\PrettyPageHandler 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...
23
use Whoops\Inspector\InspectorInterface;
0 ignored issues
show
Bug introduced by
The type Whoops\Inspector\InspectorInterface 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...
24
use Whoops\Run;
0 ignored issues
show
Bug introduced by
The type Whoops\Run 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...
25
use Whoops\RunInterface;
0 ignored issues
show
Bug introduced by
The type Whoops\RunInterface 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...
26
use Whoops\Util\Misc;
0 ignored issues
show
Bug introduced by
The type Whoops\Util\Misc 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...
27
28
/**
29
 * Gestionnaire d'exceptions
30
 */
31
class ExceptionManager
32
{
33
    /**
34
     * Gestionnaire d'exceptions de type http (404, 500) qui peuvent avoir une page d'erreur personnalisée.
35
     */
36
    public static function registerHttpErrors(Run $debugger, array $config): Run
37
    {
38
        return $debugger->pushHandler(static function (Throwable $exception, InspectorInterface $inspector, RunInterface $run) use($config) {
0 ignored issues
show
Unused Code introduced by
The parameter $inspector is not used and could be removed. ( Ignorable by Annotation )

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

38
        return $debugger->pushHandler(static function (Throwable $exception, /** @scrutinizer ignore-unused */ InspectorInterface $inspector, RunInterface $run) use($config) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $run is not used and could be removed. ( Ignorable by Annotation )

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

38
        return $debugger->pushHandler(static function (Throwable $exception, InspectorInterface $inspector, /** @scrutinizer ignore-unused */ RunInterface $run) use($config) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
39
            if (true === $config['log']) {
40
                if (! in_array($exception->getCode(), $config['ignore_codes'], true)) {
41
                    Services::logger()->error($exception);
42
                }
43
            }
44
45
            $files = array_map(fn(SplFileInfo $file) => $file->getFilenameWithoutExtension(), Services::fs()->files($config['error_view_path']));
46
47
            if (in_array((string)$exception->getCode(), $files, true)) {
48
                $view = new View();
49
                $view->setAdapter(config('view.active_adapter', 'native'), ['view_path_locator' => $config['error_view_path']])
0 ignored issues
show
Bug introduced by
config('view.active_adapter', 'native') of type BlitzPHP\Config\Config|null is incompatible with the type string expected by parameter $adapter of BlitzPHP\View\View::setAdapter(). ( Ignorable by Annotation )

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

49
                $view->setAdapter(/** @scrutinizer ignore-type */ config('view.active_adapter', 'native'), ['view_path_locator' => $config['error_view_path']])
Loading history...
50
                    ->display((string)$exception->getCode())
51
                    ->setData(['message' => $exception->getMessage()])
52
                    ->render();
53
                    
54
                return Handler::QUIT;
55
            }     
56
            
57
            return Handler::DONE;
58
        });
59
    }
60
61
    /**
62
     * Gestionnaire d'applications fournis par le developpeur.
63
     */
64
    public static function registerAppHandlers(Run $debugger, array $config): Run
65
    {
66
67
        foreach ($config['handlers'] ?? [] as $handler) {
68
            
69
            if (is_callable($handler)) {
70
                $debugger->pushHandler($handler);
71
            } else if(is_string($handler) && class_exists($handler)) {
72
                $class = Services::container()->make($handler);
73
                if (is_callable($class) || $class instanceof HandlerInterface) {
74
                    $debugger->pushHandler($class);
75
                }
76
            }
77
        }
78
79
        return $debugger;
80
    }
81
82
    /**
83
     * Gestionnaire d'erreurs globales whoops
84
     */
85
    public static function registerWhoopsHandler(Run $debugger, array $config): Run
86
    {
87
        if (Misc::isCommandLine()) {
88
            $debugger->pushHandler(new PlainTextHandler());
89
        }
90
91
        if (! is_online()) {
92
            if (Misc::isAjaxRequest()) {
93
                $debugger->pushHandler(new JsonResponseHandler());
94
            } else {
95
                $handler = new PrettyPageHandler(); 
96
97
                $handler->setEditor($config['editor'] ?: PrettyPageHandler::EDITOR_VSCODE);
98
                $handler->setPageTitle($config['title'] ?: $handler->getPageTitle());
99
                $handler->setApplicationRootPath(APP_PATH);
100
                $handler->setApplicationPaths([APP_PATH, SYST_PATH, VENDOR_PATH]);
101
102
                $handler = self::setBlacklist($handler, $config['blacklist']);
103
104
                foreach ($config['data'] as $label => $data) {
105
                    if (is_array($data)) {
106
                        $handler->addDataTable($label, $data);
107
                    } elseif (is_callable($data)) {
108
                        $handler->addDataTableCallback($label, $data);
109
                    }
110
                }                
111
                
112
                $debugger->pushHandler($handler);
113
            }
114
        }
115
116
        return $debugger;
117
    }
118
119
120
    /**
121
     * Enregistre les elements blacklisté dans l'affichage du rapport d'erreur
122
     */
123
    private static function setBlacklist(PrettyPageHandler $handler, array $blacklists): PrettyPageHandler
124
    {
125
        foreach ($blacklists as $blacklist) {
126
            [$name, $key] = explode('/', $blacklist) + [1 => '*'];
127
128
            if ($name[0] !== '_') {
129
                $name = '_' . $name;
130
            }
131
132
            $name = strtoupper($name);
133
            
134
            if ($key !== '*') {
135
                foreach (explode(',', $key) as $k) {
136
                    $handler->blacklist($name, $k);
137
                }
138
            } else {
139
                $values = match($name) {
140
                    '_GET'     => $_GET,
141
                    '_POST'    => $_POST,
142
                    '_COOKIE'  => $_COOKIE,
143
                    '_SERVER'  => $_SERVER,
144
                    '_ENV'     => $_ENV,
145
                    '_FILES'   => $_FILES ?? [],
146
                    '_SESSION' => $_SESSION ?? [],
147
                    default    => [],
148
                };
149
                foreach ($values as $key => $value) {
150
                    $handler->blacklist($name, $key);
151
                }
152
            }
153
        }
154
155
        return $handler;
156
    }
157
}
158