Passed
Push — master ( 521784...d2ef52 )
by Asmir
01:24
created

MethodMetadata::__set()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
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
 *
15
 * @property $reflection
16
 */
17
class MethodMetadata implements \Serializable
18
{
19
    /**
20
     * @var string
21
     */
22
    public $class;
23
24
    /**
25
     * @var string
26
     */
27
    public $name;
28
29
    /**
30
     * @var \ReflectionMethod
31
     */
32
    private $reflection;
33
34
    public function __construct(string $class, string $name)
35
    {
36
        $this->class = $class;
37
        $this->name = $name;
38
    }
39
40
    /**
41
     * @param mixed[] $args
42
     *
43
     * @return mixed
44
     */
45
    public function invoke(object $obj, array $args = [])
46
    {
47
        return $this->getReflection()->invokeArgs($obj, $args);
48
    }
49
50
    /**
51
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
52
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
53
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessReturnAnnotation
54
     *
55
     * @return string
56
     */
57
    public function serialize()
58
    {
59
        return serialize([$this->class, $this->name]);
60
    }
61
62
    /**
63
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingParameterTypeHint
64
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.MissingReturnTypeHint
65
     * @phpcsSuppress SlevomatCodingStandard.TypeHints.TypeHintDeclaration.UselessReturnAnnotation
66
     *
67
     * @param string $str
68
     * @return void
69
     */
70
    public function unserialize($str)
71
    {
72
        list($this->class, $this->name) = unserialize($str);
73
    }
74
75
    /**
76
     * @return mixed
77
     */
78
    public function __get(string $propertyName)
79
    {
80
        if ('reflection' === $propertyName) {
81
            return $this->getReflection();
82
        }
83
84
        return $this->$propertyName;
85
    }
86
87
    /**
88
     * @param mixed $value
89
     */
90
    public function __set(string $propertyName, $value): void
91
    {
92
        $this->$propertyName = $value;
93
    }
94
95
    private function getReflection(): \ReflectionMethod
96
    {
97
        if (null === $this->reflection) {
98
            $this->reflection = new \ReflectionMethod($this->class, $this->name);
99
            $this->reflection->setAccessible(true);
100
        }
101
102
        return $this->reflection;
103
    }
104
}
105