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

InvalidType::new()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 3

Importance

Changes 0
Metric Value
dl 0
loc 22
ccs 16
cts 16
cp 1
rs 9.2
c 0
b 0
f 0
cc 3
eloc 16
nc 4
nop 3
crap 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