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

Variable   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 55
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0
wmc 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A declaredWith() 0 3 1
A isBuiltIn() 0 3 1
A hasType() 0 3 1
A isAReference() 0 3 2
A type() 0 3 1
A name() 0 3 1
A __toString() 0 6 2
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