Passed
Push — master ( 8063cf...dfd347 )
by Smoren
02:46
created

ObjectAccessor::getPropertyValue()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 7
ccs 4
cts 4
cp 1
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 2
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Smoren\TypeTools;
6
7
use ReflectionMethod;
8
use ReflectionProperty;
9
use stdClass;
10
11
/**
12
 * Helper for accessing object properties and methods
13
 */
14
class ObjectAccessor
15
{
16
    /**
17
     * @param object $object
18
     * @param string $propertyName
19
     * @return mixed
20
     */
21 10
    public static function getPropertyValue(object $object, string $propertyName)
22
    {
23 10
        if(static::hasPublicProperty($object, $propertyName)) {
24 6
            return $object->{$propertyName};
25
        }
26
27 4
        return static::getPropertyValueByGetter($object, $propertyName);
28
    }
29
30
    /**
31
     * @param object $object
32
     * @param string $propertyName
33
     * @return mixed
34
     */
35 4
    public static function getPropertyValueByGetter(object $object, string $propertyName)
36
    {
37 4
        return $object->{static::getPropertyGetterName($propertyName)}();
38
    }
39
40
    /**
41
     * @param object $object
42
     * @param string $propertyName
43
     * @return bool
44
     */
45 34
    public static function hasAccessibleProperty(object $object, string $propertyName): bool
46
    {
47 34
        return static::hasPublicProperty($object, $propertyName)
48 34
            || static::hasPropertyAccessibleByGetter($object, $propertyName);
49
    }
50
51
    /**
52
     * @param object $object
53
     * @param string $propertyName
54
     * @return bool
55
     */
56 53
    public static function hasPublicProperty(object $object, string $propertyName): bool
57
    {
58 53
        if ($object instanceof stdClass) {
59 26
            return static::hasProperty($object, $propertyName);
60
        }
61
62 27
        return
63 27
            static::hasProperty($object, $propertyName) &&
64 27
            static::getReflectionProperty($object, $propertyName)->isPublic();
65
    }
66
67
    /**
68
     * @param object $object
69
     * @param string $methodName
70
     * @return bool
71
     */
72 43
    public static function hasPublicMethod(object $object, string $methodName): bool
73
    {
74 43
        return
75 43
            static::hasMethod($object, $methodName) &&
76 43
            static::getReflectionMethod($object, $methodName)->isPublic();
77
    }
78
79
    /**
80
     * @param object $object
81
     * @param string $propertyName
82
     * @return bool
83
     */
84 43
    public static function hasPropertyAccessibleByGetter(object $object, string $propertyName): bool
85
    {
86 43
        return static::hasPublicMethod($object, static::getPropertyGetterName($propertyName));
87
    }
88
89
    /**
90
     * @param object $object
91
     * @param string $propertyName
92
     * @return bool
93
     */
94 53
    public static function hasProperty(object $object, string $propertyName): bool
95
    {
96 53
        return property_exists($object, $propertyName);
97
    }
98
99
    /**
100
     * @param object $object
101
     * @param string $methodName
102
     * @return bool
103
     */
104 43
    public static function hasMethod(object $object, string $methodName): bool
105
    {
106 43
        return method_exists($object, $methodName);
107
    }
108
109
    /**
110
     * @param object $object
111
     * @param string $propertyName
112
     * @return ReflectionProperty
113
     */
114 18
    protected static function getReflectionProperty(object $object, string $propertyName): ReflectionProperty
115
    {
116 18
        return new ReflectionProperty(get_class($object), $propertyName);
117
    }
118
119
    /**
120
     * @param object $object
121
     * @param string $methodName
122
     * @return ReflectionMethod
123
     */
124 12
    protected static function getReflectionMethod(object $object, string $methodName): ReflectionMethod
125
    {
126 12
        return new ReflectionMethod(get_class($object), $methodName);
127
    }
128
129
    /**
130
     * @param string $propertyName
131
     * @return string
132
     */
133 43
    protected static function getPropertyGetterName(string $propertyName): string
134
    {
135 43
        return 'get'.ucfirst($propertyName);
136
    }
137
}
138