Passed
Push — main ( 56c0cb...e79186 )
by Chema
09:06 queued 14s
created

UnsupportedResponseTypeException::fromType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 3
cts 3
cp 1
crap 1
rs 10
c 1
b 0
f 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Gacela\Router\Exceptions;
6
7
use RuntimeException;
8
9
use function get_class;
10
use function gettype;
11
12
final class UnsupportedResponseTypeException extends RuntimeException
13
{
14 4
    public static function fromType(mixed $var): self
15
    {
16 4
        $type = self::inferType($var);
17
18 4
        return new self("Unsupported response type '{$type}'. Must be a string or implement Stringable interface.");
19
    }
20
21 4
    private static function inferType(mixed $var): string
22
    {
23 4
        $type = gettype($var);
24
25 4
        if ($type === 'object') {
26
            /** @var object $var */
27 1
            return get_class($var);
28
        }
29
30 3
        return $type;
31
    }
32
}
33