Property   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 246
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 100%

Importance

Changes 10
Bugs 2 Features 0
Metric Value
wmc 21
c 10
b 2
f 0
lcom 1
cbo 5
dl 0
loc 246
ccs 63
cts 63
cp 1
rs 10

17 Methods

Rating   Name   Duplication   Size   Complexity  
A init() 0 5 1
A getName() 0 4 1
A setName() 0 6 1
A getValue() 0 4 1
A hasValue() 0 4 2
A setValue() 0 10 2
A getType() 0 4 1
A setType() 0 11 1
A getDocBlock() 0 4 1
A setDocBlock() 0 6 1
A getVisibility() 0 4 1
A setVisibility() 0 7 1
A isStatic() 0 4 1
A setIsStatic() 0 6 1
A getDescription() 0 4 1
A setDescription() 0 6 1
A toString() 0 21 3
1
<?php
2
3
/*
4
 * This file is part of the ClassGeneration package.
5
 *
6
 * (c) Antonio Spinelli <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace ClassGeneration;
13
14
use ClassGeneration\DocBlock\Tag;
15
use ClassGeneration\DocBlockInterface;
16
use ClassGeneration\Element\ElementAbstract;
17
18
/**
19
 * Property ClassGeneration
20
 * @author Antonio Spinelli <[email protected]>
21
 */
22
class Property extends ElementAbstract implements PropertyInterface
23
{
24
25
    /**
26
     * Property's name
27
     * @var string
28
     */
29
    protected $name;
30
31
    /**
32
     * Property's value.
33
     * @var mixed
34
     */
35
    protected $value;
36
37
    /**
38
     * Property's type.
39
     * @var string
40
     */
41
    protected $type;
42
43
    /**
44
     * Element Visibility.
45
     * @var string
46
     */
47
    protected $visibility;
48
49
    /**
50
     * Element is static.
51
     * @var bool
52
     */
53
    protected $isStatic;
54
55
    /**
56
     * Documentation Block
57
     * @var DocBlockInterface
58
     */
59
    protected $docBlock;
60
61
    /**
62
     * {@inheritdoc}
63
     */
64 25
    public function init()
65
    {
66 25
        $this->setDocBlock(new DocBlock())
67 25
            ->setVisibility(Visibility::TYPE_PUBLIC);
68 25
    }
69
70
    /**
71
     * Gets the property's name
72
     * @return string
73
     */
74 16
    public function getName()
75
    {
76 16
        return $this->name;
77
    }
78
79
    /**
80
     * Sets the property's name.
81
     *
82
     * @param string $name
83
     *
84
     * @return Property
85
     */
86 17
    public function setName($name)
87
    {
88 17
        $this->name = (string)$name;
89
90 17
        return $this;
91
    }
92
93
    /**
94
     * Gets the property's value.
95
     * @return mixed
96
     */
97 3
    public function getValue()
98
    {
99 3
        return $this->value;
100
    }
101
102
    /**
103
     * Check the property's value.
104
     * @return boolean
105
     */
106 9
    public function hasValue()
107
    {
108 9
        return (!is_null($this->value) || !empty($this->value));
109
    }
110
111
    /**
112
     * Sets the property's value.
113
     *
114
     * @param mixed $value
115
     *
116
     * @throws \InvalidArgumentException
117
     * @return Property
118
     */
119 6
    public function setValue($value)
120
    {
121 6
        if (is_object($value)) {
122 1
            throw new \InvalidArgumentException('Object is not allowed in value for Property.');
123
        }
124
125 6
        $this->value = $value;
126
127 6
        return $this;
128
    }
129
130
    /**
131
     * Gets the property's type.
132
     * @return string
133
     */
134 4
    public function getType()
135
    {
136 4
        return $this->type;
137
    }
138
139
    /**
140
     * Sets the property's type.
141
     *
142
     * @param string $type
143
     *
144
     * @return Property
145
     */
146 3
    public function setType($type)
147
    {
148 3
        $this->type = (string)$type;
149 3
        $this->getDocBlock()->getTagCollection()->removeByName(Tag::TAG_VAR);
150
151 3
        $tag = Tag::createFromProperty($this);
152
153 3
        $this->getDocBlock()->addTag($tag);
154
155 3
        return $this;
156
    }
157
158
    /**
159
     * {@inheritdoc}
160
     */
161 10
    public function getDocBlock()
162
    {
163 10
        return $this->docBlock;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     * @return Property
169
     */
170 25
    public function setDocBlock(DocBlockInterface $docBlock)
171
    {
172 25
        $this->docBlock = $docBlock;
173
174 25
        return $this;
175
    }
176
177
    /**
178
     * {@inheritdoc}
179
     */
180 9
    public function getVisibility()
181
    {
182 9
        return $this->visibility;
183
    }
184
185
    /**
186
     * {@inheritdoc}
187
     * @return Property
188
     */
189 25
    public function setVisibility($visibility)
190
    {
191 25
        Visibility::isValid($visibility);
192 25
        $this->visibility = $visibility;
193
194 25
        return $this;
195
    }
196
197
    /**
198
     * Is a static element?
199
     * @return bool
200
     */
201 8
    public function isStatic()
202
    {
203 8
        return $this->isStatic;
204
    }
205
206
    /**
207
     * Sets this property like static
208
     *
209
     * @param bool $isStatic
210
     *
211
     * @return Property
212
     */
213 1
    public function setIsStatic($isStatic = true)
214
    {
215 1
        $this->isStatic = (bool)$isStatic;
216
217 1
        return $this;
218
    }
219
220
    /**
221
     * Gets a description
222
     * @return string
223
     */
224 1
    public function getDescription()
225
    {
226 1
        return $this->getDocBlock()->getDescription();
227
    }
228
229
    /**
230
     * Sets a description.
231
     *
232
     * @param $description
233
     *
234
     * @return Property
235
     */
236 1
    public function setDescription($description)
237
    {
238 1
        $this->getDocBlock()->setDescription($description);
239
240 1
        return $this;
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     */
246 8
    public function toString()
247
    {
248 8
        $static = '';
249 8
        if ($this->isStatic()) {
250 1
            $static = 'static ';
251 1
        }
252
253 8
        $value = '';
254 8
        if ($this->hasValue()) {
255 2
            $value = ' = ' . var_export($this->getValue(), true);
256 2
        }
257
258 8
        $property = $this->getDocBlock()->toString() . $this->getTabulationFormatted()
259 8
            . $this->getVisibility() . ' '
260 8
            . $static
261 8
            . '$' . $this->getName()
262 8
            . $value
263 8
            . ';' . PHP_EOL;
264
265 8
        return $property;
266
    }
267
}
268