Argument::hasType()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4286
cc 3
eloc 3
nc 3
nop 0
crap 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\Element\ElementAbstract;
15
16
/**
17
 * Argument ClassGeneration
18
 * @author Antonio Spinelli <[email protected]>
19
 */
20
class Argument extends ElementAbstract implements ArgumentInterface
21
{
22
23
    /**
24
     * Argument's name.
25
     * @var string
26
     */
27
    protected $name;
28
29
    /**
30
     * Argument's default value.
31
     * @var mixed
32
     */
33
    protected $value;
34
35
    /**
36
     * Argument's is optional.
37
     * @var bool
38
     */
39
    protected $isOptional = false;
40
41
    /**
42
     * Argument's description.
43
     * @var string
44
     */
45
    protected $description;
46
47
    /**
48
     * Argument's type.
49
     * @var string
50
     */
51
    protected $type;
52
53
    /**
54
     * Primitive types.
55
     * @var array
56
     */
57
    protected $primitiveTypes = array(
58
        'bool', 'boolean',
59
        'decimal', 'double',
60
        'float',
61
        'int', 'integer',
62
        'number',
63
        'string',
64
        'resource',
65
    );
66
67
    /**
68
     * {@inheritdoc}
69
     */
70 26
    public function init()
71
    {
72 26
        $this->setIsOptional(false);
73 26
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 21
    public function getName()
79
    {
80 21
        return $this->name;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86 16
    public function getNameFormatted()
87
    {
88 16
        return '$' . $this->getName();
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94 21
    public function setName($name)
95
    {
96 21
        $this->name = (string)$name;
97
98 21
        return $this;
99
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 4
    public function getValue()
105
    {
106 4
        return $this->value;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 2
    public function setValue($value)
113
    {
114 2
        $this->value = $value;
115 2
        $this->setIsOptional();
116
117 2
        return $this;
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 15
    public function isOptional()
124
    {
125 15
        return $this->isOptional;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131 26
    public function setIsOptional($isOptional = true)
132
    {
133 26
        $this->isOptional = (bool)$isOptional;
134
135 26
        return $this;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141 13
    public function getType()
142
    {
143 13
        return $this->type;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149 15
    public function hasType()
150
    {
151
152 15
        return (!is_null($this->type) || !empty($this->type))
153 15
        && !in_array(mb_strtolower($this->type), $this->primitiveTypes);
154
    }
155
156
    /**
157
     * {@inheritdoc}
158
     */
159 8
    public function setType($type)
160
    {
161 8
        $this->type = (string)$type;
162
163 8
        return $this;
164
    }
165
166
    /**
167
     * {@inheritdoc}
168
     */
169 9
    public function getDescription()
170
    {
171 9
        return $this->description;
172
    }
173
174
    /**
175
     * {@inheritdoc}
176
     */
177 1
    public function setDescription($description)
178
    {
179 1
        $this->description = $description;
180
181 1
        return $this;
182
    }
183
184
    /**
185
     * {@inheritdoc}
186
     */
187 14
    public function toString()
188
    {
189 14
        $type = '';
190 14
        if ($this->hasType()) {
191 4
            $type = $this->getType() . ' ';
192 4
        }
193 14
        $value = '';
194 14
        if ($this->isOptional()) {
195 3
            $value = ' = ' . var_export($this->getValue(), true);
196 3
        }
197 14
        $argument = trim(
198
            $type
199 14
            . $this->getNameFormatted()
200 14
            . $value
201 14
        );
202
203 14
        return $argument;
204
    }
205
206
    /**
207
     * Creates Argument from Property Object.
208
     *
209
     * @param PropertyInterface $property
210
     *
211
     * @return Argument
212
     */
213 1
    public static function createFromProperty(PropertyInterface $property)
214
    {
215 1
        $argument = new self();
216
        $argument
217 1
            ->setName($property->getName())
218 1
            ->setType($property->getType());
219
220 1
        return $argument;
221
    }
222
}
223