Completed
Push — code_refactoring ( 3ee845 )
by Luis
14:42
created

Variable::type()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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
    protected function __construct(string $name, TypeDeclaration $type)
22
    {
23
        $this->name = $name;
24
        $this->type = $type;
25
    }
26
27
    public static function declaredWith(string $name, TypeDeclaration $type = null): Variable
28
    {
29
        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
    public function isAReference(): bool
38
    {
39
        return $this->hasType() && !$this->isBuiltIn();
40
    }
41
42
    private function hasType(): bool
43
    {
44
        return $this->type->isPresent();
45
    }
46
47
    private function isBuiltIn(): bool
48
    {
49
        return $this->type->isBuiltIn();
50
    }
51
52
    public function name(): string
53
    {
54
        return $this->name;
55
    }
56
57
    public function type(): TypeDeclaration
58
    {
59
        return $this->type;
60
    }
61
62
    public function __toString()
63
    {
64
        return sprintf(
65
            '%s%s',
66
            $this->type->isPresent() ? "{$this->type} " : '',
67
            $this->name
68
        );
69
    }
70
}
71