Passed
Pull Request — master (#101)
by
unknown
14:05
created

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