Passed
Pull Request — master (#20)
by Igor
02:11
created

ObjectFunctions   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 31
Duplicated Lines 0 %

Test Coverage

Coverage 91.67%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 12
dl 0
loc 31
ccs 11
cts 12
cp 0.9167
rs 10
c 3
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B getPropertyValue() 0 23 7
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 50
    public static function getPropertyValue($object, string $fieldName)
20
    {
21 50
        if (is_array($object)) {
22 21
            return $object[$fieldName] ?? null;
23
        }
24
25
        /** @noinspection PhpUnhandledExceptionInspection */
26 30
        if (property_exists($object, $fieldName) && (new ReflectionProperty($object, $fieldName))->isPublic()) {
27 12
            return $object->{$fieldName};
28
        }
29
30 21
        if (isset($object->{$fieldName})) {
31
            return $object->{$fieldName};
32
        }
33
34 21
        if (method_exists($object, $fieldName)) {
35 5
            return $object->{$fieldName}();
36
        }
37
38 18
        if (method_exists($object, 'get'.$fieldName)) {
39 17
            return $object->{'get'.$fieldName}();
40
        }
41 2
        throw new RuntimeException("Field $fieldName is not exist for object ".var_export($object, true));
42
    }
43
}
44