ReflectionHelper::getPropertyValue()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 4
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace RazonYang\UnitHelper;
6
7
use ReflectionClass;
8
9
final class ReflectionHelper
10
{
11 3
    public static function invokeMethod(mixed $obj, string $name, mixed ...$args): mixed
12
    {
13 3
        $class = new ReflectionClass($obj);
14
15 3
        $method = $class->getMethod($name);
16 3
        $method->setAccessible(true);
17
18 3
        return $method->invokeArgs($obj, $args);
19
    }
20
21 4
    public static function getPropertyValue(mixed $obj, string $name): mixed
22
    {
23 4
        $class = new ReflectionClass($obj);
24
25 4
        $property = $class->getProperty($name);
26 4
        $property->setAccessible(true);
27
28 4
        return $property->getValue($obj);
29
    }
30
31 2
    public static function setPropertyValue(mixed $obj, string $name, mixed $value)
32
    {
33 2
        $class = new ReflectionClass($obj);
34
35 2
        $property = $class->getProperty($name);
36 2
        $property->setAccessible(true);
37
38 2
        $property->setValue($obj, $value);
39
    }
40
}
41