MethodAccessor::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 0
cp 0
crap 2
rs 10
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