Passed
Push — main ( e71635...25077e )
by Anatoly
31:13 queued 25:32
created

InvalidObjectException   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 86.05%

Importance

Changes 0
Metric Value
eloc 33
dl 0
loc 106
ccs 37
cts 43
cp 0.8605
rs 10
c 0
b 0
f 0
wmc 9

6 Methods

Rating   Name   Duplication   Size   Complexity  
A uninstantiableObject() 0 5 1
A unsupportedMethodParameterType() 0 9 1
A unsupportedPropertyType() 0 7 1
A unsupportedParameterType() 0 7 2
A unsupportedFunctionParameterType() 0 8 1
A unsupportedType() 0 15 3
1
<?php
2
3
/**
4
 * It's free open-source software released under the MIT License.
5
 *
6
 * @author Anatoly Nekhay <[email protected]>
7
 * @copyright Copyright (c) 2021, Anatoly Nekhay
8
 * @license https://github.com/sunrise-php/hydrator/blob/master/LICENSE
9
 * @link https://github.com/sunrise-php/hydrator
10
 */
11
12
declare(strict_types=1);
13
14
namespace Sunrise\Hydrator\Exception;
15
16
use LogicException;
17
use ReflectionFunctionAbstract;
18
use ReflectionMethod;
19
use ReflectionParameter;
20
use ReflectionProperty;
21
use Sunrise\Hydrator\Type;
22
23
use function sprintf;
24
25
/**
26
 * InvalidObjectException
27
 */
28
class InvalidObjectException extends LogicException implements ExceptionInterface
29
{
30
31
    /**
32
     * @param string $className
33
     *
34
     * @return self
35
     */
36 2
    final public static function uninstantiableObject(string $className): self
37
    {
38 2
        return new self(sprintf(
39 2
            'The uninstantiable class %s cannot be hydrated.',
40 2
            $className,
41 2
        ));
42
    }
43
44
    /**
45
     * @param Type $type
46
     *
47
     * @return self
48
     */
49 3
    final public static function unsupportedType(Type $type): self
50
    {
51
        /** @var mixed $holder */
52 3
        $holder = $type->getHolder();
53
54 3
        if ($holder instanceof ReflectionProperty) {
55 3
            return self::unsupportedPropertyType($type, $holder);
56
        }
57
        if ($holder instanceof ReflectionParameter) {
58
            return self::unsupportedParameterType($type, $holder);
59
        }
60
61
        return new self(sprintf(
62
            'The type {%s} is not supported.',
63
            $type->getName(),
64
        ));
65
    }
66
67
    /**
68
     * @param Type $type
69
     * @param ReflectionProperty $property
70
     *
71
     * @return self
72
     */
73 3
    final public static function unsupportedPropertyType(Type $type, ReflectionProperty $property): self
74
    {
75 3
        return new self(sprintf(
76 3
            'The property {%s::$%s} is associated with an unsupported type {%s}.',
77 3
            $property->getDeclaringClass()->getName(),
78 3
            $property->getName(),
79 3
            $type->getName(),
80 3
        ));
81
    }
82
83
    /**
84
     * @param Type $type
85
     * @param ReflectionParameter $parameter
86
     *
87
     * @return self
88
     */
89 2
    final public static function unsupportedParameterType(Type $type, ReflectionParameter $parameter): self
90
    {
91 2
        $holder = $parameter->getDeclaringFunction();
92
93 2
        return $holder instanceof ReflectionMethod ?
94 1
            self::unsupportedMethodParameterType($type, $parameter, $holder) :
95 2
            self::unsupportedFunctionParameterType($type, $parameter, $holder);
96
    }
97
98
    /**
99
     * @param Type $type
100
     * @param ReflectionParameter $parameter
101
     * @param ReflectionMethod $method
102
     *
103
     * @return self
104
     */
105
    // phpcs:ignore Generic.Files.LineLength
106 1
    final public static function unsupportedMethodParameterType(Type $type, ReflectionParameter $parameter, ReflectionMethod $method): self
107
    {
108 1
        return new self(sprintf(
109 1
            'The parameter {%s::%s($%s[%d])} is associated with an unsupported type {%s}.',
110 1
            $method->getDeclaringClass()->getName(),
111 1
            $method->getName(),
112 1
            $parameter->getName(),
113 1
            $parameter->getPosition(),
114 1
            $type->getName(),
115 1
        ));
116
    }
117
118
    /**
119
     * @param Type $type
120
     * @param ReflectionParameter $parameter
121
     * @param ReflectionFunctionAbstract $function
122
     *
123
     * @return self
124
     */
125
    // phpcs:ignore Generic.Files.LineLength
126 1
    final public static function unsupportedFunctionParameterType(Type $type, ReflectionParameter $parameter, ReflectionFunctionAbstract $function): self
127
    {
128 1
        return new self(sprintf(
129 1
            'The parameter {%s($%s[%d])} is associated with an unsupported type {%s}.',
130 1
            $function->getName(),
131 1
            $parameter->getName(),
132 1
            $parameter->getPosition(),
133 1
            $type->getName(),
134 1
        ));
135
    }
136
}
137