1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the Yep package. |
5
|
|
|
* Copyright (c) 2018 Martin Zeman (Zemistr) (http://www.zemistr.eu) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace Yep\Reflection; |
11
|
|
|
|
12
|
|
|
|
13
|
|
|
final class ReflectionClass extends \ReflectionClass |
14
|
|
|
{ |
15
|
|
|
|
16
|
|
|
/** @var object */ |
17
|
|
|
protected $object; |
18
|
|
|
|
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param object|mixed $class |
22
|
|
|
* @param object|mixed|null $object |
23
|
|
|
* @throws \ReflectionException |
24
|
|
|
*/ |
25
|
11 |
|
public function __construct($class, $object = null) |
26
|
|
|
{ |
27
|
11 |
|
if (is_object($class) && !is_object($object)) { |
28
|
7 |
|
$object = $class; |
29
|
|
|
} |
30
|
11 |
|
if (!is_object($object)) { |
31
|
4 |
|
throw new \InvalidArgumentException('Expected "object", got "' . gettype($object) . '".'); |
32
|
|
|
} |
33
|
|
|
|
34
|
7 |
|
parent::__construct($class); |
35
|
7 |
|
$this->object = $object; |
36
|
7 |
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param object|mixed $class |
41
|
|
|
* @param object|mixed|null $object |
42
|
|
|
* @return self |
|
|
|
|
43
|
|
|
* @throws \ReflectionException |
44
|
|
|
*/ |
45
|
8 |
|
public static function from($class, $object = null): self |
46
|
|
|
{ |
47
|
8 |
|
return new self($class, $object); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
1 |
|
public function getObject() |
52
|
|
|
{ |
53
|
1 |
|
return $this->object; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param string $method |
59
|
|
|
* @param mixed[] $arguments |
60
|
|
|
* @return mixed |
61
|
|
|
*/ |
62
|
1 |
|
public function invokeMethod(string $method, array $arguments = []) |
63
|
|
|
{ |
64
|
|
|
try { |
65
|
1 |
|
$reflection = $this->getMethod($method); |
66
|
|
|
} catch (\ReflectionException $e) { |
67
|
|
|
throw new \InvalidArgumentException('Reflection: Method "' . $method . '" does not exist: ' . $e->getMessage(), $e->getCode(), $e); |
68
|
|
|
} |
69
|
1 |
|
$reflection->setAccessible(true); |
70
|
|
|
|
71
|
1 |
|
return $reflection->invokeArgs($this->object, $arguments); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param mixed $value |
77
|
|
|
* @return self |
78
|
|
|
*/ |
79
|
1 |
|
public function setPropertyValue(string $property, $value): self |
80
|
|
|
{ |
81
|
|
|
try { |
82
|
1 |
|
$reflection = $this->getProperty($property); |
83
|
|
|
} catch (\ReflectionException $e) { |
84
|
|
|
throw new \InvalidArgumentException('Reflection: Property "' . $property . '" does not exist: ' . $e->getMessage(), $e->getCode(), $e); |
85
|
|
|
} |
86
|
1 |
|
$reflection->setAccessible(true); |
87
|
1 |
|
$reflection->setValue($this->object, $value); |
88
|
|
|
|
89
|
1 |
|
return $this; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return mixed |
95
|
|
|
*/ |
96
|
2 |
|
public function getPropertyValue(string $property) |
97
|
|
|
{ |
98
|
|
|
try { |
99
|
2 |
|
$reflection = $this->getProperty($property); |
100
|
|
|
} catch (\ReflectionException $e) { |
101
|
|
|
throw new \InvalidArgumentException('Reflection: Property "' . $property . '" does not exist: ' . $e->getMessage(), $e->getCode(), $e); |
102
|
|
|
} |
103
|
2 |
|
$reflection->setAccessible(true); |
104
|
|
|
|
105
|
2 |
|
return $reflection->getValue($this->object); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return self |
|
|
|
|
111
|
|
|
* @throws \ReflectionException |
112
|
|
|
*/ |
113
|
1 |
|
public function getParent(): self |
114
|
|
|
{ |
115
|
1 |
|
return self::from($this->getParentClass()->getName(), $this->object); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check compares the return type specified in the
@return
annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.