InvalidTransformationException   A
last analyzed

Complexity

Total Complexity 4

Size/Duplication

Total Lines 42
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 42
ccs 13
cts 13
cp 1
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 16 2
A getWithoutNamespace() 0 10 2
1
<?php
2
3
namespace Tdn\PhpTypes\Exception;
4
5
/**
6
 * Class InvalidTransformationException.
7
 */
8
class InvalidTransformationException extends \RuntimeException
9
{
10
    /**
11
     * @param string          $typeFrom
12
     * @param string          $typeTo
13
     * @param string|null     $message
14
     * @param int             $code
15
     * @param \Exception|null $previous
16
     */
17 46
    public function __construct(
18
        string $typeFrom,
19
        string $typeTo,
20
        string $message = null,
21
        int $code = 0,
22
        \Exception $previous = null
23
    ) {
24 46
        $typeFrom = ($typeFrom === 'double') ? 'float' : $typeFrom;
25 46
        $message = sprintf(
26 46
            $message ?? 'Could not transform %s to %s.',
27 46
            $this->getWithoutNamespace($typeFrom),
28 46
            $this->getWithoutNamespace($typeTo)
29
        );
30
31 46
        parent::__construct($message, $code, $previous);
32 46
    }
33
34
    /**
35
     * @param string $fullyQualified
36
     *
37
     * @return string
38
     */
39 46
    private function getWithoutNamespace(string $fullyQualified): string
40
    {
41 46
        if (class_exists($fullyQualified)) {
42 46
            $c = (new \ReflectionClass($fullyQualified))->getShortName();
43
44 46
            return $c;
45
        }
46
47 46
        return $fullyQualified;
48
    }
49
}
50