Failed Conditions
Pull Request — develop (#3131)
by Michael
60:38
created

InvalidType::new()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

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