MethodAccessor::getValue()   A
last analyzed

Complexity

Conditions 4
Paths 4

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 4
eloc 10
nc 4
nop 1
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 4
rs 9.9332
c 1
b 0
f 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Serialization\Accessor;
6
7
use Chubbyphp\Serialization\SerializerLogicException;
8
9
final class MethodAccessor implements AccessorInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $property;
15
16
    public function __construct(string $property)
17
    {
18
        $this->property = $property;
19 4
    }
20
21 4
    /**
22 4
     * @param object $object
23
     *
24
     * @throws SerializerLogicException
25
     *
26
     * @return mixed
27
     */
28
    public function getValue($object)
29
    {
30
        $get = 'get'.ucfirst($this->property);
31 4
        $has = 'has'.ucfirst($this->property);
32
        $is = 'is'.ucfirst($this->property);
33 4
34 4
        if (method_exists($object, $get)) {
35 4
            return $object->{$get}();
36
        }
37 4
38 1
        if (method_exists($object, $has)) {
39
            return $object->{$has}();
40
        }
41 3
42 1
        if (method_exists($object, $is)) {
43
            return $object->{$is}();
44
        }
45 2
46 1
        throw SerializerLogicException::createMissingMethod(get_class($object), [$get, $has, $is]);
47
    }
48
}
49