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
c 1
b 0
f 0
nc 4
nop 1
dl 0
loc 19
ccs 10
cts 10
cp 1
crap 4
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Chubbyphp\Deserialization\Accessor;
6
7
use Chubbyphp\Deserialization\DeserializerLogicException;
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 6
    }
20
21 6
    /**
22 6
     * @param object $object
23
     * @param mixed  $value
24
     *
25
     * @throws DeserializerLogicException
26
     */
27
    public function setValue($object, $value)
28
    {
29
        $set = 'set'.ucfirst($this->property);
30 2
        if (!method_exists($object, $set)) {
31
            throw DeserializerLogicException::createMissingMethod(get_class($object), [$set]);
32 2
        }
33 2
34 1
        return $object->{$set}($value);
35
    }
36
37 1
    /**
38
     * @param object $object
39
     *
40
     * @throws DeserializerLogicException
41
     *
42
     * @return mixed
43
     */
44
    public function getValue($object)
45
    {
46
        $get = 'get'.ucfirst($this->property);
47 4
        $has = 'has'.ucfirst($this->property);
48
        $is = 'is'.ucfirst($this->property);
49 4
50 4
        if (method_exists($object, $get)) {
51 4
            return $object->{$get}();
52
        }
53 4
54 1
        if (method_exists($object, $has)) {
55
            return $object->{$has}();
56
        }
57 3
58 1
        if (method_exists($object, $is)) {
59
            return $object->{$is}();
60
        }
61 2
62 1
        throw DeserializerLogicException::createMissingMethod(get_class($object), [$get, $has, $is]);
63
    }
64
}
65