Passed
Push — master ( 686ef7...d96e2c )
by Smoren
02:15
created

ObjectAccess::hasAccessibleProperty()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 2
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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
 * Tool for reflecting and accessing object properties and methods.
13
 */
14
class ObjectAccess
15
{
16
    /**
17
     * Returns value of the object property.
18
     *
19
     * Can access property by its name or by getter.
20
     *
21
     * @param object $object
22
     * @param string $propertyName
23
     * @return mixed
24
     */
25 10
    public static function getPropertyValue(object $object, string $propertyName)
26
    {
27 10
        if(static::hasPublicProperty($object, $propertyName)) {
28 6
            return $object->{$propertyName};
29
        }
30
31 4
        return static::getPropertyValueByGetter($object, $propertyName);
32
    }
33
34
    /**
35
     * Returns property value by getter.
36
     *
37
     * @param object $object
38
     * @param string $propertyName
39
     *
40
     * @return mixed
41
     */
42 4
    public static function getPropertyValueByGetter(object $object, string $propertyName)
43
    {
44 4
        return $object->{static::getPropertyGetterName($propertyName)}();
45
    }
46
47
    /**
48
     * Returns true if object has property that is accessible by name or by getter.
49
     *
50
     * @param object $object
51
     * @param string $propertyName
52
     *
53
     * @return bool
54
     */
55 34
    public static function hasAccessibleProperty(object $object, string $propertyName): bool
56
    {
57 34
        return static::hasPublicProperty($object, $propertyName)
58 34
            || static::hasPropertyAccessibleByGetter($object, $propertyName);
59
    }
60
61
    /**
62
     * Returns true if object has public property.
63
     *
64
     * @param object $object
65
     * @param string $propertyName
66
     *
67
     * @return bool
68
     */
69 53
    public static function hasPublicProperty(object $object, string $propertyName): bool
70
    {
71 53
        if ($object instanceof stdClass) {
72 26
            return static::hasProperty($object, $propertyName);
73
        }
74
75 27
        return
76 27
            static::hasProperty($object, $propertyName) &&
77 27
            static::getReflectionProperty($object, $propertyName)->isPublic();
78
    }
79
80
    /**
81
     * Returns true if object has property that is accessible by getter.
82
     *
83
     * @param object $object
84
     * @param string $propertyName
85
     *
86
     * @return bool
87
     */
88 43
    public static function hasPropertyAccessibleByGetter(object $object, string $propertyName): bool
89
    {
90 43
        return static::hasPublicMethod($object, static::getPropertyGetterName($propertyName));
91
    }
92
93
    /**
94
     * Returns true if object has property.
95
     *
96
     * @param object $object
97
     * @param string $propertyName
98
     *
99
     * @return bool
100
     */
101 53
    public static function hasProperty(object $object, string $propertyName): bool
102
    {
103 53
        return property_exists($object, $propertyName);
104
    }
105
106
    /**
107
     * Returns true if object has public method.
108
     *
109
     * @param object $object
110
     * @param string $methodName
111
     *
112
     * @return bool
113
     */
114 43
    public static function hasPublicMethod(object $object, string $methodName): bool
115
    {
116 43
        return
117 43
            static::hasMethod($object, $methodName) &&
118 43
            static::getReflectionMethod($object, $methodName)->isPublic();
119
    }
120
121
    /**
122
     * Returns true if object has method.
123
     *
124
     * @param object $object
125
     * @param string $methodName
126
     *
127
     * @return bool
128
     */
129 43
    public static function hasMethod(object $object, string $methodName): bool
130
    {
131 43
        return method_exists($object, $methodName);
132
    }
133
134
    /**
135
     * Returns reflection object of the object property.
136
     *
137
     * @param object $object
138
     * @param string $propertyName
139
     *
140
     * @return ReflectionProperty
141
     */
142 18
    protected static function getReflectionProperty(object $object, string $propertyName): ReflectionProperty
143
    {
144 18
        return new ReflectionProperty(get_class($object), $propertyName);
145
    }
146
147
    /**
148
     * Returns reflection object of the object method.
149
     *
150
     * @param object $object
151
     * @param string $methodName
152
     *
153
     * @return ReflectionMethod
154
     */
155 12
    protected static function getReflectionMethod(object $object, string $methodName): ReflectionMethod
156
    {
157 12
        return new ReflectionMethod(get_class($object), $methodName);
158
    }
159
160
    /**
161
     * Returns property getter name.
162
     *
163
     * @param string $propertyName
164
     *
165
     * @return string
166
     */
167 43
    protected static function getPropertyGetterName(string $propertyName): string
168
    {
169 43
        return 'get'.ucfirst($propertyName);
170
    }
171
}
172