Passed
Push — master ( ea46c1...510f1d )
by Thomas
12:51
created

DevUtils::updatePropCb()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
cc 1
eloc 4
c 2
b 1
f 0
nc 1
nop 3
dl 0
loc 6
rs 10
1
<?php
2
3
namespace LeKoala\DevToolkit\Helpers;
4
5
use ReflectionObject;
6
use ReflectionClass;
7
8
class DevUtils
9
{
10
    /**
11
     * @param object $obj
12
     * @param string $prop
13
     * @param mixed $val
14
     * @return void
15
     */
16
    public static function updateProp(object $obj, string $prop, $val): void
17
    {
18
        $refObject = new ReflectionObject($obj);
19
        $refProperty = $refObject->getProperty($prop);
20
        $refProperty->setAccessible(true);
21
        $refProperty->setValue($obj, $val);
22
    }
23
24
    /**
25
     * @param object $obj
26
     * @param string $prop
27
     * @param callable $cb
28
     * @return void
29
     */
30
    public static function updatePropCb(object $obj, string $prop, callable $cb): void
31
    {
32
        $refObject = new ReflectionObject($obj);
33
        $refProperty = $refObject->getProperty($prop);
34
        $refProperty->setAccessible(true);
35
        $refProperty->setValue($obj, $cb($refProperty->getValue($obj)));
36
    }
37
38
    /**
39
     * @param object $obj
40
     * @param string $prop
41
     * @return mixed
42
     */
43
    public static function getProp(object $obj, string $prop)
44
    {
45
        $refObject = new ReflectionObject($obj);
46
        $refProperty = $refObject->getProperty($prop);
47
        $refProperty->setAccessible(true);
48
        return $refProperty->getValue($obj);
49
    }
50
51
    /**
52
     * @param string $class
53
     * @param string $prop
54
     * @return mixed
55
     */
56
    public static function getStaticProp(string $class, string $prop)
57
    {
58
        $refClass = new ReflectionClass($class);
59
        return $refClass->getStaticPropertyValue($prop);
60
    }
61
62
    /**
63
     * @param string $class
64
     * @param string $prop
65
     * @param mixed $val
66
     * @return void
67
     */
68
    public static function updateStaticProp(string $class, string $prop, $val)
69
    {
70
        $refClass = new ReflectionClass($class);
71
        $refClass->setStaticPropertyValue($prop, $val);
72
    }
73
}
74