Completed
Push — master ( 716959...c2acd0 )
by Luis
04:37 queued 02:39
created

Variable::name()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * PHP version 7.1
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;
9
10
/**
11
 * It represents a variable declaration
12
 */
13
class Variable
14
{
15
    /** @var string */
16
    protected $name;
17
18
    /** @var TypeDeclaration */
19
    protected $type;
20
21 129
    protected function __construct(string $name, TypeDeclaration $type)
22
    {
23 129
        $this->name = $name;
24 129
        $this->type = $type;
25 129
    }
26
27 87
    public static function declaredWith(string $name, TypeDeclaration $type = null): Variable
28
    {
29 87
        return new Variable($name, $type ?? TypeDeclaration::absent());
30
    }
31
32
    /**
33
     * An attribute is a reference if it has a type and it's not a built-in type
34
     *
35
     * This is used when building the digraph and the option `createAssociations` is set
36
     */
37 27
    public function isAReference(): bool
38
    {
39 27
        return $this->hasType() && !$this->isBuiltIn();
40
    }
41
42 27
    private function hasType(): bool
43
    {
44 27
        return $this->type->isPresent();
45
    }
46
47 24
    private function isBuiltIn(): bool
48
    {
49 24
        return $this->type->isBuiltIn();
50
    }
51
52 6
    public function name(): string
53
    {
54 6
        return $this->name;
55
    }
56
57 18
    public function type(): TypeDeclaration
58
    {
59 18
        return $this->type;
60
    }
61
62 27
    public function __toString()
63
    {
64 27
        return sprintf(
65 27
            '%s%s',
66 27
            $this->type->isPresent() ? "{$this->type} " : '',
67 27
            $this->name
68
        );
69
    }
70
}
71