ProtectedHelper   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 64
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 23
c 1
b 0
f 0
dl 0
loc 64
rs 10
wmc 8

4 Methods

Rating   Name   Duplication   Size   Complexity  
A catchExceptionCode() 0 7 2
A callMethod() 0 7 2
A getProperty() 0 7 2
A setProperty() 0 9 2
1
<?php
2
3
namespace veejay\jsonrpc\tests;
4
5
use Exception;
6
use ReflectionClass;
7
8
final class ProtectedHelper
9
{
10
    /**
11
     * Get protected property.
12
     * @param string|object $object
13
     * @param string $property
14
     * @return mixed|bool
15
     */
16
    public static function getProperty($object, string $property)
17
    {
18
        $reflection = new ReflectionClass($object);
19
        if (!$reflection->hasProperty($property)) return false;
20
        $property = $reflection->getProperty($property);
21
        $property->setAccessible(true);
22
        return $property->getValue($object);
0 ignored issues
show
Bug introduced by
It seems like $object can also be of type string; however, parameter $object of ReflectionProperty::getValue() does only seem to accept null|object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

22
        return $property->getValue(/** @scrutinizer ignore-type */ $object);
Loading history...
23
    }
24
25
    /**
26
     * Set protected property.
27
     * @param string|object $object
28
     * @param string $property
29
     * @param mixed $value
30
     * @return void
31
     */
32
    public static function setProperty($object, string $property, $value)
33
    {
34
        $reflection = new ReflectionClass($object);
35
        $property = $reflection->getProperty($property);
36
        $property->setAccessible(true);
37
        if ($property->isStatic()) {
38
            $property->setValue($value);
39
        } else {
40
            $property->setValue($object, $value);
41
        }
42
    }
43
44
    /**
45
     * Call protected method.
46
     * @param string|object $object
47
     * @param string $methodName
48
     * @param array $params
49
     * @return mixed|bool
50
     */
51
    public static function callMethod($object, string $methodName, array $params = [])
52
    {
53
        $reflection = new ReflectionClass(get_class($object));
0 ignored issues
show
Bug introduced by
It seems like $object can also be of type string; however, parameter $object of get_class() does only seem to accept object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

53
        $reflection = new ReflectionClass(get_class(/** @scrutinizer ignore-type */ $object));
Loading history...
54
        if (!$reflection->hasMethod($methodName)) return false;
55
        $method = $reflection->getMethod($methodName);
56
        $method->setAccessible(true);
57
        return $method->invokeArgs($object, $params);
0 ignored issues
show
Bug introduced by
It seems like $object can also be of type string; however, parameter $object of ReflectionMethod::invokeArgs() does only seem to accept null|object, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

57
        return $method->invokeArgs(/** @scrutinizer ignore-type */ $object, $params);
Loading history...
58
    }
59
60
    /**
61
     * Catch exception and return the code.
62
     * @param callable $callback
63
     * @return int
64
     */
65
    public static function catchExceptionCode(callable $callback): int
66
    {
67
        try {
68
            $callback();
69
            return 0;
70
        } catch (Exception $e) {
71
            return $e->getCode();
72
        }
73
    }
74
}
75