MethodAccessor   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 13
dl 0
loc 38
ccs 13
cts 13
cp 1
rs 10
c 1
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A getValue() 0 19 4
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