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

tempestphp / tempest-framework / 14161923512

30 Mar 2025 01:41PM UTC coverage: 80.964% (+0.2%) from 80.716%
14161923512

push

github

web-flow
ci: prevent coveralls failures from failing tests (#1104)

11058 of 13658 relevant lines covered (80.96%)

100.68 hits per line

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

3.28
/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
use function Tempest\Support\str;
19

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

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

34
        $this->console
×
35
            ->writeln()
×
36
            ->error($throwable::class)
×
37
            ->when(
×
38
                condition: $throwable->getMessage(),
×
39
                callback: fn (Console $console) => $console->error($throwable->getMessage()),
×
40
            )
×
41
            ->writeln()
×
42
            ->writeln('In ' . $this->formatFileWithLine($throwable->getFile() . ':' . $throwable->getLine()))
×
43
            ->writeln($this->getSnippet($throwable->getFile(), $throwable->getLine()))
×
44
            ->writeln();
×
45

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

51
            $this->console->writeln();
×
52
        } else {
53
            $this->console
×
54
                ->writeln('<style="fg-blue bold">#0</style> ' . $this->formatTrace($throwable->getTrace()[0]))
×
55
                ->writeln('<style="fg-blue bold">#1</style> ' . $this->formatTrace($throwable->getTrace()[1]))
×
56
                ->writeln()
×
57
                ->writeln('   <style="dim">Run with -v to show more.</style>')
×
58
                ->writeln();
×
59
        }
60

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

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

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)
×
73
            ->writeln($this->getSnippet($errFile, $errLine));
×
74
    }
75

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

82
        $lines[$lineNumber - 1] = str($lines[$lineNumber - 1])
×
83
            ->replaceRegex('/^\d+/', fn (array $match) => "<style='fg-red'>{$match[0]}</style>")
×
84
            ->append('  <style="fg-red"><<<</style>')
×
85
            ->toString();
×
86

87
        $excerptSize = 5;
×
88
        $start = max(0, ($lineNumber - $excerptSize) - 2);
×
89
        $lines = array_slice($lines, $start, $excerptSize * 2);
×
90

91
        return PHP_EOL . implode(PHP_EOL, $lines);
×
92
    }
93

94
    private function formatFileWithLine(string $file): string
×
95
    {
96
        [$file, $line] = explode(':', $file);
×
97
        $directory = dirname($file);
×
98
        $filename = basename($file);
×
99

100
        return sprintf('<style="fg-gray">%s/</style>%s<style="dim">:</style>%s', $directory, $filename, $line);
×
101
    }
102

103
    private function formatTrace(array $trace): string
×
104
    {
105
        if (isset($trace['file'])) {
×
106
            return $this->formatFileWithLine($trace['file'] . ':' . $trace['line']);
×
107
        }
108

109
        if (isset($trace['class'])) {
×
110
            return sprintf(
×
111
                "%s<style='dim'>%s</style>%s<style='dim'>()</style>",
×
112
                $trace['class'],
×
113
                $trace['type'],
×
114
                $trace['function'],
×
115
            );
×
116
        }
117

118
        return sprintf("%s<style='dim'>()</style>", $trace['function']);
×
119
    }
120
}
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

© 2025 Coveralls, Inc