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

Variable::arrayTypeName()   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\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