Failed Conditions
Pull Request — develop (#3131)
by Michael
12:37
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 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