• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tempestphp / tempest-framework / 11874665672

16 Nov 2024 12:30PM UTC coverage: 82.07% (+0.01%) from 82.06%
11874665672

push

github

web-flow
feat(http): add `Put` and `Patch` attributes (#742)

12 of 12 new or added lines in 2 files covered. (100.0%)

22 existing lines in 2 files now uncovered.

7836 of 9548 relevant lines covered (82.07%)

51.94 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

4.17
/src/Tempest/Console/src/Exceptions/ConsoleErrorHandler.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace Tempest\Console\Exceptions;
6

7
use Tempest\Console\Console;
8
use Tempest\Console\ExitCode;
9
use Tempest\Console\HasExitCode;
10
use Tempest\Console\Input\ConsoleArgumentBag;
11
use Tempest\Container\Tag;
12
use Tempest\Core\ErrorHandler;
13
use Tempest\Core\Kernel;
14
use Tempest\Highlight\Escape;
15
use Tempest\Highlight\Highlighter;
16
use Throwable;
17

18
final readonly class ConsoleErrorHandler implements ErrorHandler
19
{
20
    public function __construct(
98✔
21
        private Kernel $kernel,
22
        #[Tag('console')]
23
        private Highlighter $highlighter,
24
        private Console $console,
25
        private ConsoleArgumentBag $argumentBag,
26
    ) {
27
    }
98✔
28

29
    public function handleException(Throwable $throwable): void
×
30
    {
31
        ll(exception: $throwable->getMessage());
×
32

33
        $this->console
×
34
            ->writeln()
×
35
            ->error($throwable::class)
×
36
            ->when(
×
37
                expression: $throwable->getMessage(),
×
38
                callback: fn (Console $console) => $console->error($throwable->getMessage()),
×
39
            )
×
UNCOV
40
            ->writeln($this->getSnippet($throwable->getFile(), $throwable->getLine()))
×
41
            ->writeln()
×
42
            ->writeln('<u>' . $throwable->getFile() . ':' . $throwable->getLine() . '</u>')
×
43
            ->writeln();
×
44

UNCOV
45
        if ($this->argumentBag->get('-v') !== null) {
×
46
            foreach ($throwable->getTrace() as $i => $trace) {
×
UNCOV
47
                $this->console->writeln("<h2>#{$i}</h2> " . $this->formatTrace($trace));
×
48
            }
49

50
            $this->console->writeln();
×
51
        } else {
52
            $firstLine = $throwable->getTrace()[0];
×
53

54
            $this->console
×
UNCOV
55
                ->writeln("<h2>#0</h2> " . $this->formatTrace($firstLine))
×
UNCOV
56
                ->writeln()
×
UNCOV
57
                ->writeln('<em>-v</em> show more')
×
58
                ->writeln();
×
59
        }
60

UNCOV
61
        $exitCode = $throwable instanceof HasExitCode ? $throwable->getExitCode() : ExitCode::ERROR;
×
62

63
        $this->kernel->shutdown($exitCode->value);
×
64
    }
65

UNCOV
66
    public function handleError(int $errNo, string $errstr, string $errFile, int $errLine): void
×
67
    {
68
        ll(error: $errstr);
×
69

70
        $this->console
×
71
            ->writeln()
×
72
            ->error($errstr)
×
UNCOV
73
            ->writeln($this->getSnippet($errFile, $errLine));
×
74
    }
75

76
    private function getSnippet(string $file, int $lineNumber): string
×
77
    {
78
        $highlighter = $this->highlighter->withGutter();
×
UNCOV
79
        $code = Escape::terminal($highlighter->parse(file_get_contents($file), 'php'));
×
80
        $lines = explode(PHP_EOL, $code);
×
81

UNCOV
82
        $lines[$lineNumber - 1] = $lines[$lineNumber - 1] . ' <error><</error>';
×
83

UNCOV
84
        $excerptSize = 5;
×
85
        $start = max(0, $lineNumber - $excerptSize - 2);
×
86
        $lines = array_slice($lines, $start, $excerptSize * 2);
×
87

UNCOV
88
        return PHP_EOL . implode(PHP_EOL, $lines);
×
89
    }
90

UNCOV
91
    private function formatTrace(mixed $trace): string
×
92
    {
93
        if (isset($trace['file'])) {
×
UNCOV
94
            return '<u>' . $trace['file'] . ':' . $trace['line'] . '</u>';
×
95
        }
96

UNCOV
97
        if (isset($trace['class'])) {
×
UNCOV
98
            return $trace['class'] . $trace['type'] . $trace['function'];
×
99
        }
100

UNCOV
101
        return $trace['function'] . '()';
×
102
    }
103
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc