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

DevUtils   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 4
Bugs 1 Features 0
Metric Value
eloc 17
c 4
b 1
f 0
dl 0
loc 64
rs 10
wmc 5

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getProp() 0 6 1
A updatePropCb() 0 6 1
A updateStaticProp() 0 4 1
A updateProp() 0 6 1
A getStaticProp() 0 4 1
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