MethodMapping::setValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
namespace Maiorano\ObjectHydrator\Mappings;
4
5
use JetBrains\PhpStorm\Pure;
6
use Maiorano\ObjectHydrator\Attributes\HydrationKey;
7
use ReflectionException;
8
use ReflectionMethod;
9
use ReflectionNamedType;
10
11
class MethodMapping implements HydrationMappingInterface
12
{
13
    /**
14
     * @var ReflectionMethod
15
     */
16
    private ReflectionMethod $reflector;
17
    /**
18
     * @var HydrationKey
19
     */
20
    private HydrationKey $key;
21
22
    /**
23
     * @param ReflectionMethod $reflector
24
     * @param HydrationKey     $key
25
     */
26 13
    public function __construct(ReflectionMethod $reflector, HydrationKey $key)
27
    {
28 13
        $this->reflector = $reflector;
29 13
        $this->key = $key;
30 13
    }
31
32
    /**
33
     * @return string
34
     */
35 7
    #[Pure]
36
    public function getKey(): string
37
    {
38 7
        return $this->key->getKey();
39
    }
40
41
    /**
42
     * @return ?ReflectionNamedType
43
     */
44 7
    #[Pure]
45
    public function getType(): ?ReflectionNamedType
46
    {
47 7
        $parameters = $this->reflector->getParameters();
48
49 7
        return $parameters[0]?->getType();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $parameters[0]->getType() could return the type ReflectionType which includes types incompatible with the type-hinted return ReflectionNamedType|null. Consider adding an additional type-check to rule them out.
Loading history...
50
    }
51
52
    /**
53
     * @param object $object
54
     * @param mixed  $value
55
     *
56
     * @throws ReflectionException
57
     *
58
     * @return void
59
     */
60 5
    public function setValue(object $object, mixed $value): void
61
    {
62 5
        $this->reflector->invoke($object, $value);
63 5
    }
64
}
65