Failed Conditions
Push — exceptions ( 64d080...7fc4df )
by Michael
15:07
created

InvalidType   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 31
ccs 16
cts 16
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A new() 0 22 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\DBAL\Types\Exception;
6
7
use Doctrine\DBAL\Types\ConversionException;
8
9
/**
10
 * Thrown when the PHP value passed to the converter was not of the expected type.
11
 */
12
final class InvalidType extends ConversionException implements TypesException
13
{
14
    /**
15
     * @param mixed    $value
16
     * @param string[] $possibleTypes
17
     *
18
     * @todo split into two methods
19
     * @todo sanitize value
20
     */
21 1260
    public static function new($value, string $toType, array $possibleTypes) : self
22
    {
23 1260
        $actualType = is_object($value) ? get_class($value) : gettype($value);
24
25 1260
        if (is_scalar($value)) {
26 780
            return new self(
27 780
                sprintf(
28 780
                    "Could not convert PHP value '%s' of type '%s' to type '%s'. Expected one of the following types: %s",
29 780
                    $value,
30 780
                    $actualType,
31 780
                    $toType,
32 780
                    implode(', ', $possibleTypes)
33
                )
34
            );
35
        }
36
37 480
        return new self(
38 480
            sprintf(
39 480
                "Could not convert PHP value of type '%s' to type '%s'. Expected one of the following types: %s",
40 480
                $actualType,
41 480
                $toType,
42 480
                implode(', ', $possibleTypes)
43
            )
44
        );
45
    }
46
}
47