Passed
Push — master ( e65e67...9d11b9 )
by Maxim
07:05 queued 10s
created

ObjectFunctions   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 27
Duplicated Lines 0 %

Test Coverage

Coverage 90%

Importance

Changes 2
Bugs 0 Features 0
Metric Value
wmc 6
eloc 10
c 2
b 0
f 0
dl 0
loc 27
ccs 9
cts 10
cp 0.9
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getPropertyValue() 0 19 6
1
<?php
2
/**
3
 * @author Maxim Sokolovsky
4
 */
5
6
namespace WS\Utils\Collections\Functions;
7
8
use ReflectionProperty;
9
use RuntimeException;
10
11
class ObjectFunctions
12
{
13
    /**
14
     * @param $object
15
     * @param string $fieldName
16
     * @return mixed
17
     * @noinspection PhpDocMissingThrowsInspection
18
     */
19 12
    public static function getPropertyValue($object, string $fieldName)
20
    {
21
        /** @noinspection PhpUnhandledExceptionInspection */
22 12
        if (property_exists($object, $fieldName) && (new ReflectionProperty($object, $fieldName))->isPublic()) {
23 3
            return $object->{$fieldName};
24
        }
25
26 10
        if (isset($object->{$fieldName})) {
27
            return $object->{$fieldName};
28
        }
29
30 10
        if (method_exists($object, $fieldName)) {
31 5
            return $object->{$fieldName}();
32
        }
33
34 7
        if (method_exists($object, 'get'.$fieldName)) {
35 7
            return $object->{'get'.$fieldName}();
36
        }
37 1
        throw new RuntimeException("Field $fieldName is not exist for object ".var_export($object, true));
38
    }
39
}
40