Completed
Push — master ( bc20b4...b7ce52 )
by Nate
03:15
created

Accessor::__construct()   B

Complexity

Conditions 5
Paths 8

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 14
ccs 8
cts 8
cp 1
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 7
nc 8
nop 1
crap 5
1
<?php
2
/*
3
 * Copyright (c) Nate Brunette.
4
 * Distributed under the MIT License (http://opensource.org/licenses/MIT)
5
 */
6
7
namespace Tebru\Gson\Annotation;
8
9
use RuntimeException;
10
use Tebru\AnnotationReader\AbstractAnnotation;
11
12
/**
13
 * Class Accessor
14
 *
15
 * Use this annotation to explicitly define a getter/setter method mapping
16
 *
17
 * @author Nate Brunette <[email protected]>
18
 *
19
 * @Annotation
20
 * @Target({"PROPERTY"})
21
 */
22
class Accessor extends AbstractAnnotation
23
{
24
    /**
25
     * Method name representing getter
26
     *
27
     * @var string
28
     */
29
    private $get;
30
31
    /**
32
     * Method name representing setter
33
     *
34
     * @var string
35
     */
36
    private $set;
37
38
    /**
39
     * Constructor
40
     *
41
     * @throws \RuntimeException
42
     */
43 4
    protected function init(): void
44
    {
45 4
        $this->get = $this->data['get'] ?? null;
46 4
        $this->set = $this->data['set'] ?? null;
47
48 4
        if (null === $this->get && null === $this->set) {
49 1
            throw new RuntimeException('@Accessor annotation must specify either get or set key');
50
        }
51 3
    }
52
53
    /**
54
     * A method name representing the getter
55
     *
56
     * @return string
57
     */
58 2
    public function getter(): ?string
59
    {
60 2
        return $this->get;
61
    }
62
63
    /**
64
     * A method name representing the setter
65
     *
66
     * @return string
67
     */
68 2
    public function setter(): ?string
69
    {
70 2
        return $this->set;
71
    }
72
}
73