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

ObjectFunctions::getPropertyValue()   A

Complexity

Conditions 6
Paths 5

Size

Total Lines 19
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 6.0359

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 19
ccs 9
cts 10
cp 0.9
rs 9.2222
cc 6
nc 5
nop 2
crap 6.0359
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