ExceptionAsString   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 29
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 12
dl 0
loc 29
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 13 1
A getPhpVariables() 0 3 1
A __toString() 0 4 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace BEAR\Package\Provide\Error;
6
7
use BEAR\Sunday\Extension\Router\RouterMatch as Request;
8
use Override;
9
use Stringable;
10
use Throwable;
11
12
use function date;
13
use function print_r;
14
use function sprintf;
15
16
use const DATE_RFC2822;
17
18
final class ExceptionAsString implements Stringable
19
{
20
    private string $string;
21
22
    public function __construct(Throwable $e, Request $request)
23
    {
24
        $eSummery = sprintf(
25
            "%s(%s)\n in file %s on line %s\n\n%s",
26
            $e::class,
27
            $e->getMessage(),
28
            $e->getFile(),
29
            $e->getLine(),
30
            $e->getTraceAsString(),
31
        );
32
33
        /** @var array<string, string> $_SERVER */ //phpcs:ignore SlevomatCodingStandard.Commenting.InlineDocCommentDeclaration.NoAssignment
34
        $this->string = sprintf("%s\n%s\n\n%s\n%s\n\n", date(DATE_RFC2822), (string) $request, $eSummery, $this->getPhpVariables($_SERVER));
35
    }
36
37
    #[Override]
38
    public function __toString(): string
39
    {
40
        return $this->string;
41
    }
42
43
    /** @param array<string, mixed> $server */
44
    private function getPhpVariables(array $server): string
45
    {
46
        return sprintf("\nPHP Variables\n\n\$_SERVER => %s", print_r($server, true)); // @codeCoverageIgnore
0 ignored issues
show
Bug introduced by
It seems like print_r($server, true) can also be of type true; however, parameter $values of sprintf() does only seem to accept double|integer|string, maybe add an additional type check? ( Ignorable by Annotation )

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

46
        return sprintf("\nPHP Variables\n\n\$_SERVER => %s", /** @scrutinizer ignore-type */ print_r($server, true)); // @codeCoverageIgnore
Loading history...
47
    }
48
}
49