Passed
Push — master ( c2a002...2a2be6 )
by Luis
04:38 queued 02:29
created

Variable   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

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

7 Methods

Rating   Name   Duplication   Size   Complexity  
A declaredWith() 0 3 1
A __construct() 0 4 1
A __toString() 0 6 2
A referenceName() 0 3 2
A arrayTypeName() 0 3 1
A isArray() 0 3 1
A typeName() 0 3 1
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\Variables;
9
10
use PhUml\Code\Name;
11
12
/**
13
 * It represents a variable declaration
14
 */
15
class Variable implements HasType
16
{
17
    use WithTypeDeclaration;
18
19
    /** @var string */
20
    protected $name;
21
22 189
    protected function __construct(string $name, TypeDeclaration $type)
23
    {
24 189
        $this->name = $name;
25 189
        $this->type = $type;
26 189
    }
27
28 117
    public static function declaredWith(string $name, TypeDeclaration $type = null): Variable
29
    {
30 117
        return new Variable($name, $type ?? TypeDeclaration::absent());
31
    }
32
33 57
    public function __toString()
34
    {
35 57
        return sprintf(
36 57
            '%s%s',
37 57
            $this->name,
38 57
            $this->type->isPresent() ? ": {$this->type}" : ''
39
        );
40
    }
41
42
    /**
43
     * References to arrays need to have the `[]` removed from their names in order to create
44
     * external definitions with a proper name
45
     *
46
     * The edges created from these references need to map to the names without the suffix
47
     *
48
     * @see \PhUml\Parser\Code\ExternalAssociationsResolver::resolveExternalAttributes()
49
     * @see \PhUml\Parser\Code\ExternalAssociationsResolver::resolveExternalConstructorParameters()
50
     * @see \PhUml\Graphviz\Builders\EdgesBuilder::addAssociation()
51
     */
52 57
    public function referenceName(): Name
53
    {
54 57
        return $this->isArray() ? $this->arrayTypeName() : $this->typeName();
55
    }
56
57 54
    private function typeName(): Name
58
    {
59 54
        return $this->type->name();
60
    }
61
62 57
    private function isArray(): bool
63
    {
64 57
        return $this->type->isArray();
65
    }
66
67 3
    private function arrayTypeName(): Name
68
    {
69 3
        return Name::from($this->type->removeArraySuffix());
70
    }
71
}
72