Failed Conditions
Pull Request — develop (#3131)
by Michael
12:37
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 1344
    public static function new($value, string $toType, array $possibleTypes) : self
22
    {
23 1344
        $actualType = is_object($value) ? get_class($value) : gettype($value);
24
25 1344
        if (is_scalar($value)) {
26 832
            return new self(
27 832
                sprintf(
28 832
                    "Could not convert PHP value '%s' of type '%s' to type '%s'. Expected one of the following types: %s",
29 832
                    $value,
30 832
                    $actualType,
31 832
                    $toType,
32 832
                    implode(', ', $possibleTypes)
33
                )
34
            );
35
        }
36
37 512
        return new self(
38 512
            sprintf(
39 512
                "Could not convert PHP value of type '%s' to type '%s'. Expected one of the following types: %s",
40 512
                $actualType,
41 512
                $toType,
42 512
                implode(', ', $possibleTypes)
43
            )
44
        );
45
    }
46
}
47