Passed
Push — master ( a995e6...3e1dc9 )
by Asmir
01:25 queued 49s
created

MethodMetadata   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 103
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 103
ccs 23
cts 23
cp 1
rs 10
c 0
b 0
f 0
wmc 11

9 Methods

Rating   Name   Duplication   Size   Complexity  
A invoke() 0 3 1
A unserialize() 0 3 1
A serialize() 0 3 1
A __construct() 0 4 1
A __serialize() 0 3 1
A __set() 0 3 1
A __get() 0 7 2
A getReflection() 0 8 2
A __unserialize() 0 3 1
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
    /**
76
     * @return array<string>
77
     */
78 4
    public function __serialize(): array
79
    {
80 4
        return [$this->class, $this->name];
81 3
    }
82
83
    /**
84 1
     * @param array<string> $data
85
     */
86
    public function __unserialize(array $data): void
87
    {
88
        [$this->class, $this->name] = $data;
89
    }
90 2
91
    /**
92 2
     * @return mixed
93 2
     */
94
    public function __get(string $propertyName)
95 4
    {
96
        if ('reflection' === $propertyName) {
97 4
            return $this->getReflection();
98 3
        }
99 3
100
        return $this->$propertyName;
101
    }
102 4
103
    /**
104
     * @param mixed $value
105
     */
106
    public function __set(string $propertyName, $value): void
107
    {
108
        $this->$propertyName = $value;
109
    }
110
111
    private function getReflection(): \ReflectionMethod
112
    {
113
        if (null === $this->reflection) {
114
            $this->reflection = new \ReflectionMethod($this->class, $this->name);
115
            $this->reflection->setAccessible(true);
116
        }
117
118
        return $this->reflection;
119
    }
120
}
121