Constant::getName()   A
last analyzed

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 3
Bugs 2 Features 0
Metric Value
c 3
b 2
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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\Collection\ArrayCollection;
15
use ClassGeneration\Element\ElementAbstract;
16
17
/**
18
 * Constants ClassGeneration
19
 * @author Antonio Spinelli <[email protected]>
20
 */
21
class Constant extends ElementAbstract implements ConstantInterface
22
{
23
24
    /**
25
     * constant's name
26
     * @var string
27
     */
28
    protected $name;
29
30
    /**
31
     * @var mixed
32
     */
33
    protected $value;
34
35
    /**
36
     * Alloweds php types.
37
     * @var ArrayCollection
38
     */
39
    protected $allowedTypes;
40
41
    /**
42
     * Documentation Block
43
     * @var DocBlockInterface
44
     */
45
    protected $docBlock;
46
47
    /**
48
     * {@inheritdoc}
49
     */
50 11
    public function init()
51
    {
52 11
        $this->setDocBlock(new DocBlock());
53 11
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 7
    public function setName($name)
59
    {
60 7
        $this->name = (string)$name;
61
62 7
        return $this;
63
    }
64
65
66
    /**
67
     * {@inheritdoc}
68
     */
69 7
    public function getName()
70
    {
71 7
        return $this->name;
72
    }
73
74
    /**
75
     * {@inheritdoc}
76
     */
77 5
    public function setValue($value)
78
    {
79 5
        if (!is_string($value) && !is_numeric($value)) {
80 1
            throw new \InvalidArgumentException('The constant value must be a string or number');
81
        }
82 5
        $this->value = $value;
83
84 5
        return $this;
85
    }
86
87
    /**
88
     * {@inheritdoc}
89
     */
90 3
    public function getValue()
91
    {
92 3
        return $this->value;
93
    }
94
95
    /**
96
     * {@inheritdoc}
97
     */
98 1
    public function setDescription($description)
99
    {
100 1
        $this->getDocBlock()->setDescription($description);
101
102 1
        return $this;
103
    }
104
105
    /**
106
     * {@inheritdoc}
107
     */
108 2
    public function getDescription()
109
    {
110 2
        return $this->getDocBlock()->getDescription();
111
    }
112
113
    /**
114
     * {@inheritdoc}
115
     */
116 4
    public function getDocBlock()
117
    {
118 4
        return $this->docBlock;
119
    }
120
121
    /**
122
     * {@inheritdoc}
123
     * @return \ClassGeneration\Constant
124
     */
125 11
    public function setDocBlock(DocBlockInterface $docBlock)
126
    {
127 11
        $this->docBlock = $docBlock;
128
129 11
        return $this;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135 2
    public function toString()
136
    {
137 2
        $constant = $this->getDocBlock()->toString()
138 2
            . $this->getTabulationFormatted()
139 2
            . 'const '
140 2
            . mb_strtoupper($this->getName())
141 2
            . ' = ' . var_export($this->getValue(), true)
142 2
            . ';' . PHP_EOL;
143
144 2
        return $constant;
145
    }
146
}
147