Passed
Push — master ( 3c5f34...4a3ec6 )
by Asmir
01:27 queued 50s
created

MethodMetadata::unserializeFromArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Metadata;
6
7
/**
8
 * Base class for method metadata.
9
 *
10
 * This class is intended to be extended to add your application specific
11
 * properties, and flags.
12
 *
13
 * @author Johannes M. Schmitt <[email protected]>
14
 * @property $reflection
15
 */
16
class MethodMetadata implements \Serializable
17
{
18
    use SerializationHelper;
19
20
    /**
21
     * @var string
22
     */
23
    public $class;
24
25
    /**
26
     * @var string
27
     */
28
    public $name;
29
30
    /**
31
     * @var \ReflectionMethod
32
     */
33 9
    private $reflection;
34
35 9
    public function __construct(string $class, string $name)
36 9
    {
37 9
        $this->class = $class;
38
        $this->name = $name;
39
    }
40
41
    /**
42
     * @param mixed[] $args
43
     *
44 1
     * @return mixed
45
     */
46 1
    public function invoke(object $obj, array $args = [])
47
    {
48
        return $this->getReflection()->invokeArgs($obj, $args);
49
    }
50
51
    /**
52
     * @return mixed
53
     */
54
    public function __get(string $propertyName)
55
    {
56 2
        if ('reflection' === $propertyName) {
57
            return $this->getReflection();
58 2
        }
59
60
        return $this->$propertyName;
61
    }
62
63
    /**
64
     * @param mixed $value
65
     */
66
    public function __set(string $propertyName, $value): void
67
    {
68
        $this->$propertyName = $value;
69
    }
70 2
71
    private function getReflection(): \ReflectionMethod
72 2
    {
73 2
        if (null === $this->reflection) {
74
            $this->reflection = new \ReflectionMethod($this->class, $this->name);
75
            $this->reflection->setAccessible(true);
76
        }
77
78 4
        return $this->reflection;
79
    }
80 4
81 3
    protected function serializeToArray(): array
82
    {
83
        return [$this->class, $this->name];
84 1
    }
85
86
    protected function unserializeFromArray(array $data): void
87
    {
88
        [$this->class, $this->name] = $data;
89
    }
90
}
91