Passed
Push — master ( cb7976...2a327d )
by Filipe
13:10 queued 13s
created

PrettyErrorHandler::handle()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 9
rs 10
1
<?php
2
3
/**
4
 * This file is part of template
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
declare(strict_types=1);
11
12
namespace Slick\Template\UserInterface;
13
14
use Slick\ErrorHandler\Exception\ExceptionInspector;
15
use Slick\ErrorHandler\Handler\HandlerInterface;
16
use Slick\ErrorHandler\RunnerInterface;
17
use Slick\Template\Engine\TwigTemplateEngine;
18
use Slick\Template\TemplateEngineInterface;
19
use Throwable;
20
use Twig\Environment;
21
use Twig\Extension\DebugExtension;
22
use Twig\Extra\Markdown\MarkdownExtension;
23
use Twig\Loader\FilesystemLoader;
24
25
/**
26
 * PrettyErrorHandler
27
 *
28
 * @package Slick\Template\UserInterface
29
 */
30
final class PrettyErrorHandler implements HandlerInterface
31
{
32
33
    public function __construct(private readonly TemplateEngineInterface $engine)
34
    {
35
    }
36
37
    public static function create(): static
38
    {
39
        $path = dirname(dirname(__DIR__)) . '/templates';
40
        $loader = new FilesystemLoader([$path]);
41
        $twig = new Environment($loader, ['cache' => false, 'debug' => true]);
42
        $twig->addExtension(new DebugExtension());
43
        $twig->addExtension(new MarkdownExtension());
44
        $templateEngine = new TwigTemplateEngine($twig);
45
46
        return new static($templateEngine);
47
    }
48
49
    public function handle(Throwable $throwable, ExceptionInspector $inspector, RunnerInterface $runner): ?int
50
    {
51
        $handler = $this;
52
        $runner->sendResponseCode($inspector->statusCode());
0 ignored issues
show
Bug introduced by
The method sendResponseCode() does not exist on Slick\ErrorHandler\RunnerInterface. ( Ignorable by Annotation )

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

52
        $runner->/** @scrutinizer ignore-call */ 
53
                 sendResponseCode($inspector->statusCode());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
The method statusCode() does not exist on Slick\ErrorHandler\Exception\ExceptionInspector. ( Ignorable by Annotation )

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

52
        $runner->sendResponseCode($inspector->/** @scrutinizer ignore-call */ statusCode());

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
53
54
        echo $this->engine
55
            ->parse('errors/exception.twig')
56
            ->process(compact('throwable', 'inspector', 'runner', 'handler'));
57
        return self::QUIT;
58
    }
59
60
    public function functionCall(Throwable $throwable, int $key): string
61
    {
62
        $lines = explode("#", $throwable->getTraceAsString());
63
        $line = $lines[$key];
64
        $parts = explode(":", $line);
65
        return trim(end($parts));
66
    }
67
68
    public function version(): string
69
    {
70
        $composerFile = dirname(__DIR__, 2) . '/webstack/composer.json';
71
        $file = is_file($composerFile)
72
            ? $composerFile
73
            : dirname(dirname(__DIR__)) . '/vendor/slick/webstack/composer.json';
74
75
        $composer = json_decode(file_get_contents($file));
76
        return $composer->version;
77
    }
78
}
79