Passed
Push — variadic-params-visibility-con... ( ca0a66 )
by Luis
13:54
created

Constant   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 28
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 13
c 0
b 0
f 0
dl 0
loc 28
rs 10
wmc 4

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 7 2
A __construct() 0 5 1
A referenceName() 0 3 1
1
<?php declare(strict_types=1);
2
/**
3
 * PHP version 7.2
4
 *
5
 * This source file is subject to the license that is bundled with this package in the file LICENSE.
6
 */
7
8
namespace PhUml\Code\Attributes;
9
10
use BadMethodCallException;
11
use PhUml\Code\Modifiers\HasVisibility;
12
use PhUml\Code\Modifiers\Visibility;
13
use PhUml\Code\Modifiers\WithVisibility;
14
use PhUml\Code\Name;
15
use PhUml\Code\Variables\HasType;
16
use PhUml\Code\Variables\TypeDeclaration;
17
use PhUml\Code\Variables\WithTypeDeclaration;
18
19
final class Constant implements HasType, HasVisibility
20
{
21
    use WithTypeDeclaration;
22
    use WithVisibility;
23
24
    /** @var string */
25
    private $name;
26
27
    public function __construct(string $name, TypeDeclaration $type, Visibility $visibility = null)
28
    {
29
        $this->name = $name;
30
        $this->type = $type;
31
        $this->modifier = $visibility ?? Visibility::public();
32
    }
33
34
    public function __toString()
35
    {
36
        return sprintf(
37
            '%s%s%s',
38
            $this->modifier,
39
            $this->name,
40
            $this->type->isPresent() ? ": {$this->type}" : ''
41
        );
42
    }
43
44
    public function referenceName(): Name
45
    {
46
        throw new BadMethodCallException('Constants must be built-in types');
47
    }
48
}
49