Completed
Push — master ( ac0b2b...25e509 )
by Nate
02:57
created

PropertyMetadata::getSerializedName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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;
8
9
use Tebru\Gson\Internal\Data\AnnotationSet;
10
11
/**
12
 * Class PropertyMetadata
13
 *
14
 * Represents a property and its annotations
15
 *
16
 * @author Nate Brunette <[email protected]>
17
 */
18
final class PropertyMetadata
19
{
20
    /**
21
     * The property name
22
     *
23
     * @var string
24
     */
25
    private $name;
26
27
    /**
28
     * The property's serialized name
29
     *
30
     * @var string
31
     */
32
    private $serializedName;
33
34
    /**
35
     * The property type
36
     *
37
     * @var PhpType
38
     */
39
    private $type;
40
41
    /**
42
     * The property modifiers (public, private, etc)
43
     *
44
     * @var int
45
     */
46
    private $modifiers;
47
48
    /**
49
     * The property declaring class metadata
50
     *
51
     * @var ClassMetadata
52
     */
53
    private $classMetadata;
54
55
    /**
56
     * The property's annotations
57
     *
58
     * @var AnnotationSet
59
     */
60
    private $annotations;
61
62
    /**
63
     * If the property is a virtual property
64
     *
65
     * @var bool
66
     */
67
    private $virtual;
68
69
    /**
70
     * Constructor
71
     *
72
     * @param string $name
73
     * @param string $serializedName
74
     * @param PhpType $type
75
     * @param string $modifiers
76
     * @param ClassMetadata $classMetadata
77
     * @param AnnotationSet $annotations
78
     * @param bool $virtual
79
     */
80 5 View Code Duplication
    public function __construct(
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
81
        string $name,
82
        string $serializedName,
83
        PhpType $type,
84
        string $modifiers,
85
        ClassMetadata $classMetadata,
86
        AnnotationSet $annotations,
87
        bool $virtual
88
    )
89
    {
90 5
        $this->name = $name;
91 5
        $this->serializedName = $serializedName;
92 5
        $this->type = $type;
93 5
        $this->modifiers = $modifiers;
0 ignored issues
show
Documentation Bug introduced by
The property $modifiers was declared of type integer, but $modifiers is of type string. Maybe add a type cast?

This check looks for assignments to scalar types that may be of the wrong type.

To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.

$answer = 42;

$correct = false;

$correct = (bool) $answer;
Loading history...
94 5
        $this->classMetadata = $classMetadata;
95 5
        $this->annotations = $annotations;
96 5
        $this->virtual = $virtual;
97 5
    }
98
99
    /**
100
     * Get the property name
101
     *
102
     * @return string
103
     */
104 1
    public function getName(): string
105
    {
106 1
        return $this->name;
107
    }
108
109
    /**
110
     * Get the property serialized name
111
     *
112
     * @return string
113
     */
114 1
    public function getSerializedName(): string
115
    {
116 1
        return $this->serializedName;
117
    }
118
119
    /**
120
     * Get the full php type object
121
     *
122
     * @return PhpType
123
     */
124 1
    public function getType(): PhpType
125
    {
126 1
        return $this->type;
127
    }
128
129
    /**
130
     * Get the property type as a string
131
     *
132
     * @return string
133
     */
134 1
    public function getTypeName(): string
135
    {
136 1
        return (string) $this->type;
137
    }
138
139
    /**
140
     * Get the property modifiers as a bitmap of [@see \ReflectionProperty] constants
141
     *
142
     * @return int
143
     */
144 1
    public function getModifiers(): int
145
    {
146 1
        return $this->modifiers;
147
    }
148
149
    /**
150
     * Get full declaring class metadata
151
     *
152
     * @return ClassMetadata
153
     */
154 1
    public function getDeclaringClassMetadata(): ClassMetadata
155
    {
156 1
        return $this->classMetadata;
157
    }
158
159
    /**
160
     * Get the declaring class name
161
     *
162
     * @return string
163
     */
164 1
    public function getDeclaringClassName(): string
165
    {
166 1
        return $this->classMetadata->getName();
167
    }
168
169
    /**
170
     * Get property annotations
171
     *
172
     * @return AnnotationSet
173
     */
174 1
    public function getAnnotations(): AnnotationSet
175
    {
176 1
        return $this->annotations;
177
    }
178
179
    /**
180
     * Get a single annotation, returns null if the annotation doesn't exist
181
     *
182
     * @param string $annotationName
183
     * @return null|object
184
     */
185 4
    public function getAnnotation(string $annotationName)
186
    {
187 4
        return $this->virtual
188 2
            ? $this->annotations->getAnnotation($annotationName, AnnotationSet::TYPE_METHOD)
189 4
            : $this->annotations->getAnnotation($annotationName, AnnotationSet::TYPE_PROPERTY);
190
    }
191
192
    /**
193
     * Returns true if the property is virtual
194
     *
195
     * @return bool
196
     */
197 1
    public function isVirtual(): bool
198
    {
199 1
        return $this->virtual;
200
    }
201
}
202