Passed
Push — main ( 76dcd0...9f35d8 )
by Anatoly
10:37 queued 04:41
created

InvalidObjectException   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 29
c 0
b 0
f 0
dl 0
loc 97
ccs 38
cts 38
cp 1
rs 10
wmc 8

6 Methods

Rating   Name   Duplication   Size   Complexity  
A uninstantiableObject() 0 5 1
A unsupportedPropertyType() 0 7 1
A unsupportedType() 0 7 2
A unsupportedMethodParameterType() 0 9 1
A unsupportedParameterType() 0 7 2
A unsupportedFunctionParameterType() 0 8 1
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 3
        $holder = $type->getHolder();
52
53 3
        return $holder instanceof ReflectionProperty ?
54 3
            self::unsupportedPropertyType($type, $holder) :
55 3
            self::unsupportedParameterType($type, $holder);
56
    }
57
58
    /**
59
     * @param Type $type
60
     * @param ReflectionProperty $property
61
     *
62
     * @return self
63
     */
64 3
    final public static function unsupportedPropertyType(Type $type, ReflectionProperty $property): self
65
    {
66 3
        return new self(sprintf(
67 3
            'The property {%s::$%s} is associated with an unsupported type {%s}.',
68 3
            $property->getDeclaringClass()->getName(),
69 3
            $property->getName(),
70 3
            $type->getName(),
71 3
        ));
72
    }
73
74
    /**
75
     * @param Type $type
76
     * @param ReflectionParameter $parameter
77
     *
78
     * @return self
79
     */
80 2
    final public static function unsupportedParameterType(Type $type, ReflectionParameter $parameter): self
81
    {
82 2
        $holder = $parameter->getDeclaringFunction();
83
84 2
        return $holder instanceof ReflectionMethod ?
85 1
            self::unsupportedMethodParameterType($type, $parameter, $holder) :
86 2
            self::unsupportedFunctionParameterType($type, $parameter, $holder);
87
    }
88
89
    /**
90
     * @param Type $type
91
     * @param ReflectionParameter $parameter
92
     * @param ReflectionMethod $method
93
     *
94
     * @return self
95
     */
96
    // phpcs:ignore Generic.Files.LineLength
97 1
    final public static function unsupportedMethodParameterType(Type $type, ReflectionParameter $parameter, ReflectionMethod $method): self
98
    {
99 1
        return new self(sprintf(
100 1
            'The parameter {%s::%s($%s[%d])} is associated with an unsupported type {%s}.',
101 1
            $method->getDeclaringClass()->getName(),
102 1
            $method->getName(),
103 1
            $parameter->getName(),
104 1
            $parameter->getPosition(),
105 1
            $type->getName(),
106 1
        ));
107
    }
108
109
    /**
110
     * @param Type $type
111
     * @param ReflectionParameter $parameter
112
     * @param ReflectionFunctionAbstract $function
113
     *
114
     * @return self
115
     */
116
    // phpcs:ignore Generic.Files.LineLength
117 1
    final public static function unsupportedFunctionParameterType(Type $type, ReflectionParameter $parameter, ReflectionFunctionAbstract $function): self
118
    {
119 1
        return new self(sprintf(
120 1
            'The parameter {%s($%s[%d])} is associated with an unsupported type {%s}.',
121 1
            $function->getName(),
122 1
            $parameter->getName(),
123 1
            $parameter->getPosition(),
124 1
            $type->getName(),
125 1
        ));
126
    }
127
}
128